package com.liquidnet.service.dragon.support;

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

/**
 * 微信小程序支付：deviceFrom / appIdType 与微信 appid 映射。
 */
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 static final String APPLET_DEFAULT = "wx4732efeaa2b08086";
    private static final String APPLET_B = "wxe3a093ce7278d5b1";
    private static final String APPLET_DOUDOU = "wxa4b24c3ed55e4e18";
    private static final String APPLET_MOOTOO = "wxe86ab49478ec3ee9";
    private static final String APPLET_WENQUE = "wxacbe59cb64cb3ed4";

    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");
    }

    public static String resolveAppletAppIdByAppIdType(String appIdType) {
        if (appIdType == null || appIdType.trim().isEmpty()) {
            return APPLET_DEFAULT;
        }
        switch (appIdType) {
            case APP_ID_TYPE_B:
                return APPLET_B;
            case APP_ID_TYPE_DOUDOU:
                return APPLET_DOUDOU;
            case APP_ID_TYPE_MOOTOO:
                return APPLET_MOOTOO;
            case APP_ID_TYPE_WENQUE:
                return APPLET_WENQUE;
            default:
                return APPLET_DEFAULT;
        }
    }

    public static String resolveAppletAppIdByPaymentType(String paymentType) {
        if (paymentType == null) {
            return APPLET_DEFAULT;
        }
        if (paymentType.contains("APPLETB")) {
            return APPLET_B;
        }
        if (paymentType.contains("DOUDOU")) {
            return APPLET_DOUDOU;
        }
        if (paymentType.contains("MOOTOO")) {
            return APPLET_MOOTOO;
        }
        if (paymentType.contains("WENQUE")) {
            return APPLET_WENQUE;
        }
        return APPLET_DEFAULT;
    }
}
