package svc

import (
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/logx"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/iGoogle-ink/gopay"
	"io/ioutil"
	"net/url"
)

func CheckAllCallbackParams(c *gin.Context) (interface{}, string, string) {
	body, _ := ioutil.ReadAll(c.Request.Body)
	dataAlipay, _ := GetAlipayCallbackParams(body)
	if dataAlipay != nil && dataAlipay.PayMethod == md.ALIPAY {
		return dataAlipay, dataAlipay.OrderType, dataAlipay.PayMethod
	}
	dataWxPay, _ := GetWxPayCallbackParams(body)
	if dataWxPay != nil && dataWxPay.PayMethod == md.WX_PAY {
		return dataWxPay, dataWxPay.OrderType, dataWxPay.PayMethod
	}
	return nil, "", ""
}

// 支付宝参数解析
func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) {
	//decodeArgs := "order_type=privilege_card&pay_method=alipay&master_id=123456&gmt_create=2021-01-06+15%3A33%3A18&charset=utf-8&seller_email=1666296478%40qq.com&subject=%E6%B5%8B%E8%AF%95&sign=ILbQtP7E51hcdKaroi%2FKlefltw%2BaSOeaqgvBhYIcIuRy5yy440OIOdxPEvhTITA%2BHnA1Lgf3STepzzHVmvvTFIGeKL%2FVvz%2FLSI7vXGpUBxcw2entVZCzhmblGAB8hiK4EOHBIfUAQiyo9ePWl63p%2B%2BvS4CQuZe8SFXrFaj2bTnRyBPemxekudU8TP8tZd630SzHx7kOHYqxTYZ7kHMrLK0fjIdSWBHtSD2cocm%2FxVQ3wYXUyyfZwwTpcXLW9ao97Uj1hZSnknuFRtMfvE57D5f4W3cB%2Bnp%2B39xpuvfbBlbXXllzKUfkWVBqV6zebYMnzaogwVN%2FDXgp74BQOzkJYvA%3D%3D&buyer_id=2088012352245491&invoice_amount=0.01&notify_id=2021010600222153319045491449160553&fund_bill_list=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22PCREDIT%22%7D%5D&notify_type=trade_status_sync&trade_status=TRADE_SUCCESS&receipt_amount=0.01&app_id=2016120103683451&buyer_pay_amount=0.01&sign_type=RSA2&seller_id=2088221294035253&gmt_payment=2021-01-06+15%3A33%3A18&notify_time=2021-01-06+15%3A33%3A19&passback_params=706160991839039994&version=1.0&out_trade_no=791161468268877545&total_amount=0.01&trade_no=2021010622001445491401511921&auth_app_id=2016120103683451&buyer_logon_id=150****0420&point_amount=0.00"
	decodeArgs, err := url.QueryUnescape(string(body))
	if err != nil {
		_ = logx.Warn(err)
		return nil, logx.Warn("回调参数解码错误")
	}
	data, err := url.ParseQuery(decodeArgs)
	if err != nil {
		return nil, err
	}
	dataMap := make(map[string]interface{})
	for k := range data {
		dataMap[k] = data.Get(k)
	}
	callbackStr := utils.Serialize(dataMap)
	fmt.Println("支付宝回调数据", string(callbackStr))
	var args md.AliPayCallback
	if err := json.Unmarshal(callbackStr, &args); err != nil {
		return nil, logx.Warn(err)
	}
	return &args, nil
}

// 微信回调参数解析
func GetWxPayCallbackParams(body []byte) (*md.WxPayCallback, error) {
	dataMap := make(gopay.BodyMap)
	if err := xml.Unmarshal(body, &dataMap); err != nil {
		return nil, fmt.Errorf("xml.Unmarshal(%s)：%w", string(body), err)
	}
	callbackStr := utils.Serialize(dataMap)
	fmt.Println("微信回调数据", string(callbackStr))
	var args md.WxPayCallback
	err := json.Unmarshal(callbackStr, &args)
	if err != nil {
		fmt.Println("err3")
		return nil, logx.Warn(err)
	}
	return &args, nil
}

var PayFuncList = map[string]map[string]func(*gin.Context) (interface{}, error){
	md.CommunityTeam: {
		md.BALANCE_PAY: BalanceCommunityTeam,
		md.ALIPAY:      AlipayCommunityTeam,
		md.WX_PAY:      WxPayCommunityTeam,
	},
	md.CommunityTeamPay: {
		md.BALANCE_PAY: BalanceCommunityTeamPay,
		md.ALIPAY:      AlipayCommunityTeamPay,
		md.WX_PAY:      WxPayCommunityTeamPay,
	},
}
var PayCallbackFuncList = map[string]map[string]func(*gin.Context){
	md.CommunityTeam: {
		md.BALANCE_PAY: nil,
		md.ALIPAY:      AlipayCallbackCommunityTeam,
		md.WX_PAY:      WxPayCallbackCommunityTeam,
	},
	md.CommunityTeamPay: {
		md.BALANCE_PAY: nil,
		md.ALIPAY:      AlipayCallbackCommunityTeamPay,
		md.WX_PAY:      WxPayCallbackCommunityTeamPay,
	},
}
