package data

import (
	"fmt"
	"os"
	"voucher/internal/conf"
	"voucher/internal/pkg/helper"
	"voucher/internal/pkg/wechat/srv/marketing"
	utils2 "voucher/internal/pkg/wechat/utils"
)

type Wx struct {
	Clients map[string]*marketing.Marketing
}

func NewWx(c *conf.Bootstrap) (*Wx, error) {

	clients := make(map[string]*marketing.Marketing, 0)

	client, err := buildWx(c.Wechat)
	if err != nil {
		return nil, err
	}

	clients[c.Wechat.MchID] = client

	for _, v := range c.WechatSubject {
		cli, e := buildWechat(v)
		if e != nil {
			return nil, e
		}
		clients[v.MchID] = cli
	}

	return &Wx{Clients: clients}, nil
}

func (this *Wx) Get(mchId string) (*marketing.Marketing, error) {

	if mchId == "" {
		return nil, fmt.Errorf("微信商户ID不能为空")
	}

	if len(this.Clients) == 0 {
		return nil, fmt.Errorf("微信调用client不存在")
	}

	if client, ok := this.Clients[mchId]; ok {
		return client, nil
	}

	return nil, fmt.Errorf("微信调用client不存在[%s]", mchId)
}

func buildWechat(wx *conf.WechatSubject) (*marketing.Marketing, error) {

	dir, err := os.Getwd()
	if err != nil {
		return nil, fmt.Errorf("商户ID[%s]商户名称[%s]获取目的地址有误[%v]", wx.MchID, wx.Name, err)
	}

	filePath := fmt.Sprintf("%s/cert/wechat/%s", dir, wx.MchID)
	if !helper.FileExists(filePath) {
		panic(fmt.Sprintf("商户ID[%s]商户名称[%s]微信密钥证书信息不存在,请联系技术人员处理", wx.MchID, wx.Name))
	}

	cc, err := utils2.CreateMchConfig(
		wx.MchID,                      // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		wx.MchCertificateSerialNumber, // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"), // 商户API证书私钥文件路径，本地文件路径
		wx.WechatPayPublicKeyID,                                  // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		fmt.Sprintf("%s/%s", filePath, "pub_key.pem"),            // 微信支付公钥文件路径，本地文件路径
		wx.MchApiV3Key,
	)
	if err != nil {
		return nil, err
	}

	return &marketing.Marketing{MchConfig: cc}, nil
}

func buildWx(wx *conf.Wechat) (*marketing.Marketing, error) {

	dir, err := os.Getwd()
	if err != nil {
		return nil, fmt.Errorf("商户ID[%s]商户名称[%s]获取目的地址有误[%v]", wx.MchID, wx.Name, err)
	}

	filePath := fmt.Sprintf("%s/cert/wechat/%s", dir, wx.MchID)
	if !helper.FileExists(filePath) {
		panic(fmt.Sprintf("商户ID[%s]商户名称[%s]微信密钥证书信息不存在,请联系技术人员处理", wx.MchID, wx.Name))
	}

	cc, err := utils2.CreateMchConfig(
		wx.MchID,                      // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		wx.MchCertificateSerialNumber, // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"), // 商户API证书私钥文件路径，本地文件路径
		wx.WechatPayPublicKeyID,                                  // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		fmt.Sprintf("%s/%s", filePath, "pub_key.pem"),            // 微信支付公钥文件路径，本地文件路径
		"",
	)
	if err != nil {
		return nil, err
	}

	return &marketing.Marketing{MchConfig: cc}, nil
}
