package payment

import (
	"context"
	"fmt"

	"bsm/full/module/finance/wallet/internal/config"
	"bsm/full/module/finance/wallet/internal/models"
	pb "bsm/full/module/finance/wallet/pb"
	"git.apinb.com/bsm-sdk/core/service"
)

// 获取平台支持的支付网关
func Way(ctx context.Context, in *pb.WayRequest) (reply *pb.WayReply, err error) {
	auth, ok := service.ParseMetaCtx(ctx, nil)
	if ok != nil {
		return nil, ok
	}

	items := make([]*pb.WayItem, 0)

	if config.Spec.Wallet.AlipyIsOpen {
		items = append(items, &pb.WayItem{
			Ident: "ALIPAY",
			Title: "支付宝",
		})
	}

	if config.Spec.Wallet.WechatpayIsOpen {
		items = append(items, &pb.WayItem{
			Ident: "WECHATPAY",
			Title: "微信支付",
		})
	}

	if config.Spec.Wallet.WalletIsOpen {
		balance := fmt.Sprintf("%.2f", models.GetWalletBalanceByPassportIdentity(auth.Identity))
		items = append(items, &pb.WayItem{
			Ident: "WALLET",
			Title: "钱包余额",
			Intro: "余额：" + balance + " 元",
		})
	}

	return &pb.WayReply{
		Way: items,
	}, nil
}
