package http

import (
	. "netease_control_center/globals"
	"netease_control_center/interfaces/log"
)

type InformType int

const (
	Type_Inform_Ota         InformType = 103
	Type_Inform_TtsDelay    InformType = 108
	Type_Inform_TtsPush     InformType = 109 //上传成对出现的tts信息流
	Type_Inform_BtOnWifiOff InformType = 110
)
const (
	Type_Inform_Top3_Play InformType = 104 + iota
	Type_Inform_Scene_List_Invalid
	Type_Inform_Top3_Play_Sigle
)

func init() {
	RegisteApi(reqWeather{}, ApiContainer{Url: URL_WEATHER_INFO, ReqparamIsString: true, IsEncrypted: false})

	RegisteApi(reqCfgListPush{}, ApiContainer{Url: URL_CfgListPush_INFO, ReqparamIsString: true, IsEncrypted: false})

	RegisteApi(InfoForApp{}, ApiContainer{Url: URL_Update_Info_For_App, ReqparamIsString: true, IsEncrypted: false})
}

func GetWeatherInfo() (*WeatherResp, int, *string, error) {
	var req reqWeather
	var res WeatherResp
	var detail string
	var code int
	var err error
	code, detail, err = postInterface(req, &res)

	if int(ERR_SUCCESS) != code && "" == detail {
		return nil, code, &detail, Server_ERR_RET
	}
	if nil != err {
		log.Println(log.ErrorLog, err.Error())
		return nil, code, nil, err
	}
	return &res, code, nil, nil
}

func InformMsgToServer(informType InformType, data interface{}) error {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, err)
		}
	}()
	var res HttpRspInform
	httpInformReq := HttpReqInform{InformType: informType, Data: data}

	if code, _, err := HttpPostAuto(httpInformReq, &res); code == 1000 && err == nil {
		log.Println(log.DebugLog, "has inform server of data:", data, " rsp:", res)
		return err
	} else {
		log.Println(log.WarningLog, "inform server failed, data:", data, "code: ", code, " err:", err)
		return err
	}
}

func InformLocalMsgToServer(informType InformType, cmd int, content string, data string) {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, err)
		}
	}()
	var res HttpRspInform
	httpInformReq := HttpReqInform109{InformType: informType, Cmd: cmd, Content: content, Data: data}

	if code, _, err := HttpPostAuto(httpInformReq, &res); code == 1000 && err == nil {
		log.Println(log.DebugLog, "has Local inform server of data:", data, " rsp:", res)
	} else {
		log.Println(log.WarningLog, "Local inform server failed, data:", data, "code: ", code, " err:", err)
	}
}

func UpdateCfgList() (bool, error) {
	return UpdateCfgList2()
}

func UpdateCfgList2() (bool, error) {
	var req reqCfgList2 = reqCfgList2{Key: ""}
	var res RespCfgList2

	var detail string
	var code int
	var err error
	code, detail, err = postInterface(req, &res)
	log.Printf(log.DebugLog, "%+v\n", res)
	if int(ERR_SUCCESS) != code && "" == detail {
		return false, err
	}
	if nil != err {
		log.Println(log.ErrorLog, err.Error())
		return false, err
	}

	SetAndSaveCfgList(CfgListType(res))
	return true, nil
}

func PushCfgList(cfgList string) {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, err)
		}
	}()
	var res rspCfgListPush
	httpInformReq := reqCfgListPush{Value: cfgList}

	if code, _, err := HttpPostAuto(httpInformReq, &res); code == 1000 && err == nil {
		log.Println(log.DebugLog, "has inform server of data:", cfgList, " rsp:", res)
	} else {
		log.Println(log.WarningLog, "inform server failed, data:", cfgList, "code: ", code, " err:", err)
	}
}

//func Get(url string) (resp *http.Response, err error) {
//	return http.Get(url)
//}

func PushInfoToServerForApp(infoData InfoForApp) {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, err)
		}
	}()

	if infoData.ResponseId == "" {
		//resp id 为空，不需要上报
		log.Printf(log.WarningLog, "cancel update info, because resp id is empty: %+v\n", infoData)
		return
	}

	var res HttpRspInform

	if code, _, err := HttpPostAuto(infoData, &res); code == 1000 && err == nil {
		log.Printf(log.WarningLog, "Push info of data:%+v, rsp:%+v\n", infoData, res)
	} else {
		log.Printf(log.WarningLog, "Push info failed, data:%+v, code:%+v, err:%+v\n", infoData, code, err)
	}
}
