package domain

import (
	"encoding/json"
	"openapi/pkg/infrastructure/log"
)

/*PushInfo 推送信息*/
type PushInfoOriginalRequest struct {
	Type         int      `json:"msgType"`
	ClientIdList []string `json:"clientId"`
	AppKey       string   `json:"appKey" valid:"Required"`
	Secret       string   `json:"secret" valid:"Required"`
	AppId        string   `json:"appId" valid:"Required"`

	Title   string                 `json:"title" valid:"Required"`
	Content string                 `json:"content" valid:"Required"`
	Ext     map[string]interface{} `json:"ext"` //key->transData:透传数据
}

func (req *PushInfoOriginalRequest) SetExt(key string, value interface{}, force bool) {
	if force {
		req.Ext[key] = value
		return
	}
	if _, ok := req.Ext[key]; ok {
		return
	}
	req.Ext[key] = value
}

type PushInfoResponse struct {
	ExtData map[string]interface{} `json:"-"`
}

/*PushInfo 推送信息*/
type PushInfoRequest struct {
	Type        int                    `json:"msgType"`
	Receivers   []int64                `json:"receivers"` //接受用户id列表
	ProjectKey  string                 `json:"project"`   //ability
	ProjectKeys []string               `json:"projects"`  //同时推送多个项目的时候 eg:例如能力展示有一些发布了独立的企业版本,需要能同时推送到达
	Title       string                 `json:"title" valid:"Required"`
	Content     string                 `json:"content" valid:"Required"`
	Ext         map[string]interface{} `json:"ext"` //key->transData:透传数据

	ActualReceivers []int64 `json:"-"` //实际发送的人员id列表
}

/*UpdateDevice 更新设备*/
type UpdateDeviceRequest struct {
	ProjectMasterKey string `json:"projectKey"`             //项目主编号
	Project          string `json:"-"`                      //项目主编号
	Muid             int64  `json:"muid" valid:"Required;"` //企业平台中的用户 UID
	ClientId         string `json:"clientId" valid:"Required"`
	DeviceToken      string `json:"deviceToken"`
	Phone            string `json:"phone"`
}
type UpdateDeviceResponse struct {
}

//设备信息
type Device struct {
	Uid         int64
	ClientId    string
	DeviceToken string
	IsActive    int
}

//应用信息
type AppInfo struct {
	Id              int
	AppKey          string
	AppMasterSecret string
	AppId           string
	ProjectId       int //项目编号
	ExtInfo         string
}

type ExtInfo struct {
	Intent  string `json:"intent"`
	Sound   string `json:"sound,omitempty"`
	HWSound string `json:"sound_hw,omitempty"`
	XMSound string `json:"sound_xm,omitempty"`
}

func (t *AppInfo) GetExtInfo() (*ExtInfo, bool) {
	extInfo := &ExtInfo{}
	if len(t.ExtInfo) == 0 {
		return nil, false
	}
	if err := json.Unmarshal([]byte(t.ExtInfo), extInfo); err != nil {
		log.Error(err.Error())
		return nil, false
	}
	return extInfo, true
}