package protocol

import (
	"time"
)

type PTZMoveInfo struct {
	ProfileToken string `json:"profiletoken,omitempty"`
	Direction    uint8  `json:"direction"` //0:停止移动;1:向左边;2:向右边;3:向上;4:向下;5:缩小;6:放大
	Speed        uint8  `json:"speed"`     //操控数据，0到255
}

type Pack_PTZMoveInfo struct {
	Header
	Data PTZMoveInfo `json:"data"`
}

func (o *Pack_PTZMoveInfo) EnCode(id, gid, profiletoken string, seq uint64, direction, speed uint8) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.ProfileToken = profiletoken
	o.Data.Direction = direction
	o.Data.Speed = speed
	return json.MarshalToString(o)
}

func (o *Pack_PTZMoveInfo) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type PTZCommonInfo struct {
	ProfileToken string `json:"profiletoken,omitempty"`
	PresetToken  string `json:"presettoken,omitempty"`
	PresetName   string `json:"presetname,omitempty"`
	Flag         int    `json:"flag"` //0:获取所有有效的预置点，1:设置预置点，2:删除预置点，3:跳转到预置点，4:设置home点，5:跳转到home点，6:获取所有预置点
}

type Pack_PTZCommonInfo struct {
	Header
	Data PTZCommonInfo `json:"data"`
}

func (o *Pack_PTZCommonInfo) EnCode(id, gid, profiletoken, presettoken, presetname string, seq uint64, flag int) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.ProfileToken = profiletoken
	o.Data.PresetToken = presettoken
	o.Data.PresetName = presetname
	o.Data.Flag = flag
	return json.MarshalToString(o)
}

func (o *Pack_PTZCommonInfo) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type MediaCommonInfo struct {
	ProfileToken string `json:"profiletoken,omitempty"`
	Flag         int    `json:"flag"` //0：抓图；1：推流; 2:停止推流;3:录mp4
}

type Pack_MediaCommonInfo struct {
	Header
	Data MediaCommonInfo `json:"data"`
}

func (o *Pack_MediaCommonInfo) EnCode(id, gid, profiletoken string, seq uint64, flag int) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.ProfileToken = profiletoken
	o.Data.Flag = flag
	return json.MarshalToString(o)
}

func (o *Pack_MediaCommonInfo) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

//ONVIF设备告警
type OnvifAlarmInfo struct {
	AlarmTopic string            `json:"alarmtopic"`       //告警主题
	Time       string            `json:"time"`             //时间
	Source     map[string]string `json:"source,omitempty"` //告警来源
	Key        map[string]string `json:"key,omitempty"`    //告警key
	Data       map[string]string `json:"data,omitempty"`   //告警数据
}

type OnvifAlarm struct {
	Alarm OnvifAlarmInfo `json:"alarm"`
}

type Pack_OnvifAlarm struct {
	Header
	Data OnvifAlarm `json:"data"`
}

func (o *Pack_OnvifAlarm) EnCode(id, gid string, seq uint64, ca *OnvifAlarmInfo) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Alarm = *ca
	return json.MarshalToString(o)
}

func (o *Pack_OnvifAlarm) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

//ONVIF设备基本信息上报
type Pack_OnvifDev struct {
	Header
	Data []OnvifDev `json:"data"`
}

func (o *Pack_OnvifDev) EnCode(gid string, seq uint64, onvifdevs []OnvifDev) (string, error) {
	o.SetHeaderData(gid, gid, seq)
	o.Data = onvifdevs
	return json.MarshalToString(o)
}

func (o *Pack_OnvifDev) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

//获取所有预置位信息
type PresetInfo struct {
	Token string  `json:"token"`
	Name  string  `json:"name"`
	X     float32 `json:"x"`
	Y     float32 `json:"y"`
	Z     float32 `json:"z"`
}

type PresetInfos struct {
	StateError
	Flag    int          `json:"flag"`
	Presets []PresetInfo `json:"presets"`
}

type Pack_PresetInfo struct {
	Header
	Data PresetInfos `json:"data"`
}

func (o *Pack_PresetInfo) EnCode(id, gid string, seq uint64, err error, flag int, presets []PresetInfo) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.SetStateErrorData(err)
	o.Data.Flag = flag
	o.Data.Presets = presets
	return json.MarshalToString(o)
}

func (o *Pack_PresetInfo) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

//设置预置点
type IPCSetPresetACK struct {
	StateError
	Token string  `json:"presettoken,omitempty"` //预置点token
	Name  string  `json:"presetname,omitempty"`  //预置点token
	X     float32 `json:"x"`                     //预置点x
	Y     float32 `json:"y"`                     //预置点y
	Z     float32 `json:"z"`                     //预置点z
	File  string  `json:"file,omitempty"`        //预置点快照图片文件名
}

type Pack_IPCSetPresetACK struct {
	Header
	Data IPCSetPresetACK `json:"data"`
}

func (o *Pack_IPCSetPresetACK) EnCode(id, gid, presettoken, name, file string, x, y, z float32, seq uint64, err error) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.SetStateErrorData(err)
	o.Data.Token = presettoken
	o.Data.Name = name
	o.Data.File = file
	o.Data.X = x
	o.Data.Y = y
	o.Data.Z = z
	return json.MarshalToString(o)
}

func (o *Pack_IPCSetPresetACK) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type IPCCommonACK struct {
	StateError
	Token string `json:"presettoken,omitempty"` //预置点token
}

type Pack_IPCCommonACK struct {
	Header
	Data IPCCommonACK `json:"data"`
}

func (o *Pack_IPCCommonACK) EnCode(id, gid, presettoken string, seq uint64, err error) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.SetStateErrorData(err)
	o.Data.Token = presettoken
	return json.MarshalToString(o)
}

func (o *Pack_IPCCommonACK) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type IPCCtrlComm struct {
	Flag int `json:"flag"` //0:重启，1:同步时间
}
type Pack_IPCCtrlComm struct {
	Header
	Data IPCCtrlComm `json:"data"`
}

func (o *Pack_IPCCtrlComm) EnCode(id, gid string, seq uint64, flag int) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Flag = flag
	return json.MarshalToString(o)
}

func (o *Pack_IPCCtrlComm) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type IPCState struct {
	Time  string `json:"time"`
	State uint8  `json:"state"` //0 在线，1离线
}

type Pack_IPCState struct {
	Header
	Data IPCState `json:"data"`
}

func (o *Pack_IPCState) EnCode(id, gid string, seq uint64, changetime time.Time, state uint8) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Time = changetime.Format("2006-01-02 15:04:05")
	o.Data.State = state
	return json.MarshalToString(o)
}

func (o *Pack_IPCState) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}
