package com.liquidnet.service.dragon.support;

import com.liquidnet.service.dragon.dto.DragonPayBaseReqDto;

/**
 * 微信小程序支付：deviceFrom / appIdType / paymentType 约定（不含 appid，appid 见 order 内 {@code WepayAppletPayConfigure}）。
 */
public final class WepayAppletPaySupport {

    public static final String DEVICE_APPLETB = "appletb";
    public static final String DEVICE_APPLET_DOUDOU = "appletdoudou";
    public static final String DEVICE_APPLET_MOOTOO = "appletmootoo";
    public static final String DEVICE_APPLET_WENQUE = "appletwenque";

    public static final String APP_ID_TYPE_B = "b";
    public static final String APP_ID_TYPE_DOUDOU = "doudou";
    public static final String APP_ID_TYPE_MOOTOO = "mootoo";
    public static final String APP_ID_TYPE_WENQUE = "wenque";

    private WepayAppletPaySupport() {
    }

    public static boolean requiresWechatOpenId(String deviceFrom) {
        if (deviceFrom == null) {
            return false;
        }
        return "js".equalsIgnoreCase(deviceFrom)
                || "applet".equalsIgnoreCase(deviceFrom)
                || DEVICE_APPLETB.equalsIgnoreCase(deviceFrom)
                || DEVICE_APPLET_DOUDOU.equalsIgnoreCase(deviceFrom)
                || DEVICE_APPLET_MOOTOO.equalsIgnoreCase(deviceFrom)
                || DEVICE_APPLET_WENQUE.equalsIgnoreCase(deviceFrom);
    }

    /**
     * 将 appletdoudou 等 deviceFrom 规范为 applet，并写入 appIdType。
     */
    public static void applyAppletDeviceFrom(DragonPayBaseReqDto dto) {
        if (dto == null || dto.getDeviceFrom() == null) {
            return;
        }
        String deviceFrom = dto.getDeviceFrom();
        if (DEVICE_APPLETB.equals(deviceFrom)) {
            dto.setDeviceFrom("applet");
            dto.setAppIdType(APP_ID_TYPE_B);
        } else if (DEVICE_APPLET_DOUDOU.equals(deviceFrom)) {
            dto.setDeviceFrom("applet");
            dto.setAppIdType(APP_ID_TYPE_DOUDOU);
        } else if (DEVICE_APPLET_MOOTOO.equals(deviceFrom)) {
            dto.setDeviceFrom("applet");
            dto.setAppIdType(APP_ID_TYPE_MOOTOO);
        } else if (DEVICE_APPLET_WENQUE.equals(deviceFrom)) {
            dto.setDeviceFrom("applet");
            dto.setAppIdType(APP_ID_TYPE_WENQUE);
        }
    }

    public static boolean usesMerchantB(String appIdType) {
        return APP_ID_TYPE_B.equals(appIdType);
    }

    public static boolean usesMerchantBByPaymentType(String paymentType) {
        return paymentType != null && paymentType.contains("APPLETB");
    }

    /**
     * 小程序微信 JSAPI：含历史 APPLETWEPAY、APPLETBWEPAY 及 APPLETDOUDOUWEPAY 等。
     */
    public static boolean isAppletWechatPaymentType(String paymentType) {
        return paymentType != null && paymentType.contains("APPLET") && paymentType.endsWith("WEPAY");
    }

    /** 场次批量退款统计：微信渠道 payment_type 白名单 */
    public static String[] wechatPaymentTypesForRefundStatis() {
        return new String[]{
                "APPWEPAY", "APPLETWEPAY", "APPLETBWEPAY",
                "APPLETDOUDOUWEPAY", "APPLETMOOTOOWEPAY", "APPLETWENQUEWEPAY",
                "WAPWEPAY", "JSWEPAY", "wepay", "MICROPAYWEPAY"
        };
    }

    /** 场次批量退款统计：支付宝渠道 payment_type 白名单 */
    public static String[] alipayPaymentTypesForRefundStatis() {
        return new String[]{"APPALIPAY", "WAPALIPAY", "APPLETALIPAY", "alipay"};
    }

    /** Admin 列表/详情：payment_type 展示文案 */
    public static String paymentChannelDisplayLabel(String paymentType) {
        if (paymentType == null || paymentType.trim().isEmpty()) {
            return "";
        }
        String pt = paymentType.trim();
        if (pt.contains("UNIONPAY")) {
            return "银联云闪付";
        }
        if (pt.contains("DOUYIN")) {
            return "抖音支付";
        }
        if (pt.contains("ALIPAY") || "alipay".equalsIgnoreCase(pt)) {
            return "支付宝";
        }
        if (isAppletWechatPaymentType(pt)) {
            if (pt.contains("DOUDOU")) {
                return "微信(DouDou)";
            }
            if (pt.contains("MOOTOO")) {
                return "微信(mootoo)";
            }
            if (pt.contains("WENQUE")) {
                return "微信(wenque)";
            }
            if (pt.contains("APPLETB")) {
                return "微信(摩登)";
            }
            return "微信小程序";
        }
        if (pt.endsWith("WEPAY") || "wepay".equalsIgnoreCase(pt)) {
            return "微信";
        }
        return pt;
    }
}
