package dadaapi

import "git.rosy.net.cn/baseapi/utils"

const (
	MessageTypeRidderCancel = 1
)

type RidderCancelNotify struct {
	OrderID      string `json:"orderId"`
	DadaOrderID  int64  `json:"dadaOrderId"`
	CancelReason string `json:"cancelReason"`
}

type RidderCancelConfirm struct {
	OrderID     string `json:"orderId"`
	DadaOrderID int64  `json:"dadaOrderId,omitempty"`
	IsConfirm   int    `json:"isConfirm"`
}

type ConfirmMsg struct {
	MessageType int    `json:"messageType"`
	MessageBody string `json:"messageBody"`
}

type NotifyMsg struct {
	MessageType int    `json:"messageType"`
	MessageBody string `json:"messageBody"`
	CreateTime  int64  `json:"createTime"`

	MessageObj *RidderCancelNotify `json:"messageObj"`
}

type NotifyResponse struct {
	Status string `json:"status"`
}

var (
	SuccessNotifyResponse = &NotifyResponse{Status: "ok"}
	FailedNotifyResponse  = &NotifyResponse{Status: "fail"}
)

func (a *API) GetNotifyMsg(data []byte) (msg *NotifyMsg, notifyResponse *NotifyResponse) {
	err := utils.UnmarshalUseNumber(data, &msg)
	if err == nil && msg.MessageBody != "" {
		err = utils.UnmarshalUseNumber([]byte(msg.MessageBody), &msg.MessageObj)
	}
	if err != nil {
		notifyResponse = FailedNotifyResponse
	}
	return msg, notifyResponse
}

// 消息确认
// http://newopen.imdada.cn/#/development/file/merchantConfirm?_k=gqkja3
func (a *API) ConfirmRidderCancel(orderID string, dadaOrderID int64, isConfirm bool) (err error) {
	_, err = a.AccessAPI("api/message/confirm", utils.Struct2FlatMap(&ConfirmMsg{
		MessageType: MessageTypeRidderCancel,
		MessageBody: string(utils.MustMarshal(&RidderCancelConfirm{
			OrderID:     orderID,
			DadaOrderID: dadaOrderID,
			IsConfirm:   utils.Bool2Int(isConfirm),
		})),
	}))
	return err
}
