package controller

import (
	"fmt"
	"netease_control_center/globals"
	"netease_control_center/interfaces/log"
	. "netease_control_center/interfaces/vui"
	"netease_control_center/interfaces/vui/cvui"
	. "netease_control_center/modules/hardware/led_mcu"
	. "netease_control_center/modules/hardware/mcu_leds/atservice"
	"netease_control_center/utils"
	"time"
)

func init() {
	DoShowIcon = ledShow
	McuReadyCb = func() {
		log.Println(log.DebugLog, "Restore last icon and led!!")
		if globals.IsMicClose == true {
			ShowDirectLed(DirectLed{EffectId: GUIDE_EFFECT_ID_MIC_STAT_OFF, Action: GUIDE_ACTION_START, StartNum: 0, Data: []byte{}})
		}
		RestoreLastIcon()
		ShowLastDirectLed()
	}

	globals.IconLedMusicPlayMode.RegisterCb(func(e interface{}, cbType globals.CallCbType) {
		if cbType == globals.REBOOT_CALL {
			IconLed_Clear.Show()
			return
		}
		if getCurDeviceMode(false) == globals.Exec_MusicPlay_mode {
			ShowIconMusic()
		}
	})
}

//Warn: do not exec this func directly
func ledShow(icon IconLedDesc) {
	log.Println(log.DebugLog, "Deeper layer icon, Begin to show:", icon.Name)
	//jumpin := time.Now()
	switch icon.Name {
	case IconLed_iconMusic001.Name:
		Matrix_music_mode(MATRIX_MUSIC_PLAY_SYNC)
	case IconLed_iconMusic002.Name:
		Matrix_music_mode(MATRIX_MUSIC_PLAY_FILTER)
	case IconLed_iconMusic003.Name:
		Matrix_music_mode(MATRIX_MUSIC_PLAY_LINE)
	case IconLed_iconMusic004.Name:
		Matrix_music_mode(MATRIX_SPEAK_PLAY_LINE)
	case IconLed_Clear.Name:
		ClearLedDisplay()
	case IconLed_StopMusicMode.Name:
		Matrix_music_mode(MATRIX_MUSIC_PLAY_NONE)
	case IconLed_ShowVolume.Name:
		if vol, volok := icon.Arg.(int); volok == true {
			DeeperShowVolumeIcon(vol)
			//jumpin := time.Now()
			//if vol <= 10 {
			//	restag := fmt.Sprintf("p%d", 500+vol)
			//	if path := cvui.GetSkinRes(restag); len(path) > 0 {
			//		log.Println(log.DebugLog, "Get skin time:", time.Now().Sub(jumpin))
			//		ShowIconWithPath(path)
			//	} else {
			//		log.Println(log.DebugLog, "Get res fail!")
			//	}
			//} else if vol >10 && vol <100{
			//	leftint:= vol/10
			//	rightint := vol%10
			//	lefttag := fmt.Sprintf("p%d", leftint+510)
			//	righttag := fmt.Sprintf("p%d", rightint+520)
			//	if leftpath, rightpath := cvui.GetSkinRes(lefttag), cvui.GetSkinRes(righttag); len(leftpath) > 0 && len(rightpath) > 0 {
			//		fmt.Println(log.DebugLog, "Get skin time:", time.Now().Sub(jumpin))
			//		Show2Icon(leftpath, rightpath)
			//	} else {
			//		log.Println(log.ErrorLog, "Get path error!", righttag," = ", rightpath,"  ", lefttag, " = ", leftpath)
			//	}
			//} else if vol == 100 {
			//	if path := cvui.GetSkinRes("p531"); len(path) > 0 {
			//		log.Println(log.DebugLog, "Get skin time:", time.Now().Sub(jumpin))
			//		ShowIconWithPath(path)
			//	} else {
			//		log.Println(log.DebugLog, "Get res fail!")
			//	}
			//} else {
			//	log.Println(log.ErrorLog, "Show volume err:", vol)
			//}
		}
	default:
		if path := icon.GetPath(); len(path) > 0 {
			ShowIconWithPath(path)
		} else {
			ClearLedDisplay()
			log.Println(log.ErrorLog, "Get led res path len not match,", path)
		}
	}
}

func DeeperShowVolumeIcon(vol int) {
	//jumpin := time.Now()
	resId := fmt.Sprintf("p%d", 500+vol)

	if path := cvui.GetSkinRes(resId); len(path) > 0 {
		//log.Println(log.DebugLog, "Get skin time:", time.Now().Sub(jumpin))
		ShowIconWithPath(path)
		//log.Println(log.DebugLog, "Show vol:", vol, path, time.Now().Sub(jumpin))
	} else {
		log.Println(log.ErrorLog, "Get res fail:", resId)
	}
}

func ShowIconWithPath(path string) {
	//log.Println(log.DebugLog, "Show icon path:", path)
	//jumpin := time.Now()
	if err, totalFrame, delay, oneImageSize, picData := utils.DecodeImage(path); err == nil {
		//decodetime := time.Now()
		PlaySimpleAnimation(uint8(0), uint8(totalFrame), uint16(oneImageSize), uint16(delay), picData)
		//showtime := time.Now()
		//log.Println(log.DebugLog, "Decode time:", decodetime.Sub(jumpin), " mcu show time:", showtime.Sub(decodetime))
	} else {
		log.Println(log.ErrorLog, "Decode image err:", err)
	}
}

func Show2Icon(pathleft string, pathright string) {
	//log.Println(log.DebugLog, "Show icon path:", pathleft, pathright)
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, "err:", err)
		}
	}()
	width := 9
	height := 9
	jumpin := time.Now()
	if err, totalFrame, delay, oneImageSize, leftpicData := utils.DecodeImage(pathleft); err == nil {
		if err, _, _, _, rightpicData := utils.DecodeImage(pathright); err == nil {
			for y := 0; y < height; y++ {
				for x := 0; x < width; x++ {
					rightpicData[x+y*width] += leftpicData[x+y*width]
				}
			}
			decodetime := time.Now()
			PlaySimpleAnimation(uint8(0), uint8(totalFrame), uint16(oneImageSize), uint16(delay), rightpicData)
			showtime := time.Now()
			log.Println(log.DebugLog, "Decode time:", decodetime.Sub(jumpin), " mcu show time:", showtime.Sub(decodetime))
		} else {
			log.Println(log.DebugLog, "Decode right pic err:", err)
		}
	} else {
		log.Println(log.ErrorLog, "Decode image err:", err)
	}
}
