package svc

import (
	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
	"applet/app/utils/logx"
	"code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/pay"
	"github.com/gin-gonic/gin"
	"strings"
)

// 微信支付回调处理
func wxPayCallback(c *gin.Context) (string, error) {
	data, ok := c.Get("callback")
	if data == nil || !ok {
		return "", e.NewErrCode(e.ERR_INVALID_ARGS)
	}
	args := data.(*md.WxPayCallback)
	_, ok = db.DBs[args.MasterID]
	if !ok {
		return "", logx.Warn("wxpay Failed : master_id not found")
	}
	c.Set("mid", args.MasterID)
	//回调交易状态失败
	if args.ResultCode != "SUCCESS" || args.ReturnCode != "SUCCESS" {
		return "", logx.Warn("wxpay Failed : trade status failed")
	}
	return args.OutTradeNo, nil
}
func CommPayData(c *gin.Context, params map[string]string) (interface{}, error) {
	platform := c.GetHeader("Platform")
	browser := c.GetHeader("browser")
	var r interface{}
	var err error
	switch platform {
	case md.PLATFORM_WX_APPLET:
		params = WxMiniProgPayConfig(c, params)
		r, err = pay.WxMiniProgPay(params)
	case md.PLATFORM_WAP:
		if strings.Contains(browser, "wx_pay_browser") {
			params = WxJsApiConfig(c, params)
			r, err = pay.WxAppJSAPIPay(params)
		} else {
			params = WxH5PayConfig(c, params)
			r, err = pay.WxH5Pay(params)
		}
	case md.PLATFORM_ANDROID, md.PLATFORM_IOS, md.PLATFORM_JSAPI:
		params = WxAPPConfig(c, params)
		r, err = pay.WxAppPay(params)
	default:
		return nil, e.NewErrCode(e.ERR_PLATFORM)
	}
	if err != nil {
		return nil, err
	}

	return r, nil
}
func WxH5PayConfig(c *gin.Context, params map[string]string) map[string]string {
	params["pay_wx_appid"] = SysCfgGet(c, "wx_official_account_app_id")
	return params
}
func WxAPPConfig(c *gin.Context, params map[string]string) map[string]string {
	params["pay_wx_appid"] = SysCfgGet(c, "pay_wx_appid")
	return params
}

// 小程序v2
func WxMiniProgPayConfig(c *gin.Context, params map[string]string) map[string]string {
	//读取小程序设置的
	wxAppletCfg := db.GetAppletKey(c, MasterDb(c))
	params["pay_wx_appid"] = wxAppletCfg["app_id"]
	// 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType（因为该路由未经过jwt-auth中间件）
	user, err := CheckUser(c)
	if user == nil || err != nil {
		return params
	}
	if c.GetHeader("openid") != "" { //前端会传过来
		user.Profile.ThirdPartyWechatMiniOpenid = c.GetHeader("openid")
	}
	if user.Profile.ThirdPartyWechatMiniOpenid == "" {
		return params
	}
	params["third_party_wechat_openid"] = user.Profile.ThirdPartyWechatMiniOpenid
	return params
}
func WxJsApiConfig(c *gin.Context, params map[string]string) map[string]string {
	params["pay_wx_appid"] = SysCfgGet(c, "wx_official_account_app_id")
	// 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType（因为该路由未经过jwt-auth中间件）
	user, err := CheckUser(c)
	if user == nil || err != nil {
		return params
	}
	if c.GetHeader("openid") != "" { //前端会传过来
		user.Profile.ThirdPartyWechatH5Openid = c.GetHeader("openid")
	}
	if user.Profile.ThirdPartyWechatH5Openid == "" {
		return params
	}
	params["third_party_wechat_openid"] = user.Profile.ThirdPartyWechatH5Openid
	return params
}
