package api

import (
	"fmt"
	"net/http"
	"pay/bus"
	"pay/db/model"
	"strings"

	"github.com/go-pay/gopay/alipay"
	"infish.cn/comm"
)

type SingleAliPayCallback struct {
}

func NewSingleAliPayCallback() PayCallback {
	return &SingleAliPayCallback{}
}

// CallBack 阿里回调
func (a *SingleAliPayCallback) CallBack(c *http.Request) (interface{}, error) {
	fmt.Println("--------------------------------------------alipay-single-callback----------------------------------------")
	fmt.Println("---------------------------------------------------------------------------------------------------------")

	notifyReq, err := alipay.ParseNotifyToBodyMap(c) // c.Request 是 gin 框架的写法
	if err != nil {
		return "fail", err
	}
	// gopay.BodyMap{"app_id": "2021003132622153", "auth_app_id": "2021003132622153", "buyer_id": "2088802597650055",
	// 	"buyer_logon_id": "134****6663", "buyer_pay_amount": "0.01", "charset": "utf-8",
	// 	"fund_bill_list": "[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]", "gmt_create": "2022-09-19 14:41:09",
	// 	"gmt_payment": "2022-09-19 14:41:26", "invoice_amount": "0.01", "notify_id": "2022091900222144127050051432338907",
	// 	"notify_time": "2022-09-19 14:41:30", "notify_type": "trade_status_sync", "out_trade_no": "alipay_63116317f17ac790a0e71a71",
	// 	"point_amount": "0.00", "receipt_amount": "0.01", "seller_email": "xuanku202206@163.com", "seller_id": "2088441503441284",
	// 	"sign":      "eHWrJQoapaIlttBLMjVFXT88RulOXf1eVOekTeY6HkzhpzZVuNQw3psPQyHD5vaXKqv7Eda1LzSmH/0yyf9ampBInLk1VPG8xkcMwakUquTllgDITiNv5BVmMeM+7dVU/g0KQPabjrY+ZNFiDqWAuT0ST7Yb75OrEt7hTqkXbie++jWjkNq3JNI3hoxbyh8dkvYjJkBhd1e7wCi2RwQ4unyixlsIyBjH6t2FkEPZZiz/Hxl6bfKFSaHlAcCY6P1eSpgv3ooKl6jUwk7usoDJ/7OinzqR2+hl5Tq7kotmOp8TpLr3dui4l1owd6Vr1titiFY8AKRuKj2lYl1IktaA/Q==",
	// 	"sign_type": "RSA2", "subject": "test alipay", "total_amount": "0.01", "trade_no": "2022091922001450051425907523", "trade_status": "TRADE_SUCCESS", "version": "1.0"}

	// orderSlice := strings.Split(notifyReq["out_trade_no"].(string), "_")
	// payMod := orderSlice[0]
	// tradeNo := orderSlice[1]

	// payMode := ALIPAY
	// res := model.SUCCESS.SetData(&model.OrderMsg{Id: orderSlice[1], PayMode: &payMode, ModName: orderSlice[0]})

	// singlePay := new(bus.SinglePay)
	// flag := singlePay.PushResultMessage(res)

	// if !flag {
	// 	return "faild", nil
	// }
	orderSlice := strings.Split(notifyReq["out_trade_no"].(string), "_")
	payMode := 0
	res := &model.OrderMsg{Id: orderSlice[1], PayMode: &payMode, ModName: orderSlice[0]}
	if notifyReq["trade_status"] == "TRADE_SUCCESS" {

		fmt.Println("----------------success-----------------------")
		err = bus.NatsCenter.PushMessage(comm.NewAppPaySuccSubject(orderSlice[0]), res)
		if err != nil {
			fmt.Println("----------------success-err-----------------------")
			return "fail", err
		}

	} else {
		fmt.Println("----------------fail-----------------------")
		bus.NatsCenter.PushMessage(comm.NewAppPayFailSubject(orderSlice[0]), res)
		if err != nil {
			fmt.Println("----------------fail-err-----------------------")
			return "fail", err
		}
	}
	fmt.Println("----------------success-end-----------------------")
	return "success", nil
}
