package com.sonic.shark.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 {
    NO_OPERATION_AUTH("No operation authority"),

    NETWORK_FAILURE("Network failure"),
    SYSTEM_EXCEPTION("Internal error, please try again later"),

    SYS_OPERATOR_24_HOUR_ERROR("operate too frequently，please try again 24h later"),

    SYS_BIZ_UPGRADE_ERROR("Feature adjusted, check for update in the App/Play store"),


    /**
     * 文件上传相关
     */
    PICTURES_VALIDATE_ERROR("NSFW materials have been detected, please upload other photos"),

    /**
     * 图片鉴黄
     */
    FILE_NOT_EXISTS_ERROR("File does not exist"),
    FILE_MAX_SIZE_ERROR("File exceeds maximum limit"),
    FILE_TYPE_ERROR("File type does not exist"),
    INVALID_REQUEST_IMAGE_ADDRESS("Invalid request image address"),
    STS_BIZ_TYPE_ERROR("wrong business type"),

    ;


    private final String errorCode;
    private final String errorMsg;

    ToastResultCode(String errorMsg) {
        this.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
     */
    public void check(boolean expect, String msg) {
        if (expect) {
            throw new BizException(this.getErrorCode(), StringUtils.isNotEmpty(msg) ? msg : this.getErrorMsg());
        }
    }
}
