package hardware

import (
	"container/ring"
	"math/rand"
	"netease_control_center/globals"
	"netease_control_center/interfaces/log"
	"netease_control_center/nembd"
	"netease_control_center/nembd/controller/is31fl3732a"
	_ "netease_control_center/nembd/host/ihw1"
	"strconv"
	"time"
)

var (
	micleds []nembd.LED
)

func Init() {

	for i := 0; i <= 11; i = i + 1 {
		led, _ := nembd.NewLED(i)
		led.Rgb(0, 0, 0)
		micleds = append(micleds, led)
		log.Println(log.DebugLog, "Add led ", i)
	}
}

const brightness = 4

type color struct {
	r int
	g int
	b int
}

var (
	togaltest  bool = false
	togaltest2 bool = false
)

func Togaltest() {
	if togaltest {
		togaltest = false
		micleds[5].Rgb(50, 0, 0)
	} else {
		togaltest = true
		micleds[5].Rgb(0, 0, 0)
	}
}
func Togaltest2() {
	if togaltest2 {
		togaltest2 = false
		micleds[7].Rgb(0, 50, 0)
	} else {
		togaltest2 = true
		micleds[7].Rgb(0, 0, 0)
	}
}

func nshow(n int) {
	micleds[n].Rgb(rand.Intn(255)*brightness/100, rand.Intn(255)*brightness/100, rand.Intn(255)*brightness/100)
}

func nrgbshow(n, r, g, b int) {
	if n >= len(micleds) {
		return
	}
	micleds[n].Rgb(r*brightness/100, g*brightness/100, b*brightness/100)
}

func noff(n int) {
	micleds[n].Rgb(0, 0, 0)
}
func Show1() {
	for k, _ := range micleds {
		rand.Seed(time.Now().UTC().UnixNano())
		nshow(k)
		time.Sleep(time.Millisecond * time.Duration(80))
	}
}

func Show2() {
	num := len(micleds)
	rand.Seed(time.Now().UTC().UnixNano())
	nshow(rand.Intn(num))
	time.Sleep(time.Millisecond * time.Duration(50))
}

func Show3() {
	num := len(micleds)
	rand.Seed(time.Now().UTC().UnixNano())
	n := rand.Intn(num)
	nshow(n)
	time.Sleep(time.Millisecond * time.Duration(50))
	noff(n)
}

func Show4(cur *globals.MicLedStatuses, should globals.MicLedStatuses) {
	num := len(micleds)
	ledring := ring.New(num)
	for p, i := ledring, 0; i < num; i, p = i+1, p.Next() {
		va := color{}
		rand.Seed(time.Now().UTC().UnixNano())
		va.r = rand.Intn(256)
		rand.Seed(time.Now().UTC().UnixNano())
		va.g = rand.Intn(256)
		rand.Seed(time.Now().UTC().UnixNano())
		va.b = rand.Intn(256)

		p.Value = va
		log.Println(log.DebugLog, i, va)
	}

	for {
		no := 0
		ledring.Do(func(i interface{}) {
			co, _ := i.(color)
			micleds[no].Rgb(co.r*brightness/100, co.g*brightness/100, co.b*brightness/100)
			no = no + 1
		})
		ledring = ledring.Prev()
		time.Sleep(100 * time.Millisecond)

		if *cur != should {
			break
		}
	}
}

func Show5(cur *globals.MicLedStatuses, should globals.MicLedStatuses) {
	num := len(micleds)
	ledring := ring.New(num)
	for p, i := ledring, 0; i < num; i, p = i+1, p.Next() {
		if i%2 != 0 {
			continue
		}
		va := color{}
		rand.Seed(time.Now().UTC().UnixNano())
		va.r = rand.Intn(256)
		rand.Seed(time.Now().UTC().UnixNano())
		va.g = rand.Intn(256)
		rand.Seed(time.Now().UTC().UnixNano())
		va.b = rand.Intn(256)

		p.Value = va
	}

	for {
		no := 0
		ledring.Do(func(i interface{}) {
			co, _ := i.(color)
			micleds[no].Rgb(co.r*brightness/100, co.g*brightness/100, co.b*brightness/100)
			no = no + 1
		})
		ledring = ledring.Prev()
		time.Sleep(200 * time.Millisecond)

		if *cur != should {
			break
		}
	}

}

//pao ma deng
func Show6(cur *globals.MicLedStatuses, should globals.MicLedStatuses) {
	lightnums := 5
	num := len(micleds)
	ledring := ring.New(num)
	for p, i := ledring, 0; i < num; i, p = i+1, p.Next() {
		va := color{}
		if lightnums > 0 {
			rand.Seed(time.Now().UTC().UnixNano())
			va.r = rand.Intn(256)
			rand.Seed(time.Now().UTC().UnixNano())
			va.g = rand.Intn(256)
			rand.Seed(time.Now().UTC().UnixNano())
			va.b = rand.Intn(256)
			lightnums = lightnums - 1
		}

		p.Value = va
	}

	for {
		no := 0
		ledring.Do(func(i interface{}) {
			co, _ := i.(color)
			micleds[no].Rgb(co.r*brightness/100, co.g*brightness/100, co.b*brightness/100)
			no = no + 1
		})
		ledring = ledring.Prev()
		time.Sleep(100 * time.Millisecond)
		if *cur != should {
			break
		}
	}
}

func Show7() {
	for k, _ := range micleds {
		nrgbshow(k, 100, 100, 100)
	}
}

func Show8(r, g, b int) {
	for k, _ := range micleds {
		nrgbshow(k, r, g, b)
	}
}

func Ledtest() {
	go func() {
		return
		for {
			log.Println(log.DebugLog, "just test")
			Show4(&globals.CurMicLedStatus, globals.MicLedStatuses_TEST)
		}
	}()

	leds := is31fl3732a.New(9, 9)
	leds.On()
	leds.SetBtrightness(5)
	leds.Test()
	//go leds.FixedTest()
}

func MicLedsAllOff() {
	for k, _ := range micleds {
		noff(k)
	}
}

func MicAng(ang int) {
	main := int(ang / 60)
	minor := int(ang % 60)

	if minor > 30 {
		main++
	}
	if main >= len(micleds)/2 {
		main = 0
	}
	log.Println(log.DebugLog, "the angle", ang, main)
	MicLedsAllOff()
	nembd.LEDRgbOn("ang"+strconv.Itoa(main)+"-0", 255*brightness/100, 255*brightness/100, 255*brightness/100)
	nembd.LEDRgbOn("ang"+strconv.Itoa(main)+"-1", 255*brightness/100, 255*brightness/100, 255*brightness/100)
}

func ShowVolumeLeds(vol int) {
	var n int
	n = len(micleds) * vol / globals.MaxVolume
	if vol == 1 {
		n = 1
	}
	for i := 0; i < len(micleds); i++ {
		if i < n {
			nrgbshow(i, 255, 0, 0)
		} else {
			nrgbshow(i, 0, 0, 0)
		}
	}
}
