package getui

import (
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
	"openapi/internal/push"
)

type Template struct {
	ClientId  string   `json:"cid"`
	RequestId string   `json:"requestid"`
	Message   *Message `json:"message"`
}

//1.消息模板
type NotificationTemplate struct {
	*Template
	Notification *Notification `json:"notification"`
}

//2.透传模板
type TransmissionTemplate struct{}

type Notification struct {
	Style *Style `json:"style"`
	*Transmission
}
type Message struct {
	AppKey    string `json:"appkey"`
	IsOffline bool   `json:"is_offline"`
	MsgType   string `json:"msgtype"`
}
type Transmission struct {
	TransmissionType    bool   `json:"transmission_type"`
	TransmissionContent string `json:"transmission_content,omitempty"`
	DurationBegin       string `json:"duration_begin,omitempty"`
	DurationEnd         string `json:"duration_end,omitempty"`
}

func (o *Transmission) SetTransmissionType(t bool) {
	o.TransmissionType = t
}
func (o *Transmission) SetTransmissionContent(s string) {
	o.TransmissionContent = s
}
func (o *Transmission) SetDuration(begin, end string) {
	o.DurationBegin = begin
	o.DurationEnd = end
}

//1.新建单推
func NewNotificationTemplate(options *push.Options) *NotificationTemplate {
	return &NotificationTemplate{
		Template: NewTemplate(options),
		Notification: &Notification{
			Style:        (&Style{}).SetStyle0(options),
			Transmission: NewTransmission(options),
		},
	}
}
func NewTemplate(options *push.Options) *Template {
	return &Template{
		Message:   NewMessage(options),
		ClientId:  options.ClientId,
		RequestId: genRequestId(),
	}
}
func NewTransmission(options *push.Options) *Transmission {
	t := &Transmission{}
	if len(options.TransmissionContent) == 0 {
		t.SetTransmissionType(false)
		return t
	}
	t.SetTransmissionType(true)
	t.SetTransmissionContent(options.TransmissionContent)
	return t
}
func NewMessage(options *push.Options) *Message {
	return &Message{
		AppKey:    options.AppKey,
		IsOffline: true,
		MsgType:   resolveMsgType(options.MsgType),
	}
}
func resolveMsgType(msgType int) string {
	/*
		消息应用类型,
		可选项:notification、link、notypopload、startactivity, transmission
	*/
	switch msgType {
	case 1:
		return "notification"

	}
	return "notification"
}
func genRequestId() string {
	return uid.NewV1().StringNoDash()
}

//样式 0:系统样式 1:个推样式 4:纯图样式 6:展开通知样式
type Style struct {
	Type  int    `json:"type"`           //样式类型
	Text  string `json:"text"`           //通知内容
	Title string `json:"title"`          //通知标题
	Logo  string `json:"logo,omitempty"` //通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png”
	//IsRing bool `json:"is_ring"`  //收到通知是否响铃:true响铃,false不响铃。默认响铃
	//IsVibrate bool `json:"is_vibrate"`  //收到通知是否振动:true振动,false不振动。默认振动
}

func (s *Style) SetStyle0(options *push.Options) *Style {
	s.Type = 0
	s.Title = options.Title
	s.Text = options.Content
	s.Logo = "push.png" //TODO:设置Logo地址
	return s
}

//认证请求/应答
type AuthSignRequest struct {
	Sign      string `json:"sign"`
	Timestamp string `json:"timestamp"`
	AppKey    string `json:"appkey"`
}
type AuthSignResponse struct {
	*Result
	AuthToken string `json:"auth_token"`
}

type Result struct {
	Result string `json:"result"`
	TaskId string `json:"taskid"`
	Status string `json:"status"`
	Desc   string `json:"desc"`
}