package jdapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"io/ioutil"
	"net/http"
	"time"
)

const (
	ImCallbackRisMsg  = "chatRiskMsg"   // 风控消息
	ImCallbackUserMsg = "msChatMessage" // 用户消息
	ImCallbackReadMsg = "msgReadAck"    // 消息已读
)

//#region im信息推送相关

// ImStatusModify 开启全平台或者单门店im通知
func (a *API) ImStatusModify(stationId string, typePattern int, status bool) error {
	parameter := map[string]interface{}{
		"stationId":  stationId,
		"type":       typePattern,
		"openStatus": status,
	}
	_, err := a.AccessAPINoPage("ImMultiChatRpc/imStatusModify", parameter, nil, nil, genNoPageResultParser("code", "msg", "result", "0"))
	if err != nil {
		return err
	}
	return nil
}

// ImWaiterStatusModify 门店客服状态控制 0：下线；1：在线
func (a *API) ImWaiterStatusModify(clientType int, stationId string, presence int) error {
	parameter := map[string]interface{}{
		"stationId":  stationId,
		"clientType": clientType,
		"presence":   presence,
	}
	_, err := a.AccessAPINoPage("ImMultiChatRpc/waiterStatusModify", parameter, nil, nil, genNoPageResultParser("code", "msg", "data", "成功"))
	if err != nil {
		return err
	}
	return nil
}

// MessagePushService 给京东用户推送消息 https://scene-control-access-pro.pf.jd.com/#/flow-center(咚咚场景化消息接入平台)
func (a *API) MessagePushService(msg *ContextMsg) error {
	systemParam := map[string]interface{}{
		"aspid":       "P0000201",      // 可在“咚咚场景化消息接入平台 - 产品线 - 基础信息”中查看
		"accessid":    utils.GetUUID(), // 访问ID，可用于请求去重
		"timestamp":   time.Now().UnixNano(),
		"version":     "3.0",
		"accessToken": "af1aa6a267849b3f9f4caa19e1ad5564", // 咚咚场景化消息接入平台 - 产品线 - 基础信息
		"source":      msg.Body.Template.Source,
	}
	systemParam["jsonMsg"] = string(utils.MustMarshal(msg))

	_, err := a.AccessAPINoPage("MessagePushService/push2", systemParam, nil, nil, genNoPageResultParser("code", "msg", "data", "230001"))
	if err != nil {
		return err
	}
	return nil
}

//#endregion

//#region im回到信息相关

// ImChatRiskMsg IM(风控,用户消息,消息已读)消息推送
func ImChatRiskMsg(request *http.Request) (back []byte, url string, err error) {
	msgURL := getMsgURLFromRequest(request)
	data, err := ioutil.ReadAll(request.Body)
	globals.SugarLogger.Debugf("------ImChatRiskMsg:= %s", string(data))
	if err != nil {
		return nil, "", err
	}
	values, err := utils.HTTPBody2Values(data, false)
	valuesMap := utils.URLValues2Map(values)
	return utils.MustMarshal(valuesMap), msgURL, err
}

func ImMsgChange(msg *JdParamJsonObj, msgURL string) (info *ChatRisInfo, user *UserChatMsg, read *UserMsgReadAck, err error) {
	var msgContext *ChatRisInfo
	var userMsg *UserChatMsg
	var redMsg *UserMsgReadAck

	switch msgURL {
	case ImCallbackRisMsg: // 风控消息
		if err = utils.UnmarshalUseNumber([]byte(msg.ExtendJsonData), &msgContext); err != nil {
			return nil, nil, nil, err
		}
		return msgContext, nil, nil, err
	case ImCallbackUserMsg: // 用户消息
		if err = utils.UnmarshalUseNumber([]byte(msg.ExtendJsonData), &userMsg); err != nil {
			return nil, nil, nil, err
		}
		return nil, userMsg, nil, err
	case ImCallbackReadMsg: //推送用户消息已读状态
		if err = utils.UnmarshalUseNumber([]byte(msg.ExtendJsonData), &redMsg); err != nil {
			return nil, nil, nil, err
		}
		return nil, nil, redMsg, err
	}

	return nil, nil, nil, fmt.Errorf("%s,暂无此类消息推送", msg)
}

//#endregion
