package globals

import (
	"time"
	"strconv"
	"strings"
)

const (
	YunxinServerAccount = "000000"
)

type VboxInfo struct {
	Id         int64  `json:"id"`
	Uuid       string `json:"uuid"`
	Deviceid   string `json:"deviceid"`
	Mac        string `json:"mac"`
	Secret     string `json:"secret"`
	DeviceName string `json:"device_name"`
	YxToken    string `json:"yx_token"`
	CreateTime int64  `json:"create_time"`
	Status     int    `json:"status"`
}

type CloudHistoryStautus int

const (
	CloudHistoryStautus_Save   CloudHistoryStautus = 1
	CloudHistoryStautus_UnSave CloudHistoryStautus = 0
)

type OffLineStatus int

const (
	OffLineStatus_Online  OffLineStatus = 0
	OffLineStatus_Offline OffLineStatus = 1
)

type MillTimeStamp time.Time

func (t MillTimeStamp) MarshalJSON() ([]byte, error) {
	return []byte(strconv.FormatInt(time.Time(t).UnixNano()/1e6, 10)), nil
}

func (t *MillTimeStamp) UnmarshalJSON(s []byte) (err error) {
	r := strings.Replace(string(s), `"`, ``, -1)

	q, err := strconv.ParseInt(r, 10, 64)
	if err != nil {
		return err
	}
	*(*time.Time)(t) = time.Unix(q/1000, q%1000*1e6)
	return
}

type YunxinAPPMsg struct {
	ResCode int `json:"resCode"`
	Feature int `json:"feature"`
	Content struct {
		ToType        int                 `json:"to_type"`
		TimeStampM    MillTimeStamp       `json:"time"`
		MsgType       int                 `json:"msg_type"`
		MsgBody       string              `json:"msg_body"`
		CloudHistory  CloudHistoryStautus `json:"cloud_history"`
		OfflineStatus OffLineStatus       `json:"offline_msg"`
	} `json:"content"`
}

type YunxinBasePack struct {
	Cmd       Cmd   `json:"cmd"`
	Timestamp int64 `json:"ts"`
}

type YunxinWifiParing struct {
	YunxinBasePack
	VboxInfo
}

type YunxinSysmsgContent struct {
	MsgId       uint64 `json:"msg_id"`
	ToAccount   string `json:"to_account"`
	MsgTime     uint64 `json:"msg_time"`
	MsgType     int    `json:"msg_type"`
	Attach      string `json:"attach"`
	FromAccount string `json:"from_account"`
}
type YunxinSysmsg struct {
	Rescode int                 `json:"rescode"`
	Content YunxinSysmsgContent `json:"content"`
}

type YunxinServerCmd struct {
	CmdId  YunxinServerCmdId `json:"cmd"`
	MsgId  string            `json:"msgid"`
	Ts     int64             `json:"ts"`
	Detail string            `json:"detail"`
}

type YunxinStatus struct {
	IsLogin uint8 `json:"IsLogin"`
}

type YunxinServerCmdId int

const (
	YxSCmd_Upgrade             YunxinServerCmdId = 8001 + iota //exec vol=0
	YxSCmd_ReBindUserid        YunxinServerCmdId = 8002        //exec vol=0
	YxSCmd_YinYueAccountChange YunxinServerCmdId = 8003
	YxSCmd_RebindNotice        YunxinServerCmdId = 8004
	YxSCmd_ChangeAccountMode   YunxinServerCmdId = 8005
	YxSCmd_Top3Rcmd            YunxinServerCmdId = 8007
	YxScmd_Tts_Delay           YunxinServerCmdId = 8008
	YxScmd_Res_Refresh         YunxinServerCmdId = 8010 //exec vol=0
	YxSCmd_HeartListChange     YunxinServerCmdId = 8101
	YxSCmd_HeartSongChange     YunxinServerCmdId = 8103
	YxSCmd_AlarmChange         YunxinServerCmdId = 8201 //exec vol=0
	YxSCmd_RemindChange        YunxinServerCmdId = 8202 //exec vol=0
	YxSCmd_SceneAlarmChange    YunxinServerCmdId = 8203 //exec vol=0
	YxSCmd_UploadLog           YunxinServerCmdId = 8300 //exec vol=0
	YxSCmd_RefreshCfglist      YunxinServerCmdId = 8302
	YxSCmd_IotWifiParingResult YunxinServerCmdId = 8303
	YxSCmd_InBtOnWifiOff       YunxinServerCmdId = 8304

	YxSCmd_MscTest YunxinServerCmdId = 77777 //exec vol=0
)

const (
	Type_Create_Session int = 1 + iota
	Type_Not_Create_Session
)
