package com.sonic.frog.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 {
    SYS_SYSTEM_EXCEPTION("", "System error"),

    SYS_PARAMETERS_VALIDATE_EXCEPTION("-1", "sys.parameters.validate.exception"),

    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"),
    ACCOUNT_DOES_NOT_EXIST("The account does not exist"),

    AGE_CHECK("0010", "User age cannot be less than 18 years old"),

    USER_NICKNAME_EXIST("0010", "User nickname already exists"),

    SYS_PERMISSION_DENIED("0011", "Insufficient permissions"),

    AI_USER_NOT_EXIST("0012", "AI user does not exist"),

    ALBUM_IS_DELETED("0012", "Image has been deleted"),
    ALBUM_NOT_EXIST("0013", "Image does not exist"),
    ALBUM_DEFAULT_CAN_NOT_DELETE("0014", "Default image cannot be deleted"),
    ALBUM_ENCRYPT_CAN_NOT_LIKE("", "Encrypted image cannot be liked"),
    ALBUM_DEFAULT_CAN_NOT_MODIFY_PAY("", "Default image cannot be changed to paid"),
    ALBUM_ADD_MAX_ONE("", "Can add up to one image"),

    GEN_IMAGE_LIMIT_ERROR("", "Maximum of 10 image changes within 24 hours, please try again in %s hours and %s minutes"),

    CHAT_BUBBLE_NOT_EXIST("0013", "Chat bubble does not exist"),
    BACKGROUND_IS_DELETED("0014", "Background has been deleted"),
    BACKGROUND_NOT_EXIST("0015", "Background does not exist"),
    BACKGROUND_ADD_MAX_ONE("", "Can add up to one background"),

    GIFT_NOT_EXIST("0016", "Gift does not exist"),
    NOT_MEMBER_CREATE_AI_ONE("017", "Non-members can only create 1 AI"),
    MEMBER_CREATE_AI_MAX_NUM("018", "Members can create up to 5 AIs"),

    USER_CREATE_COUNT_NONE("019", "No creation attempts remaining"),

    PARAM_NOT_NULL("0020", "Parameter cannot be empty"),
    PARAM_LEN_MIN_ERROR("0021", "Length is insufficient"),


    ;


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