package controller

/*
#include <uvdbus/smart_sound.h>
*/
import "C"

import (
	"netease_control_center/globals"
	. "netease_control_center/globals"
	"netease_control_center/interfaces/http"
	"netease_control_center/interfaces/log"
	"netease_control_center/interfaces/schedule"
	"netease_control_center/interfaces/vui"
	"netease_control_center/interfaces/vui/cvui"
	"netease_control_center/modules/player"
	"netease_control_center/modules/storage"
	"netease_control_center/utils/customization"
	"time"
)

func SyncAfterNetworkConnect() {
	log.Println(log.DebugLog, "Sync after network reconnect!")
	//Here is ota success! tell server!
	if IsAfterOtaFirstBoot == true {
		IsAfterOtaFirstBoot = false
		http.InformMsgToServer(http.Type_Inform_Ota, map[string]interface{}{"version": *GitTag, "dateTime": time.Now().Unix() * 1000, "hardwareVersion": *Hv})
	}
	go customization.DoCheck(10)
	SendHolidayinfo(uint32(time.Now().Local().Year() - 1900))
	DbusRefleshAlarmClocks()
	DbusRefleshReminds()
	UpdateCronTime()
	DbusRefleshSceneAlarm()
	OTACheckNewVersion(false, false)
	CheckServerBindUser()
	cvui.UpdateResFromServer()
	http.UpdateCfgList()
	customization.AfterInternetConnectCheckOem()
	btOnWifiOffCheckFlag()
}

func AccountChange() {
	log.Println(log.DebugLog, "Account change!")
	player.InitPlayer()
	schedule.SendSCmd(schedule.SCMD_MusicPlayer_Stop)
	SendUUIDToVE()

	SyncAfterNetworkConnect()
	schedule.InitPlayerFramework()
	time.Sleep(50 * time.Millisecond)
	VolSet(globals.DefaultVol.GetCfgListVui())

	globals.IconLedSwitch.CfgKeyValRestore()
	globals.IconLedMusicPlayMode.CfgKeyValRestore()
	globals.SaveCfgList2Sql()

}

func AccountUnbind() {
	log.Println(log.DebugLog, "Account unbind!")
	player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, vui.SoundNotice_Slianwangshibai001.GetPath())

	player.InitPlayer()
	schedule.SendSCmd(schedule.SCMD_MusicPlayer_Stop)
	SendUUIDToVE()

	SyncAfterNetworkConnect()
	log.Println(log.DebugLog, "Begin to exec wifi reset sh!")
	//nembd.ExecOutput("sh", "/etc/rftest/reset_wireless_normal.sh")

	//lupeng add 20180517
	BtOnline := globals.CurMusicSrcOnOffSt.BtOn

	DisableBle()
	DisableBT()
	SendWifiCmd(uint32(C.CMD_WIFI_UNBIND_ACCOUNT))
	schedule.InitPlayerFramework()
	time.Sleep(50 * time.Millisecond)
	VolSet(globals.DefaultVol.GetCfgListVui())

	globals.IconLedSwitch.CfgKeyValRestore()
	globals.IconLedMusicPlayMode.CfgKeyValRestore()
	globals.SaveCfgList2Sql()

	//lupeng add 20180517
	if BtOnline == true {
		log.Println(log.DebugLog, "Make bt online!!")
		player.OpenBtOfBt()
	}

	storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, "")
	storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_UUID, "")
	storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_AESKEY, "")
	storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_YXTOKEN, "")

	log.Println(log.DebugLog, "2s after reboot because of unbind!")
	time.Sleep(2 * time.Second)
	CSystemSafeReboot()
}
