package com.cf.imes.framework.pay.config;

import com.cf.imes.framework.common.util.encrypt.AesUtils;
import com.github.binarywang.wxpay.config.WxPayConfig;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.nio.charset.StandardCharsets;

/**
 * @author Gqr
 * @since 2025/4/15 16:52
 */
@ConfigurationProperties(prefix = "chenfeng.pay.wechat")
@Data
public class ChenfengWechatPayConfig extends WxPayConfig {
    @Value("${chenfeng.encrypt.publicKey:}")
    private String publicKey;

    /**
     * 给微信支付的回调地址
     */
    private String notifyUrl;

    @Override
    public void setAppId(String appId) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setAppId(AesUtils.decrypt(appId, publicKey));
        } else {
            super.setAppId(appId);
        }
    }

    @Override
    public void setMchId(String mchId) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setMchId(AesUtils.decrypt(mchId, publicKey));
        } else {
            super.setMchId(mchId);
        }
    }

    @Override
    public void setMchKey(String mchKey) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setMchKey(AesUtils.decrypt(mchKey, publicKey));
        } else {
            super.setMchKey(mchKey);
        }
    }

    @Override
    public void setApiV3Key(String apiV3Key) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setApiV3Key(AesUtils.decrypt(apiV3Key, publicKey));
        } else {
            super.setApiV3Key(apiV3Key);
        }
    }

    @Override
    public void setCertSerialNo(String certSerialNo) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setCertSerialNo(AesUtils.decrypt(certSerialNo, publicKey));
        } else {
            super.setCertSerialNo(certSerialNo);
        }
    }

    @Override
    public void setPublicKeyId(String publicKeyId) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setPublicKeyId(AesUtils.decrypt(publicKeyId, publicKey));
        } else {
            super.setPublicKeyId(publicKeyId);
        }
    }

    @Override
    public void setNotifyUrl(String notifyUrl) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setNotifyUrl(AesUtils.decrypt(notifyUrl, publicKey));
        } else {
            super.setNotifyUrl(notifyUrl);
        }
    }

    @Override
    public void setPublicKeyString(String publicKeyString) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setPublicKeyContent(AesUtils.decrypt(publicKeyString, publicKey).getBytes(StandardCharsets.UTF_8));
        } else {
            super.setPublicKeyContent(publicKeyString.getBytes(StandardCharsets.UTF_8));
        }
    }

    @Override
    public void setPrivateKeyString(String privateKeyString) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setPrivateKeyContent(AesUtils.decrypt(privateKeyString, publicKey).getBytes(StandardCharsets.UTF_8));
        } else {
            super.setPrivateKeyContent(privateKeyString.getBytes(StandardCharsets.UTF_8));
        }
    }
}
