package com.liquidnet.service.dragon.config;

import com.liquidnet.service.dragon.support.WepayAppletPaySupport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * 小程序微信支付 appid，与 sweet {@code liquidnet.wechat.applet.*} 配置一致。
 */
@Configuration
public class WepayAppletPayConfigure {

    private static String appidZhengzai;
    private static String appidModern;
    private static String appidDoudou;
    private static String appidMootoo;
    private static String appidWenque;

    @Value("${liquidnet.wechat.applet.zhengzai.appid}")
    public void setAppidZhengzai(String appidZhengzai) {
        WepayAppletPayConfigure.appidZhengzai = appidZhengzai;
    }

    @Value("${liquidnet.wechat.applet.modern.appid}")
    public void setAppidModern(String appidModern) {
        WepayAppletPayConfigure.appidModern = appidModern;
    }

    @Value("${liquidnet.wechat.applet.doudou.appid}")
    public void setAppidDoudou(String appidDoudou) {
        WepayAppletPayConfigure.appidDoudou = appidDoudou;
    }

    @Value("${liquidnet.wechat.applet.mootoo.appid}")
    public void setAppidMootoo(String appidMootoo) {
        WepayAppletPayConfigure.appidMootoo = appidMootoo;
    }

    @Value("${liquidnet.wechat.applet.wenque.appid}")
    public void setAppidWenque(String appidWenque) {
        WepayAppletPayConfigure.appidWenque = appidWenque;
    }

    public static String resolveAppletAppIdByAppIdType(String appIdType) {
        if (appIdType == null || appIdType.trim().isEmpty()) {
            return appidZhengzai;
        }
        switch (appIdType) {
            case WepayAppletPaySupport.APP_ID_TYPE_B:
                return appidModern;
            case WepayAppletPaySupport.APP_ID_TYPE_DOUDOU:
                return appidDoudou;
            case WepayAppletPaySupport.APP_ID_TYPE_MOOTOO:
                return appidMootoo;
            case WepayAppletPaySupport.APP_ID_TYPE_WENQUE:
                return appidWenque;
            default:
                return appidZhengzai;
        }
    }

    public static String resolveAppletAppIdByPaymentType(String paymentType) {
        if (paymentType == null) {
            return appidZhengzai;
        }
        if (paymentType.contains("APPLETB")) {
            return appidModern;
        }
        if (paymentType.contains("DOUDOU")) {
            return appidDoudou;
        }
        if (paymentType.contains("MOOTOO")) {
            return appidMootoo;
        }
        if (paymentType.contains("WENQUE")) {
            return appidWenque;
        }
        return appidZhengzai;
    }

    /**
     * 查单/退款：优先 paymentType；老摩登单可能 payment_type=APPLETWEPAY 但 pay_code 为 PAYB 前缀。
     */
    public static String resolveAppletAppIdForTradeQuery(String paymentType, String payCode) {
        String appId = resolveAppletAppIdByPaymentType(paymentType);
        if (appidZhengzai != null && appidZhengzai.equals(appId) && payCode != null
                && (payCode.startsWith("PAYB") || payCode.contains("b"))) {
            return appidModern;
        }
        return appId;
    }
}
