package com.copy.trade.util;


import com.copy.trade.exception.ServiceException;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @author quzhimin
 * @date 2018/12/31 17:43
 */
public abstract class WebAssert {

    /**
     * 用于参数验证的场景
     *
     * @param expression
     * @param code
     * @param args
     */
    public static void isTrue(boolean expression, String code, Object... args) {
        if (!expression) {
            throwException(code, args);
        }
    }

    /**
     * 用于参数验证的场景
     *
     * @param obj
     * @param code
     * @param args
     */
    public static void isNotBlank(String obj, String code, Object... args) {
        isTrue(StringUtils.hasText(obj), code, args);
    }

    /**
     * 用于参数验证的场景
     *
     * @param obj
     * @param code
     * @param args
     */
    public static void isBlank(String obj, String code, Object... args) {
        isTrue(!StringUtils.hasText(obj), code, args);
    }

    /**
     * 用于业务是否匹配的判断
     *
     * @param obj
     * @param code
     * @param args
     */
    public static void isNull(Object obj, String code, Object... args) {
        if (obj != null) {
            throwException(code, args);
        }
    }

    /**
     * 用于业务是否匹配的判断
     *
     * @param obj
     * @param code
     * @param args
     */
    public static void isNotNull(Object obj, String code, Object... args) {
        if (obj == null) {
            throwException(code, args);
        }
    }

    /**
     * 用于业务是否匹配的判断
     *
     * @param result
     * @param code
     * @param args
     */
    public static void expectGt0(int result, String code, Object... args) {
        if (result < 1) {
            throwException(code, args);
        }
    }

    public static void expectGte0(int result, String code, Object... args) {
        if (result < 0) {
            throwException(code, args);
        }
    }

    public static void expectLte0(int result, String code, Object... args) {
        if (result > 0) {
            throwException(code, args);
        }
    }

    public static void expectLt0(int result, String code, Object... args) {
        if (result > -1) {
            throwException(code, args);
        }
    }

    /**
     * 用于业务是否匹配的判断
     *
     * @param result
     * @param code
     * @param args
     */
    public static void expectNotGt0(int result, String code, Object... args) {
        if (result > 0) {
            throwException(code, args);
        }
    }

    /**
     * 用于业务是否匹配的判断
     *
     * @param expression
     * @param code
     * @param args
     */
    public static void expect(boolean expression, String code, Object... args) {
        if (!expression) {
            throwException(code, args);
        }
    }


    public static void expectEmpty(Map<?, ?> map, String code, Object... args) {
        if (!CollectionUtils.isEmpty(map)) {
            throwException(code, args);
        }
    }

    public static void expectEmpty(Collection<?> collection, String code, Object... args) {
        if (CollectionUtils.isEmpty(collection)) {
            throwException(code, args);
        }
    }

    public static void expectNotEmpty(Collection<?> collection, String code, Object... args) {
        if (CollectionUtils.isEmpty(collection)) {
            throwException(code, args);
        }
    }

    public static void expectNotEmpty(Map<?, ?> map, String code, Object... args) {
        if (CollectionUtils.isEmpty(map)) {
            throwException(code, args);
        }
    }

    /**
     * 是否登录，
     * @param expression
     * @param code
     * @param args
     */
    public static void hasAuth(boolean expression, String code, Object... args) {
        if (!expression) {
            error(false, HttpStatus.UNAUTHORIZED, code, args);
        }
    }

    /**
     * 用于越权检查，资源归属
     *
     * @param expression
     */
    public static void hasPermission(boolean expression, String code, Object... args) {
        if (!expression) {
            error(false, HttpStatus.FORBIDDEN, code, args);
        }
    }

    public static void error(boolean expression, String code, Object... args) {
        error(expression, HttpStatus.PRECONDITION_FAILED, code, args);
    }

    /**
     * 用于其他系统错误
     * 因为
     * 需要传入具体错误码和错误信息
     *
     * @param expression
     * @param statusCode
     * @param code
     * @param args
     */
    public static void error(boolean expression, HttpStatus statusCode, String code, Object... args) {
        if (!expression) {
            throwException(statusCode.value(), code, args);
        }
    }

    public static void throwException(String code, Object... args) {
        throw new ServiceException(code, args);
    }

    public static void throwException(HttpStatus statusCode, String code, Object... args) {
        throwException(statusCode.value(), code, args);
    }

    public static void throwException(int statusCode, String code, Object... args) {
        throw new ServiceException(statusCode, code, args);
    }
}

