package services

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
	"hongze/hongze_cygx/utils"
	"time"
)

//const (
//	MchPKFileName              = "../hongze_mfyx/utils/cert/apiclient_key.pem"
//	Mchid                      = "1624495680"
//	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
//	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
//)

//// 微信商户建立连接
//func getWechatClient() (context.Context, *core.Client, error) {
//	var err error
//	defer func() {
//		if err != nil {
//			fmt.Println("err", err)
//			go utils.SendAlarmMsg(fmt.Sprint("微信商户建立连接失败 getWechatClient, err：", err.Error()), 2)
//		}
//	}()
//	// 使用 utils 提供的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
//	mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
//	if err != nil {
//		log.Print("load merchant private key error")
//		return nil, nil, err
//	}
//	ctx := context.Background()
//	// 使用商户私钥等初始化 client，并使它具有自动定时获取微信支付平台证书的能力
//	opts := []core.ClientOption{
//		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
//	}
//	client, err := core.NewClient(ctx, opts...)
//	if err != nil {
//		log.Printf("new wechat pay client err:%s", err)
//		return nil, nil, err
//	}
//	return ctx, client, nil
//}

//func init() {
//	GetQueryOrderByOutTradeNo("OD202404081631545174")
//}

// 根据订单号查询订单状态失败
func GetQueryOrderByOutTradeNo(outTradeNo string) (tradeState string, statusCode int, itemResp *Transaction) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println("err", err)
			go utils.SendAlarmMsg(fmt.Sprint("根据订单号查询订单状态失败 GetQueryOrderByOutTradeNo, err：", err.Error(), "outTradeNo", outTradeNo), 2)
		}
	}()
	//ctx, client, err := getWechatClient()
	//if err != nil {
	//	log.Printf("getWechatClientt err:%s", err)
	//	return
	//}
	ctx := context.Background()
	svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
	resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
		jsapi.QueryOrderByOutTradeNoRequest{
			OutTradeNo: core.String(outTradeNo),
			Mchid:      core.String(utils.Mchid),
		},
	)
	statusCode = result.Response.StatusCode
	//fmt.Println(statusCode)
	//订单状态码不存在直接返回
	if statusCode == 404 {
		err = nil
		return
	}
	tradeState = *resp.TradeState
	data, err := json.Marshal(resp)
	if err != nil {
		return
	}
	jsonstr := string(data)
	//item := new(Transaction)
	if err = json.Unmarshal([]byte(jsonstr), &itemResp); err != nil {
		return
	}
	//itemResp = item
	//fmt.Println(itemResp)
	//fmt.Println(tradeState)
	//fmt.Println(itemResp.TransactionId)
	return
}

// 订单超时手动关闭订单
func ExampleJsapiApiService_CloseOrder(OutTradeNo string) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleJsapiApiService_CloseOrder, err：", err.Error(), "OrderCode:", OutTradeNo), 2)
		}
	}()

	ctx := context.Background()
	svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
	_, err = svc.CloseOrder(ctx,
		jsapi.CloseOrderRequest{
			OutTradeNo: core.String(OutTradeNo),
			Mchid:      core.String(utils.Mchid),
		},
	)
	return
}

// Transaction
type Transaction struct {
	Amount          TransactionAmount `json:"amount,omitempty"`
	Appid           string            `json:"appid,omitempty"`
	Attach          string            `json:"attach,omitempty"`
	BankType        string            `json:"bank_type,omitempty"`
	Mchid           string            `json:"mchid,omitempty"`
	OutTradeNo      string            `json:"out_trade_no,omitempty"`
	Payer           TransactionPayer  `json:"payer,omitempty"`
	PromotionDetail []PromotionDetail `json:"promotion_detail,omitempty"`
	SuccessTime     time.Time         `json:"success_time,omitempty"`
	TradeState      string            `json:"trade_state,omitempty"`
	TradeStateDesc  string            `json:"trade_state_desc,omitempty"`
	TradeType       string            `json:"trade_type,omitempty"`
	TransactionId   string            `json:"transaction_id,omitempty"`
}

// TransactionAmount
type TransactionAmount struct {
	Currency      string `json:"currency,omitempty"`
	PayerCurrency string `json:"payer_currency,omitempty"`
	PayerTotal    int64  `json:"payer_total,omitempty"`
	Total         int64  `json:"total,omitempty"`
}

// TransactionPayer
type TransactionPayer struct {
	Openid string `json:"openid,omitempty"`
}

// PromotionDetail
type PromotionDetail struct {
	// 券ID
	CouponId *string `json:"coupon_id,omitempty"`
	// 优惠名称
	Name *string `json:"name,omitempty"`
	// GLOBAL：全场代金券；SINGLE：单品优惠
	Scope *string `json:"scope,omitempty"`
	// CASH：充值；NOCASH：预充值。
	Type *string `json:"type,omitempty"`
	// 优惠券面额
	Amount *int64 `json:"amount,omitempty"`
	// 活动ID，批次ID
	StockId *string `json:"stock_id,omitempty"`
	// 单位为分
	WechatpayContribute *int64 `json:"wechatpay_contribute,omitempty"`
	// 单位为分
	MerchantContribute *int64 `json:"merchant_contribute,omitempty"`
	// 单位为分
	OtherContribute *int64 `json:"other_contribute,omitempty"`
	// CNY：人民币，境内商户号仅支持人民币。
	Currency    *string                `json:"currency,omitempty"`
	GoodsDetail []PromotionGoodsDetail `json:"goods_detail,omitempty"`
}

// PromotionGoodsDetail
type PromotionGoodsDetail struct {
	// 商品编码
	GoodsId *string `json:"goods_id"`
	// 商品数量
	Quantity *int64 `json:"quantity"`
	// 商品价格
	UnitPrice *int64 `json:"unit_price"`
	// 商品优惠金额
	DiscountAmount *int64 `json:"discount_amount"`
	// 商品备注
	GoodsRemark *string `json:"goods_remark,omitempty"`
}
