package controllers

import (
	"bytes"
	"encoding/json"
	"git.rosy.net.cn/jx-callback/globals/api"

	"git.rosy.net.cn/jx-callback/business/partner/im"
	"github.com/astaxie/beego/server/web"
)

type IMController struct {
	web.Controller
}

// @Title IM获取门店用户聊天列表
// @Description IM获取门店用户聊天列表
// @Param	token			header		string	true	"认证token"
// @Param	payLoad	        formData	string	true	"平台应用映射关系"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetImUserList [get]
func (c *IMController) GetImUserList() {
	c.callGetImUserList(func(params *tImGetImUserListParams) (retVal interface{}, errCode string, err error) {
		var relInfo []im.RelInfo
		if err = json.Unmarshal([]byte(params.PayLoad), &relInfo); err == nil {
			retVal, err = im.GetImUserList(relInfo)
		}
		return retVal, "", err
	})
}

// @Title IM获取单个用户聊天详情
// @Description IM获取单个用户聊天详情
// @Param	token			header		string	true	"认证token"
// @Param	payLoad	        formData	string	true	"平台用户应用映射关系"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetImChatDetail [get]
func (c *IMController) GetImChatDetail() {
	c.callGetImChatDetail(func(params *tImGetImChatDetailParams) (retVal interface{}, errCode string, err error) {
		var temp []im.UserRelInfo
		if err = json.Unmarshal([]byte(params.PayLoad), &temp); err == nil {
			retVal, err = im.GetImChatDetail(temp)
		}
		return retVal, "", err
	})
}

// @Title IM设置门店与单个用户已读
// @Description IM设置门店与单个用户已读
// @Param	token			header		string	true	"认证token"
// @Param	appID			formData	string	true	"应用id"
// @Param	vendorStoreID	formData	string	true	"平台门店id"
// @Param	vendorID		formData	string	true	"平台id"
// @Param	userID	        formData	string	true	"用户id/会话id"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SetImMsgRead [post]
func (c *IMController) SetImMsgRead() {
	c.callSetImMsgRead(func(params *tImSetImMsgReadParams) (retVal interface{}, errCode string, err error) {
		err = im.SetJxMsgRead(params.AppID, params.VendorStoreID, params.VendorID, params.UserID)
		return nil, "", err
	})
}

// @Title 向平台商发送信息(https方式)
// @Description 向平台商发送信息(https方式)
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrgCode	formData	string	true	"平台ppid"
// @Param	sendData		formData	string	true	"平台商消息结构体"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendToVendorV2 [post]
func (c *IMController) SendToVendorV2() {
	c.callSendToVendorV2(func(params *tImSendToVendorV2Params) (retVal interface{}, errCode string, err error) {
		var sendData im.SendData
		b := bytes.NewBufferString(params.SendData)
		decoder := json.NewDecoder(b)
		if err = decoder.Decode(&sendData); err == nil {
			if err = im.SendVendorV2(sendData, params.VendorOrgCode); err != nil {
				return nil, "", err
			}
		}
		return nil, "", nil
	})
}

// @Title 查询多平台门店IM单聊开关状态
// @Description 查询多平台门店IM单聊开关状态
// @Param	token			header		string	true	"认证token"
// @Param	data    	    query	    string	true	"查询多门店im状态信息"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetPoiIMStatus [get]
func (c *IMController) GetPoiIMStatus() {
	c.callGetPoiIMStatus(func(params *tImGetPoiIMStatusParams) (interface{}, string, error) {
		var req []im.GetPoiIMStatusReq
		b := bytes.NewBufferString(params.Data)
		decoder := json.NewDecoder(b)
		if err := decoder.Decode(&req); err == nil {
			retVal := im.GetPoiIMStatus(req)
			return retVal, "", nil
		} else {
			return nil, "", err
		}
	})
}

// @Title 设置多平台门店IM线上状态
// @Description 设置多平台门店IM线上状态
// @Param	token			header		string	true	"认证token"
// @Param	data    	    formData	string	true	"设置多平台门店im状态信息"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SetPoiIMStatus [post]
func (c *IMController) SetPoiIMStatus() {
	c.callSetPoiIMStatus(func(params *tImSetPoiIMStatusParams) (interface{}, string, error) {
		var req []im.SetPoiIMStatusReq
		b := bytes.NewBufferString(params.Data)
		decoder := json.NewDecoder(b)
		if err := decoder.Decode(&req); err == nil {
			err := im.SetPoiIMStatus(req)
			return nil, "", err
		}
		return nil, "", nil
	})
}

// @Title 获取饿百多媒体链接
// @Description 获取饿百多媒体链接
// @Param	token			header		string	true	"认证token"
// @Param	platformShopID  query   	string	true	"饿了么门店id"
// @Param	mediaID         query   	string	true	"多媒体资源id"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetElmMedia [get]
func (c *IMController) GetElmMedia() {
	c.callGetElmMedia(func(params *tImGetElmMediaParams) (interface{}, string, error) {
		retVal, err := api.EbaiAPI.GetMediaUrl(params.PlatformShopID, params.MediaID)
		return retVal, "", err
	})
}

// @Title 增加门店自定义回复模板
// @Description 增加门店自定义回复模板
// @Param	token			header		string	true	"认证token"
// @Param	appID           formData   	string	true	"应用id"
// @Param	vendorStoreID   formData   	string	true	"美团门店id"
// @Param	replyTemplate   formData   	string	true	"新增或修改自定义模板"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddCustomReply [post]
func (c *IMController) AddCustomReply() {
	c.callAddCustomReply(func(params *tImAddCustomReplyParams) (interface{}, string, error) {
		retVal, err := im.AddCustomReply(params.AppID, params.VendorStoreID, params.ReplyTemplate)
		return retVal, "", err
	})
}

// @Title 获取门店自定义回复模板
// @Description 获取门店自定义回复模板
// @Param	token			header		string	true	"认证token"
// @Param	appID           query   	string	true	"应用id"
// @Param	vendorStoreID   query   	string	true	"美团门店id"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetCustomReply [get]
func (c *IMController) GetCustomReply() {
	c.callGetCustomReply(func(params *tImGetCustomReplyParams) (interface{}, string, error) {
		retVal := im.GetCustomTemplate(params.AppID, params.VendorStoreID)
		return retVal, "", nil
	})
}
