package controller

/*
#include <uvdbus/smart_sound.h>
*/
import "C"
import (
	"encoding/json"
	"errors"
	"fmt"
	"netease_control_center/globals"
	"netease_control_center/interfaces"
	. "netease_control_center/interfaces/http"
	"netease_control_center/interfaces/log"
	"netease_control_center/modules/storage"
	"netease_control_center/utils/customization"
)

const (
//tmpuuid = "1-956d1bb7f32d9b79"
//tmpuuid = "1-990e165da2e8a97b"

)

var (
	Err_binduser_account_limit = errors.New("binduser_account_limit")
)

func HttpGetUUID(bindUser string) (err error, errcode int) {
	var req HttpReqFirstRun
	var res HttpRspFirstRun

	req.Mac = "thisismacaddr"
	req.BindUser = bindUser

	code, detail, err := HttpPostAuto(req, &res)
	log.Println(log.DebugLog, "wzj,http inituuid:", code, detail, res)
	if err == nil && code == 1000 {
		globals.BindUser = bindUser
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, bindUser)
		globals.Uuid = res.VboxId
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_UUID, res.VboxId)
		globals.AesKey = res.AesKey
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_AESKEY, res.AesKey)
		globals.YxToken = res.YxToken
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_YXTOKEN, res.YxToken)
		return nil, code
	}

	if err != nil {
		return err, code
	}

	if code != int(globals.ERR_SUCCESS) {
		return errors.New(detail), code
	}

	return errors.New("Unknow error"), code
}

func HttpRebindUUID(bindUser string) (err error, errcode int) {
	var res HttpRspRebindUser
	var req HttpReqRebindUser
	req.AppUuid = bindUser

	code, detail, err := HttpPostAuto(req, &res)
	log.Println(log.DebugLog, code, detail, err)
	log.Printf(log.DebugLog, "Rebind get info:%+v\n", res)
	if err == nil && code == int(globals.ERR_SUCCESS) {
		globals.BindUser = bindUser
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, bindUser)
		globals.Uuid = res.VboxId
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_UUID, res.VboxId)
		globals.AesKey = res.AesKey
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_AESKEY, res.AesKey)
		globals.YxToken = res.YxToken
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_YXTOKEN, res.YxToken)

		return nil, code
	}

	if err != nil {
		return err, code
	}

	if code != int(globals.ERR_SUCCESS) {
		return errors.New(detail), code
	}

	return errors.New("Unknow error"), code
}

func FinalInitUUID(newBoard bool, bindUser string) (err error, errcode int) {
	var req HttpReqInit
	var res HttpRspInit

	req.Init(newBoard, bindUser, globals.CPU_ID)

	code, detail, err := HttpPostAuto(req, &res)
	log.Println(log.DebugLog, "Finally, http inituuid:", code, detail, res)
	if err == nil && code == 1000 {
		globals.BindUser = bindUser
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, bindUser)
		globals.Uuid = res.Vboxid
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_UUID, res.Vboxid)
		globals.AesKey = res.AesKey
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_AESKEY, res.AesKey)
		globals.YxToken = res.YxToken
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_YXTOKEN, res.YxToken)
		globals.SongUrl = res.SongUrl
		storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_OEM_MANUFACTURE, res.Channel)
		globals.OemManufature = res.Channel
		if len(res.ChannelInfo) > 0 {
			storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_TELECOM_CTEI, res.ChannelInfo)
			globals.CTEI = res.ChannelInfo
		}
		go customization.DoCheck(10)

		return nil, code
	}

	if err != nil {
		return err, code
	}

	if code != int(globals.ERR_SUCCESS) {
		return errors.New(detail), code
	}

	return errors.New("Unknow error"), code
}

func InitUUID(bindUser string) (err error, errcode int) {
	uuid, bduser, yxtoken, aeskey := GetSqlUUIDS()
	uuidlen, bduserlen, yxtokenlen, aeskeylen := len(uuid), len(bduser), len(yxtoken), len(aeskey)

	log.Println(log.DebugLog, "uuid: ", uuid, " len:", uuidlen)
	log.Println(log.DebugLog, "binduser: ", bduser, " len:", bduserlen)
	log.Println(log.DebugLog, "yxtoken: ", yxtoken, " len:", yxtokenlen)
	log.Println(log.DebugLog, "aeskey: ", aeskey, " len:", aeskeylen)
	log.Println(log.DebugLog, "Will bind user:", bindUser)
	//if uuidlen > 0 && bduserlen > 0 && yxtokenlen > 0 && aeskeylen > 0 && bindUser == bduser {
	//	globals.Uuid = uuid
	//	globals.BindUser = bduser
	//	globals.YxToken = yxtoken
	//	globals.AesKey = aeskey
	//	return nil
	//} else
	log.Println(log.DebugLog, "Here do not check user id equal, call bind or rebind")
	if uuidlen > 0 && yxtokenlen > 0 && aeskeylen > 0 {
		//Rebind
		globals.Uuid = uuid
		globals.BindUser = bduser
		globals.YxToken = yxtoken
		globals.AesKey = aeskey

		return HttpRebindUUID(bindUser)
	} else {
		//Init bind
		return HttpGetUUID(bindUser)
	}
}

func GetSqlUUIDS() (uuid, bduser, yxtoken, aeskey string) {
	storage.Config_get(globals.MODULE_CONTROLLER, globals.SQL_KEY_UUID, &uuid)
	storage.Config_get(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, &bduser)
	storage.Config_get(globals.MODULE_CONTROLLER, globals.SQL_KEY_YXTOKEN, &yxtoken)
	storage.Config_get(globals.MODULE_CONTROLLER, globals.SQL_KEY_AESKEY, &aeskey)
	return
}

func ShowUUID() {
	fmt.Println("UUID:", globals.Uuid, "binduser: ", globals.BindUser, "aes: ", globals.AesKey, "yxtoken: ", globals.YxToken)
}

func GetUUIDS() (uuid, binduser, aeskey, yxtoken string, err error) {
	return globals.Uuid, globals.BindUser, globals.AesKey, globals.YxToken, err
}

func SendUUIDToVE() {
	if len(globals.Uuid) == 0 || len(globals.YxToken) == 0 {
		return
	}

	var tmp globals.ControllerInitArgs
	tmp.Uuid = globals.Uuid
	tmp.BindUser = globals.BindUser
	tmp.YxToken = globals.YxToken
	tmp.AesKey = globals.AesKey
	data, err := json.Marshal(tmp)
	if err == nil {
		interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_VOICEENGINE, uint32(C.CMD_CONTROLLER_RSPMSG_INITARGS), &data)
	} else {
		log.Printf(log.ErrorLog, "Code json error! %v\n", err)
	}
}

func CheckServerBindUser() {
	var req HttpReqQueryBindUser
	var rsp HttpRspQueryBindUser

	if code, detail, err := HttpPostAuto(req, &rsp); err == nil && globals.ERR_SUCCESS == globals.ErrCode(code) {
		log.Println(log.DebugLog, "Get server bind user:", rsp.Uuid, " , Local bind user:", globals.BindUser)
		if globals.BindUser != rsp.Uuid {
			log.Println(log.DebugLog, "Storage binduser!")
			globals.BindUser = rsp.Uuid
			storage.Config_set(globals.MODULE_CONTROLLER, globals.SQL_KEY_BINDUSER, rsp.Uuid)
			if len(rsp.Uuid) == 0 {
				AccountUnbind()
			} else {
				AccountChange()
			}
		}
	} else {
		log.Println(log.ErrorLog, "Get binduser error!", detail, err)
	}
}
