package ebaiapi

import (
	"errors"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
)

const (
	IMStoreStatusOnLine = "ONLINE"        // 门店im在线状态
	IMStoreStatusBusy   = "BUSY"          // 忙碌状态
	IMType              = "IM"            // 业务类型,消息默认IM
	IMTypeSendMsg       = "SEND_MESSAGE"  //
	IMTypeReadMsg       = "READ_MESSAGE"  //
	IMTypeGetMedia      = "GET_MEDIA_URL" //

	DefaultUrlExpireTime = 86400 //s

	//消息类型
	ContentTypeNormal = 1 //普通文本信息
	ContentTypeAt     = 8 //@ 消息

	//门店im状态
	ImStatusOpen  = 1  //开启
	ImStatusClose = -1 //关闭

	//10(用户)、20(骑手)、30(商家)、32（连锁账号登录）、95（系统）
	SenderTypeUser                = "10"
	SenderTypeRider               = "20"
	SenderTypeBusiness            = "30"
	SenderTypeChainedAccountLogin = "32"
	SenderTypeSystem              = "95"
)

// BusinessSendMsgReq im发送消息
type BusinessSendMsgReq struct {
	PlatformShopId string             `json:"platformShopId"` // 平台门店id
	BizType        string             `json:"bizType"`        // 业务类型，IM消息。默认值：IM
	SubBizType     string             `json:"subBizType"`     // 子业务类型，发送消息。默认值：SEND_MESSAGE
	PayLoad        BusinessMsgPayload `json:"payLoad"`
}

type BusinessMsgPayload struct {
	GroupId     string   `json:"groupId"`     // 会话ID
	MsgId       string   `json:"msgId"`       // 消息ID
	ReceiverIds []string `json:"receiverIds"` // 接收人列表
	Content     string   `json:"content"`     // 发送内容，格式：JSON  {"text":"msg"}
	ContentType string   `json:"contentType"` // 	内容类型，目前只支持文本消息。枚举值： 1-普通文本
}

// BusinessSendMsg 门店老板发送消息
func (a *API) BusinessSendMsg(param *BusinessSendMsgReq) error {
	result, err := a.AccessAPI("im.message.send", utils.Struct2MapByJson(param))
	if err != nil {
		return err
	}
	if result.ErrNo != 0 {
		globals.SugarLogger.Debugf("ebaiBusinessSendMsg err=%v", result.Error)
		return errors.New(result.Error)
	}

	return nil
}

// GetMediaUrl 获取多媒体文件url
func (a *API) GetMediaUrl(platformShopId, mediaID string) (url string, err error) {
	result, err := a.AccessAPI("im.get.media.url", map[string]interface{}{
		"platformShopId": platformShopId,
		"bizType":        IMType,
		"subBizType":     IMTypeGetMedia,
		"payload": map[string]interface{}{
			"mediaId":       mediaID,
			"urlExpireTime": DefaultUrlExpireTime,
		},
	})
	if err != nil {
		return "", err
	}
	if result.ErrNo != 0 {
		return "", errors.New(result.Error)
	}
	return result.Data.(map[string]interface{})["url"].(string), nil
}

// GetStoreIMStatus 获取门店的im状态 1：开启，-1：关闭
func (a *API) GetStoreIMStatus(platformShopId string) (string, error) {
	result, err := a.AccessAPI("im.getIMStatus", map[string]interface{}{"platformShopId": platformShopId})
	if err != nil {
		return "0", err
	}
	if result.ErrNo != 0 {
		return "0", errors.New(result.Error)
	}

	return result.Data.(map[string]interface{})["imStatus"].(string), nil
}

// UpdateIMStatus 更新店铺IM开关状态 1-开启,-1-关闭
func (a *API) UpdateIMStatus(platformShopId string, status int) error {
	result, err := a.AccessAPI("im.updateIMStatus", map[string]interface{}{"platformShopId": platformShopId, "status": status})
	if err != nil {
		return err
	}
	if result.ErrNo != 0 {
		return errors.New(result.Error)
	}

	return nil
}

// SettingStoreMsgRead 设置消息已读
//func (a *API) SettingStoreMsgRead(platformShopId string, msgId string) error {
//	result, err := a.AccessAPI("im.message.read", map[string]interface{}{
//		"platformShopId": platformShopId,
//		"bizType":        IMType,
//		"subBizType":     ReadIMType,
//		"payload":        map[string]string{"msgId": msgId},
//	})
//	if err != nil {
//		return err
//	}
//	if result.ErrNo != 0 {
//		return errors.New(result.Error)
//	}
//	return nil
//}

// GetImOnlineStatus 获取门店IM线上状态
//func (a *API) GetImOnlineStatus(platformShopId string) (int, error) {
//	result, err := a.AccessAPI("im.getIMOnlineStatus", map[string]interface{}{"platformShopId": platformShopId})
//	if err != nil {
//		return 0, err
//	}
//	if result.ErrNo != 0 {
//		return 0, errors.New(result.Error)
//	}
//
//	return result.Data.(map[string]interface{})["status"].(int), nil
//}

// SetImOnlineStatus 设置im线上状态 ONLINE - 在线 BUSY - 忙碌
//func (a *API) SetImOnlineStatus(platformShopId string, status string) error {
//	result, err := a.AccessAPI("im.updateIMOnlineStatus", map[string]interface{}{"platformShopId": platformShopId, "status": status})
//	if err != nil {
//		return err
//	}
//	if result.ErrNo != 0 {
//		return errors.New(result.Error)
//	}
//
//	return nil
//}
