package com.sonic.bear.enums;

import com.sonic.common.exception.BizException;
import com.sonic.common.rpc.ApiResultCode;
import com.sonic.common.utils.MessageUtils;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

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

    /** 可以在此处扩展服务自身需要用到的错误码信息 */
    BIZ_ERROR1("-1", "biz error"),
    NO_LOGIN("0001", "User is not logged in"),
    USER_NOT_EXIST("0002", "User does not exist"),
    APP_CLIENT_NOT_EXIST("0003", "Login client does not exist"),
    APP_CLIENT_NOT_ALLOW("0004", "Not authorized to log in to the client"),
    SESSION_EXPIRED("0005", "Session is expired"),
    SESSION_INVALID("0006", "Session is invalid"),
    PASSWORD_INVALID("0007", "Invalid account or password"),
    /** 第三方登陆授权失败 */
    AUTH_FAIL("0008","auth.fail"),
    USER_FROZEN("0009", "User is frozen"),

    ;


    private final String errorCode;
    private final String errorMsg;

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

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

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

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

    /**
     * 校验方法
     *
     * @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
     */
    public void check(boolean expect, String msg) {
        if (expect) {
            if(StringUtils.isNotBlank(msg)) {
                throw new BizException(this.getErrorCode(), msg);

            } else {
                String message = MessageUtils.get(this.name());
                throw new BizException(this.getErrorCode(), this.name().equals(message) ? this.getErrorMsg() : message);
            }
        }
    }
}
