package yibao_pay

import (
	"code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical"
	zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/yop-platform/yop-go-sdk/yop/client"
	"github.com/yop-platform/yop-go-sdk/yop/constants"
	"github.com/yop-platform/yop-go-sdk/yop/request"
	"github.com/yop-platform/yop-go-sdk/yop/response"
	"github.com/yop-platform/yop-go-sdk/yop/utils"
	"net/url"
	"os"
	"strings"
	"xorm.io/xorm"
)

func GetSend(method, appId, isvPriKey string, param map[string]string) (*response.YopResponse, error) {
	var priKey = request.IsvPriKey{Value: isvPriKey, CertType: request.RSA2048}
	var yopRequest = request.NewYopRequest(constants.GET_HTTP_METHOD, method)
	yopRequest.AppId = appId
	yopRequest.IsvPriKey = priKey
	for k, v := range param {
		yopRequest.AddParam(k, v)
	}
	yopResp, err := client.DefaultClient.Request(yopRequest)
	return yopResp, err
}
func PostFormSend(method, appId, isvPriKey string, param map[string]string) (*response.YopResponse, error) {
	var priKey = request.IsvPriKey{Value: isvPriKey, CertType: request.RSA2048}
	var yopRequest = request.NewYopRequest(constants.POST_HTTP_METHOD, method)
	yopRequest.AppId = appId
	yopRequest.IsvPriKey = priKey
	for k, v := range param {
		yopRequest.AddParam(k, v)
	}
	yopResp, err := client.DefaultClient.Request(yopRequest)
	return yopResp, err
}
func PostJsonSend(method, appId, isvPriKey string, params map[string]interface{}) (*response.YopResponse, error) {
	var priKey = request.IsvPriKey{Value: isvPriKey, CertType: request.RSA2048}
	var yopRequest = request.NewYopRequest(constants.POST_HTTP_METHOD, method)
	yopRequest.AppId = appId
	yopRequest.IsvPriKey = priKey
	yopRequest.Content = utils.ParseToJsonStr(params)
	yopResp, err := client.DefaultClient.Request(yopRequest)
	return yopResp, err
}
func FileUploadSend(method, appId, isvPriKey string, name string, f *os.File) (*response.YopResponse, error) {
	var priKey = request.IsvPriKey{Value: isvPriKey, CertType: request.RSA2048}
	var yopRequest = request.NewYopRequest(constants.POST_HTTP_METHOD, method)
	yopRequest.AppId = appId
	yopRequest.IsvPriKey = priKey
	yopRequest.AddFile(name, f)
	yopResp, err := client.DefaultClient.Request(yopRequest)
	return yopResp, err
}
func GetYibao(bodyStr string) (*YibaoJoinCallback, error) {
	decodeArgs, err := url.QueryUnescape(bodyStr)
	if err != nil {
		return nil, errors.New("失败")
	}
	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 := zhios_third_party_utils.SerializeStr(dataMap)
	callbackStr = strings.ReplaceAll(callbackStr, "\\r", "")
	callbackStr = strings.ReplaceAll(callbackStr, "\\n", "")
	fmt.Println("易宝回调数据", string(callbackStr))
	var args YibaoJoinCallback
	if err := json.Unmarshal([]byte(callbackStr), &args); err != nil {
		return nil, err
	}
	return &args, nil
}
func CommKey(eg *xorm.Engine, Prd bool) map[string]string {
	appId := offical.SysCfgByKeyStr(eg, "yibao_appid")
	isvPriKey := offical.SysCfgByKeyStr(eg, "yibao_key")
	parentMerchantNo := offical.SysCfgByKeyStr(eg, "yibao_merchant_no")
	zhiyingMerchantNo := offical.SysCfgByKeyStr(eg, "yibao_zhiying_merchant_no")
	zhiyingOnlineBili := offical.SysCfgByKeyStr(eg, "yibao_zhiying_online_bili")
	zhiyingUnlineBili := offical.SysCfgByKeyStr(eg, "yibao_zhiying_unline_bili")
	yibaoOnlineBili := offical.SysCfgByKeyStr(eg, "yibao_online_bili")
	yibaoUnlineBili := offical.SysCfgByKeyStr(eg, "yibao_unline_bili")
	if Prd == false {
		appId = offical.SysCfgByKeyStr(eg, "yibao_appid_test")
		isvPriKey = offical.SysCfgByKeyStr(eg, "yibao_key_test")
	}
	res := map[string]string{
		"zhiyingOnlineBili": zhiyingOnlineBili,
		"zhiyingUnlineBili": zhiyingUnlineBili,
		"yibaoOnlineBili":   yibaoOnlineBili,
		"yibaoUnlineBili":   yibaoUnlineBili,
		"appId":             appId,
		"zhiyingMerchantNo": zhiyingMerchantNo,
		"isvPriKey":         isvPriKey,
		"parentMerchantNo":  parentMerchantNo,
	}
	return res
}