package bus

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

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

type MultiPay struct{}

func (m *MultiPay) CreateQr() *comm.NatsMsgReplyer {
	utils.ConsoleFormat(pay.PayMultiQrApi)

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

// 多商户关闭订单应答
func (m *MultiPay) CloseOrder() *comm.NatsMsgReplyer {
	utils.ConsoleFormat("comm.pay.multiPay.closeOrder")

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

// 退款
func (m *MultiPay) Refund() *comm.NatsMsgReplyer {
	utils.ConsoleFormat("comm.pay.multiPay.refund")

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

// 退款查询
func (m *MultiPay) RefundQuery() *comm.NatsMsgReplyer {
	utils.ConsoleFormat("comm.pay.multiPay.refundQuery")

	return &comm.NatsMsgReplyer{
		Subject: "comm.pay.multiPay.refundQuery",
		Entity:  func() interface{} { return &model.ReFund{} },
		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
			fmt.Println("------------------multipay-refund-query-----------------")
			m := entity.(*model.ReFund)
			pay := PayMod(int(*m.PayMode), MULTI)
			ctx := context.Background()
			status, err := pay.ReFundQuery(ctx, m)
			return status, err
		},
	}
}
