package v1

import (
	"bytes"
	"encoding/json"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
	"openapi/pkg/application/push/service"
	protocol "openapi/pkg/domain"
	"openapi/pkg/port/beego/controllers"
)

type PushController struct {
	controllers.BaseController
}

//推送信息 PushInfo
// @router /pushInfo [post]
func (this *PushController) PushInfo() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.PushInfoRequest
	decoder := json.NewDecoder(bytes.NewBuffer(this.ByteBody))
	decoder.UseNumber()
	if err := decoder.Decode(&request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(push.Notification(header, request))
}

//原生推送信息 PushInfoOriginal
// @router /pushInfoOriginal [post]
func (this *PushController) PushInfoOriginal() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.PushInfoOriginalRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(push.NotificationOriginal(header, request))
}

//UpdateDevice
//@router /updateDevice [post]
func (this *PushController) UpdateDevice() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.UpdateDeviceRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if request.ProjectKey == "" {
		request.ProjectKey = "ability" //默认能力展示项目
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(push.UpdateDevice(header, request))
}