package com.sonic.pigeon.enums;

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

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

    /**
     * 可以在此处扩展服务自身需要用到的错误码信息
     */
    SYS_PARAMETERS_VALIDATE_EXCEPTION("10010003", "参数验证错误"),
    SYS_VALIDATION_FAILED_ERROR("1001011", "验证失败，无效请求"),

    ACCOUNT_NOT_REGISTER("-1", "Incorrect username or password"),
    FACEBOOK_ACCOUNT_ERROR("-1", "Facebook account could not be verified"),
    FACEBOOK_INVALID("-1", "Facebook login is invalid, please login again"),

    APPLE_NETWORK_ERROR("10000001", "apple network error"),

    MISS_PARAM_ERROR("1001010", "Missing parameter"),

    DISCORD_NETWORK_ERROR("10000001", "discord network error"),
    GOOGLE_ID_GET_ERROR("10010151", "GOOGLE ID GET ERROR"),

    AUTH_FAIL("10010151", "授权失败"),

    DEVICE_BLOCK_ERROR("10010152", "该设备已被封禁"),

    FREEZE_ERROR("10010153", "账号已被冻结"),

    ;


    private final String errorCode;
    private final String errorMsg;

    BizResultCode(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 "1002";
    }

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

    /**
     * 校验方法
     *
     * @param expect
     * @param code
     */
    public void check(boolean expect, String code) {
        if (expect) {
            String message = MessageUtils.get(this.name());
            throw new BizException(code, this.name().equals(message) ? this.getErrorMsg() : message);

        }
    }

    /**
     * 校验方法
     *
     * @param expect
     * @param code
     * @param message
     */
    public void check(boolean expect, String code, String message) {
        if (expect) {
            throw new BizException(code, message);
        }
    }
}
