package com.sonic.cow.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 exception"),
    SYS_PERMISSION_DENIED("0011","Insufficient permissions"),


    GEN_CONTENT_LIMIT_ERROR("","The maximum number of generations has been exceeded within 24 hours. Please try again later"),

    GEN_IMAGE_LIMIT_ERROR("","You can change your avatar at most 10 times within 24 hours. Please try again in %s hours and %s minutes"),

    SOUND_ASR_ERROR("","Audio recognition failed"),

    AI_NOT_EXIST("","AI does not exist"),

    IMAGE_NOT_ILLEGAL("","Image is illegal"),

    USER_BALANCE_INSUFFICIENT("INSUFFICIENT_BALANCE","Your balance is insufficient!"),

    NSWF_ERROR("", "Please remove the following sensitive content: %s"),

    REQUEST_LIMIT_ERROR("", "The requests are too frequent. Please try again later"),


    ;


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