package controllers

import (
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"

	"git.rosy.net.cn/jx-callback/business/jxstore/common"

	"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
	"git.rosy.net.cn/baseapi/utils"

	"git.rosy.net.cn/jx-callback/globals"

	"git.rosy.net.cn/jx-callback/business/jxutils/eventhub/syseventhub"
	"git.rosy.net.cn/jx-callback/business/model"
	"github.com/astaxie/beego/server/web"
)

type SysController struct {
	web.Controller
}

// @Title 得到微信token
// @Description 得到微信token
// @Param	accessKey			query		string	true	"假token"
// @Param	oldToken			query		string	false	"之前的token"
// @Param	waitSecond			query		int		false	"等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWXToken [get]
func (c *SysController) GetWXToken() {
	c.callGetWXToken(func(params *tSysGetWXTokenParams) (retVal interface{}, errCode string, err error) {
		if params.AccessKey == globals.GetWeixinTokenKey {
			retVal = syseventhub.SysEventHub.GetWXToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
		}
		return retVal, "", err
	})
}

// @Title 得到微信小程序2token
// @Description 得到微信小程序2token
// @Param	accessKey			query		string	true	"假token"
// @Param	oldToken			query		string	false	"之前的token"
// @Param	waitSecond			query		int		false	"等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWX2Token [get]
func (c *SysController) GetWX2Token() {
	c.callGetWX2Token(func(params *tSysGetWX2TokenParams) (retVal interface{}, errCode string, err error) {
		if params.AccessKey == globals.GetWeixinTokenKey {
			retVal = syseventhub.SysEventHub.GetWX2Token(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
		}
		return retVal, "", err
	})
}

// @Title 得到易联云token
// @Description 得到易联云token
// @Param	accessKey			query		string	true	"假token"
// @Param	oldToken			query		string	false	"之前的token"
// @Param	waitSecond			query		int		false	"等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetYLYToken [get]
func (c *SysController) GetYLYToken() {
	c.callGetYLYToken(func(params *tSysGetYLYTokenParams) (retVal interface{}, errCode string, err error) {
		if params.AccessKey == globals.GetWeixinTokenKey {
			retVal = syseventhub.SysEventHub.GetYLYToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
		}
		return retVal, "", err
	})
}

// @Title 得到个推token
// @Description 得到个推token
// @Param	accessKey			query		string	true	"假token"
// @Param	oldToken			query		string	false	"之前的token"
// @Param	waitSecond			query		int		false	"等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetPushToken [get]
func (c *SysController) GetPushToken() {
	c.callGetPushToken(func(params *tSysGetPushTokenParams) (retVal interface{}, errCode string, err error) {
		if params.AccessKey == globals.GetWeixinTokenKey {
			retVal = syseventhub.SysEventHub.GetPushToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
		}
		return retVal, "", err
	})
}

// @Title 得到微盟token
// @Description 得到微盟token
// @Param	accessKey			query		string	true	"假token"
// @Param	oldToken			query		string	false	"之前的token"
// @Param	waitSecond			query		int		false	"等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWeimobToken [get]
func (c *SysController) GetWeimobToken() {
	c.callGetWeimobToken(func(params *tSysGetWeimobTokenParams) (retVal interface{}, errCode string, err error) {
		if params.AccessKey == globals.GetWeixinTokenKey {
			retVal = syseventhub.SysEventHub.GetWeimobToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
		}
		return retVal, "", err
	})
}

// @Title 得到饿百RTF转换内容
// @Description 得到饿百RTF转换内容
// @Param	imgListStr		query	string	true	"逗号分隔的图片列表可以是转义后的"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetEbaiRTFDetail [get]
func (c *SysController) GetEbaiRTFDetail() {
	var html string
	c.callGetEbaiRTFDetail(func(params *tSysGetEbaiRTFDetailParams) (retVal interface{}, errCode string, err error) {
		params.ImgListStr, _ = url.QueryUnescape(params.ImgListStr)
		imgList := strings.Split(params.ImgListStr, ",")
		html = ebaiapi.BuildRFTFromImgs(imgList...)
		return retVal, model.ErrorCodeIgnore, err
	})
	w := c.Ctx.ResponseWriter
	w.Header().Add("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)
	io.WriteString(w, html)
}

// @Title 得到平台账号信息
// @Description 得到平台账号信息
// @Param	token				header		string	true	"token"
// @Param	vendorID			query		int		false	"平台ID"
// @Param	vendorOrgCode		query		string	false	"平台账号"
// @Param	vendorType			query		string	false	"平台账号类型"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetVendorOrgCode [get]
func (c *SysController) GetVendorOrgCode() {
	c.callGetVendorOrgCode(func(params *tSysGetVendorOrgCodeParams) (retVal interface{}, errCode string, err error) {
		retVal, err = common.GetVendorOrgCode(params.Ctx, params.VendorID, params.VendorOrgCode, params.VendorType)
		return retVal, "", err
	})
}

// @Title 修改平台账号信息
// @Description 修改平台账号信息
// @Param	token			header		string	true	"token"
// @Param	id				formData	int		true	"ID"
// @Param	payload			formData	string	true	"json数据，vendorOrgCode对象()"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateVendorOrgCode [post]
func (c *SysController) UpdateVendorOrgCode() {
	c.callUpdateVendorOrgCode(func(params *tSysUpdateVendorOrgCodeParams) (retVal interface{}, errCode string, err error) {
		payload := make(map[string]interface{})
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), &payload); err == nil {
			err = common.UpdateVendorOrgCode(params.Ctx, params.Id, payload)
		}
		return retVal, "", err
	})
}

// @Title 添加平台账号信息
// @Description 添加平台账号信息
// @Param	token			header		string	true	"token"
// @Param	payload			formData	string	true	"json数据，vendorOrgCode对象()"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddVendorOrgCode [post]
func (c *SysController) AddVendorOrgCode() {
	c.callAddVendorOrgCode(func(params *tSysAddVendorOrgCodeParams) (retVal interface{}, errCode string, err error) {
		vendorOrgCode := &model.VendorOrgCode{}
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), &vendorOrgCode); err == nil {
			err = common.AddVendorOrgCode(params.Ctx, vendorOrgCode)
		}
		return retVal, "", err
	})
}
