package com.sonic.lion.enums;

import com.sonic.common.exception.BizException;
import com.sonic.common.rpc.ApiResultCode;
import com.sonic.common.utils.MessageUtils;
import lombok.Getter;

import java.text.MessageFormat;

/**
 * @description: 弹窗消息的定义
 * @author: code
 * @create: 2021-12-17 10:25
 **/
@Getter
public enum ToastResultCode implements ApiResultCode {

    SYSTEM_EXCEPTION("0000", "系统异常"),
    IP_EXCEPTION("0000", "系统异常"),

    PARAM_ERROR("0001", "参数异常"),

    PARAM_BLANK("0001", "参数不能为空"),

    /**
     * No third-party account opened
     */
    NO_THIRD_PARTY_ACCOUNT("", "三方账号不存在"),

    /**
     * Unbound account data
     */
    THIRD_PARTY_ACCOUNT_UNBIND("", "三方账户未绑定"),

    DATA_NOT_EXITS("", "数据不存在"),

    OUT_TRADE_NO_EMPTY("", "out trade no empty"),

    DATA_STATUS_INCORRECT("", "数据状态错误"),

    DATA_STATUS_CHANGED("1010", "数据状态已经变更请刷新后再试"),
    /**
     * Subscription platform must be paypal
     */
    PLATFORM_PAYPAL("", "来源必须是paypal"),
    /**
     * The order has been changed
     */
    PAY_TRADE_STATUS_CHANGE("", "交易状态已变更"),

    AMOUNT_LESS_THAN0("", "实付金额不能小于0"),

    AMOUNT_LESS_THAN_N("", "实付金额不能小于N"),

    PAY_TRADE_PAYER_PAYEE_ERROR("", "交易双方不能相同"),

    PAY_TRADE_PAYER_ERROR("", "this trade not for you"),

    PAY_TRADE_PAYMENT_TYPE_ERROR("", "payment type error"),

    /**
     * The subscription must be active
     */
    SUBSCRIPTION_STATUS_ERROR("", "paypal订阅状态错误"),
    /**
     * bind  subscription error user id can't be null
     */
    USER_ID_NOT_NULL("", "绑定用户id不能为空"),
    /**
     * 游戏订单支付失败
     * game order pay fail
     */
    GAME_PAY_FAIL("1008", "game order pay fail"),

    INSUFFICIENT_BALANCE("INSUFFICIENT_BALANCE", "Insufficient balance"),

    INSUFFICIENT_BALANCE_WHEN_ORDER_FAIL("1008","Insufficient balance"),

    GOOGLE_TICKET_STATUS_ERROR("", "Incorrect ticket status"),

    REVEIVE_FAIL("", "没有达到领取条件"),

    REWARD_REVEIVED("", "已经被领取"),

    IAP_RECEIPT_STATUS_ERROR("", "收据不是成功状态"),

    IAP_BUNDLE_ID_ERROR("", "收据应用ID对应不上"),

    DUPLICATE_ORDER_NUMBER("", "Duplicate order number"),

    /**
     * UNSUPPORTED_BUSINESS_TYPE
     */
    UNSUPPORTED_BUSINESS_TYPE("", "不支持的业务类型"),

    IN_PROCESSING("1005", "tradeHandle processing. Please refresh and try again later"),

    RESOURCEKEY_IS_BLANK("", "resourceKey is blank"),

    RESOURCENUM_EXCEEDS_THE_MAXIMUM("", "resourceNum exceeds the maximum"),

    TRADE_NO_BLANK("", "tradeNo can not be"),

    TURN_ON_OFF("", "开关未开启"),

    KEY_ERROR("", "秘钥错误"),

    AUTH_ERROR("", ""),

    LIMIT_ERROR("", ""),

    CHARGE_CANCEL("2001", ""),
    CHARGE_EXPIRED("2002", ""),
    CHARGE_FAIL("2003", ""),

    SYS_PERMISSION_DENIED("权限不足", ""),

    CHANNEL_BLACK_ERROR("", "The current payment method is not supported for payment. Please use another payment method"),

    AIRWALLEX_CREATE_USER_ERROR("10044001", ""),

    CHANNEL_NOT_OPEN("", "Channel not open"),

    SUB_PRODUCT_NOT_FOUND("", "订阅商品不存在"),

    SUBSCRIBED_NO_DUPLICATE("", "订阅中，不允许重复订阅"),

    ;


    private final String errorCode;
    private final String errorMsg;

    ToastResultCode(String errorMsg) {
        this.errorCode = "";
        this.errorMsg = errorMsg;
    }

    ToastResultCode(String errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 得到 message 的值.
     *
     * @return 属性 message 的值.
     */
    @Override
    public String getErrorMsg() {
        return errorMsg ;//+ "(" + getAppId() + ")";
    }

    /**
     * @return 当前服务的AppId
     */
    @Override
    public String getAppId() {
        return "1004";
    }

    /**
     * 校验方法
     *
     * @param expect
     */
    public void check(boolean expect) {
        if (expect) {
            throw new BizException(this.getErrorCode(), MessageUtils.get(this.name()));
        }
    }

    public void check(boolean expect,String format) {
        if (expect) {
            throw new BizException(this.getErrorCode(),  MessageFormat.format( MessageUtils.get(this.name()),format));
        }
    }

    public void check(boolean expect, String errorCode, String message) {
        if (expect) {
            throw new BizException(errorCode, message);
        }
    }
}
