package bus

import (
	"context"
	"fmt"
	"pay/db"
	"pay/db/model"
	"pay/db/repo"
	"pay/log"
	"pay/utils"

	"github.com/nats-io/nats.go"
	"infish.cn/comm"
	"infish.cn/comm/pay"
)

type SinglePay struct{}

// 创建二维码
func (s *SinglePay) CreateQr() *comm.NatsMsgReplyer {
	utils.ConsoleFormat(pay.PaySingleQrApi)

	return &comm.NatsMsgReplyer{
		Subject: pay.PaySingleQrApi,
		Entity:  func() interface{} { return &pay.OrderMsg{} },
		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
			m := entity.(*pay.OrderMsg)
			pay := PayMod(int(*m.PayMod), SINGLE)
			ctx := context.Background()
			res, err := pay.Pay(ctx, m)
			return res, err
		},
	}
}

// 关闭订单应答
func (s *SinglePay) CloseOrder() *comm.NatsMsgReplyer {
	utils.ConsoleFormat(pay.PaySingleCloseOrderApi)

	return &comm.NatsMsgReplyer{
		Subject: pay.PaySingleCloseOrderApi,
		Entity:  func() interface{} { return &pay.OrderMsg{} },
		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
			m := entity.(*pay.OrderMsg)
			pay := PayMod(int(*m.PayMod), SINGLE)
			ctx := context.Background()
			flag, err := pay.Close(ctx, m)
			return flag, err
		},
	}
}

// 退款
func (s *SinglePay) Refund() *comm.NatsMsgReplyer {
	utils.ConsoleFormat(pay.PaySingleRefundApi)

	return &comm.NatsMsgReplyer{
		Subject: pay.PaySingleRefundApi,
		Entity:  func() interface{} { return &model.ReFund{} },
		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
			refund := entity.(*model.ReFund)
			// ???适配缺失退款理由
			if refund.Reason == "" {
				refund.Reason = "默认退款"
			}
			pay := PayMod(int(*refund.PayMode), SINGLE)
			ctx := context.Background()
			flag, err := pay.ReFundPay(ctx, refund)
			return flag, err
		},
	}
}

// 退款查询
func (s *SinglePay) RefundQuery() *comm.NatsMsgReplyer {
	utils.ConsoleFormat(pay.PaySingleRefundQueryApi)

	return &comm.NatsMsgReplyer{
		Subject: pay.PaySingleRefundQueryApi,
		Entity:  func() interface{} { return &model.ReFund{} },
		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
			m := entity.(*model.ReFund)
			pay := PayMod(int(*m.PayMode), SINGLE)
			ctx := context.Background()
			status, err := pay.ReFundQuery(ctx, m)
			return status, err
		},
	}
}

// 支付成功
func (s *SinglePay) SuccPayStream() *comm.NatsStreamWather {
	res := &model.ResData{}
	// "queenshow-pay-succ#queenshow.paysucc#queenshow-paysucc-queue
	return &comm.NatsStreamWather{
		Stream:        "queenshow-pay-succ",
		Topic:         "queenshow.paysucc",
		Queue:         "queenshow-paysucc-queue",
		AckWaitMinute: int64(1),
		Entity:        func() interface{} { return res },
		Cb: func(msg *nats.Msg, entity interface{}) {
			defer func() {
				if err := recover(); err != nil {
					log.Info("系统异常", err)
				}
			}()

			session := &repo.RepoSession{Client: db.GMongoDb}
			fmt.Println(session.Client.Database.Name())

			// fmt.Println("===============购买结果返回=================")
			// log.Info("===============购买结果返回=================")
			// data := entity.(*model.ResData)
			// orderMsg := data.Data.(map[string]interface{})
			// flag, _ := Paied(data.Code, orderMsg["_id"].(string), int(orderMsg["payMode"].(float64)), session)
			// if flag {
			// 	msg.Ack()
			// }
		},
	}

}
