package klink

import "github.com/gogf/gf/encoding/gjson"

type PacketAction string

const (
	DevSendAction        PacketAction = "devSend"        // 设备上报数据
	BatchDevSendAction   PacketAction = "batchDevSend"   // 网关批量上报设备数据
	CloudSendAction      PacketAction = "cloudSend"      // 云端给设备下发指令
	ReportFirmwareAction PacketAction = "reportFirmware" // 设备上报固件信息
	DevLoginAction       PacketAction = "devLogin"       // 子设备上线
	DevLogoutAction      PacketAction = "devLogout"      // 子设备下线
	DevNetConfigAction   PacketAction = "devNetConfig"   // 设备配网信息
	DevUpgradeAction     PacketAction = "devUpgrade"     // 设备升级
)

// DevLogin 子设备上线
type DevLogin struct {
	Action      string `json:"action"`
	MsgId       int64  `json:"msgId"`
	DeviceCode  string `json:"deviceCode"`
	Pk          string `json:"pk"`
	SubDeviceId string `json:"subDeviceId"`
	Timestamp   int64  `json:"timestamp"`
}

func (d *DevLogin) Marshal() ([]byte, error) {
	return gjson.New(d).ToJson()
}

func (d *DevLogin) UnMarshal(bytes []byte) error {
	j, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return err
	}
	return j.Struct(d)
}

// DevLogout 子设备下线
type DevLogout struct {
	Action      string `json:"action"`
	Pk          string `json:"pk"`
	MsgId       int64  `json:"msgId"`
	DeviceCode  string `json:"deviceCode"`
	SubDeviceId string `json:"subDeviceId"`
	Timestamp   int64  `json:"timestamp"`
}

func (d *DevLogout) Marshal() ([]byte, error) {
	return gjson.New(d).ToJson()
}

func (d *DevLogout) UnMarshal(bytes []byte) error {
	j, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return err
	}
	return j.Struct(d)
}

// DevSend 设备上报数据帧
type DevSend struct {
	Action      string       `json:"action"`
	MsgId       int64        `json:"msgId"`
	DeviceCode  string       `json:"deviceCode"`
	SubDeviceId string       `json:"subDeviceId"`
	Data        *DevSendData `json:"data"`
	Timestamp   int64        `json:"timestamp"`
	Version     string       `json:"version"`
}

func (d *DevSend) Marshal() ([]byte, error) {
	return gjson.New(d).ToJson()
}

func (d *DevSend) UnMarshal(bytes []byte) error {
	j, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return err
	}
	return j.Struct(d)
}

type DevSendData struct {
	Cmd    string      `json:"cmd"`
	Params *gjson.Json `json:"params"`
}

type CloudSendData struct {
	Cmd    string                 `json:"cmd"`
	Params map[string]interface{} `json:"params"`
}

type CloudSend struct {
	Action      string         `json:"action"`
	MsgId       int64          `json:"msgId"`
	DeviceCode  string         `json:"deviceCode"`
	Timestamp   int64          `json:"timestamp"`
	Data        *CloudSendData `json:"data"`
	SubDeviceId string         `json:"subDeviceId"`
}

func (c *CloudSend) Marshal() ([]byte, error) {
	return gjson.New(c).ToJson()
}

func (c *CloudSend) UnMarshal(bytes []byte) error {
	j, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return err
	}
	return j.Struct(c)
}

// DevUpgrade 设备升级
type DevUpgrade struct {
	Action      string `json:"action"`
	MsgId       int    `json:"msgId"`
	DeviceCode  string `json:"deviceCode"`
	SubDeviceId string `json:"subDeviceId"`
	Timestamp   int64  `json:"timestamp"`
	Url         string `json:"url"`
	Md5         string `json:"md5"`
	Type        string `json:"type"`
	Version     string `json:"version"`
}

func (c *DevUpgrade) Marshal() ([]byte, error) {
	return gjson.New(c).ToJson()
}

func (c *DevUpgrade) UnMarshal(bytes []byte) error {
	j, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return err
	}
	return j.Struct(c)
}
