package cn.axzo.msg.center.common.utils;

import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.basics.common.util.AssertUtil;
import cn.axzo.framework.domain.web.result.ApiListResult;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.azxo.framework.common.model.CommonResponse;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author yanglin
 */
@Slf4j
public class BizAssertions {

    /**
     * 断言为NULL
     */
    public static void assertNull(Object actual, String message, Object... args) {
        AssertUtil.isNull(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言不为NULL
     */
    public static void assertNotNull(Object actual, String message, Object... args) {
        AssertUtil.notNull(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言集合不为空
     */
    public static void assertNotEmpty(Collection<?> actual, String message, Object... args) {
        AssertUtil.notEmpty(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言数组不为空
     */
    public static <T> void assertNotEmpty(T[] actual, String message, Object... args) {
        AssertUtil.notEmpty(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言值为真
     */
    public static  void assertTrue(boolean actual, String message, Object... args) {
        AssertUtil.isTrue(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言值不为真
     */
    public static  void assertFalse(boolean actual, String message, Object... args) {
        AssertUtil.isFalse(actual, MessageFormatter.arrayFormat(message, args).getMessage());
    }

    /**
     * 断言值2个值是否equals
     */
    public static  void assertEquals(Object expected, Object actual, String message, Object... args) {
        if (!Objects.equals(expected, actual)) {
            throw new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
        }
    }

    /**
     * 断言值2个值是否不equals
     */
    public static  void assertNotEquals(Object expected, Object actual, String message, Object... args) {
        if (Objects.equals(expected, actual)) {
            throw new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
        }
    }

    public static void assertNotBlank(String content, String message, Object... args) {
        if (StringUtils.isBlank(content)) {
            throw new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
        }
    }

    public static void assertBlank(String content, String message, Object... args) {
        if (StringUtils.isNotBlank(content)) {
            throw new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
        }
    }

    public static <T> T assertResponse(CommonResponse<T> response) {
        return assertResponse(response, "error resp={}", JSON.toJSONString(response));
    }

    public static <T> T assertResponse(CommonResponse<T> response, String message, Object... args) {
        if (response == null || response.getCode() != HttpStatus.HTTP_OK) {
            ServiceException e = new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
            log.warn("remote call response with error", e);
            throw e;
        }
        return response.getData();
    }

    public static <T> T assertResponse(ApiResult<T> response) {
        return assertResponse(response, "error resp={}", JSON.toJSONString(response));
    }

    public static <T> T assertResponse(ApiResult<T> response, String message, Object... args) {
        if (!response.isSuccess()) {
            ServiceException e = new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
            log.warn("remote call response with error", e);
            throw e;
        }
        return response.getData();
    }

    public static <T> List<T> assertResponse(ApiListResult<T> response) {
        return assertResponse(response, "error resp={}", JSON.toJSONString(response));
    }

    public static <T> List<T> assertResponse(ApiListResult<T> response, String message, Object... args) {
        if (!response.isSuccess()) {
            ServiceException e = new ServiceException(MessageFormatter.arrayFormat(message, args).getMessage());
            log.warn("remote call response with error", e);
            throw e;
        }
        return response.getData();
    }

}