package com.jay.monitor.data.core.utils;

import com.jay.monitor.data.core.enums.CommonErrors;
import com.jay.monitor.data.core.enums.EnumService;
import com.jay.monitor.data.core.exception.BusinessException;
import com.jay.monitor.data.core.model.response.*;

import java.util.List;

/**
 * @author zhub
 * @create 2018/7/8 16:23
 * @email zhub1@elab-plus.com
 **/
public class ResponseUtils {

    static public <Response extends ResponseCommonModel> Response result(boolean success, String code, String message, Response response) {
        response.setMessage(message);
        response.setErrorCode(code);
        response.setSuccess(success);
        return response;
    }

    /**
     * 参数错误
     *
     * @param param
     * @param response
     * @param <Response>
     * @return
     */
    static public <Response extends ResponseCommonModel> Response paramErrorResult(String param, Response response) {
        response.setMessage(String.format(CommonErrors.COMMON_PARAM_ERROR.getMessage(), param));
        response.setErrorCode(CommonErrors.COMMON_PARAM_ERROR.getCode());
        response.setSuccess(false);
        return response;
    }

    /**
     * 错误返回
     *
     * @param info 枚举的key
     * @return
     */
    public static ObjectResponseModel<?> falseObject(Enum<? extends EnumService> info) {
        ObjectResponseModel responseCommonModel = new ObjectResponseModel();
        setData(info, responseCommonModel);
        return responseCommonModel;
    }

    /**
     * 返回公共参数
     *
     * @param info
     * @return
     */
    public static ResponseCommonModel falses(Enum<? extends EnumService> info) {
        ResponseCommonModel responseCommonModel = new ResponseCommonModel();
        setData(info, responseCommonModel);
        return responseCommonModel;
    }

    /**
     * 统一设置参数
     *
     * @param info
     * @param responseCommonModel
     */
    private static void setData(Enum<? extends EnumService> info, ResponseCommonModel responseCommonModel) {
        String msg = ((EnumService) info).getMessage();
        String code = ((EnumService) info).getCode();
        responseCommonModel.setSuccess(false);
        responseCommonModel.setErrorCode(code);
        responseCommonModel.setMessage(msg);
    }

    /**
     * 错误返回 -> List
     *
     * @param info 枚举类的key
     * @return
     */
    public static ListResponseModel<?> falseList(Enum<? extends EnumService> info) {
        ListResponseModel responseCommonModel = new ListResponseModel();
        setData(info, responseCommonModel);
        return responseCommonModel;
    }

    /**
     * 错误分页对象返回
     *
     * @param info
     * @return
     */
    public static PageResponseModel<?> falsePage(Enum<? extends EnumService> info) {
        PageResponseModel responseCommonModel = new PageResponseModel();
        setData(info, responseCommonModel);
        return responseCommonModel;
    }


    public static ObjectResponseModel trues() {
        ObjectResponseModel responseCommonModel = new ObjectResponseModel();
        responseCommonModel.setSuccess(true);
        return responseCommonModel;
    }

    /**
     * 处理正常并且返回对象
     *
     * @param obj
     * @return
     */
    public static ObjectResponseModel trueObj(Object obj) {
        ObjectResponseModel responseCommonModel = new ObjectResponseModel();
        responseCommonModel.setSuccess(true);
        responseCommonModel.setSingle(obj);
        return responseCommonModel;
    }

    /**
     * 返回结果正常包装并且返回集合
     *
     * @param list
     * @return
     */
    public static ListResponseModel trueList(List<?> list) {
        ListResponseModel listResponseModel = new ListResponseModel();
        listResponseModel.setSuccess(true);
        listResponseModel.setList(list);
        return listResponseModel;
    }

    /**
     * 返回结果正常并且返回分页对象
     *
     * @param pageInfoModel
     * @return
     */
    public static PageResponseModel<?> truePage(PageInfoModel<?> pageInfoModel) {
        PageResponseModel pageResponseModel = new PageResponseModel();
        pageResponseModel.setSuccess(true);
        pageResponseModel.setPageModel(pageInfoModel);
        return pageResponseModel;
    }


    /**
     * 如果该对象为空,则抛出异常
     *
     * @param obj
     * @param info
     */
    public static void checkNull(Object obj, Enum<? extends EnumService> info) {
        if (obj == null || obj.toString().trim().equals("")) {
            businessException(info);
        }
    }

    /**
     * 判断字段是否为空
     *
     * @param obj   字段对象
     * @param field 字段名称
     */
    public static void checkParamNull(Object obj, String field) {
        if (obj == null || obj.toString().trim().equals("")) {
            businessParamsException(CommonErrors.COMMON_PARAM_ERROR, field);
        }
    }

    /**
     * 判断字段是否为空，如果为空则异常返回
     *
     * @param info  枚举值
     * @param field 字段名称
     */
    private static void businessParamsException(Enum<? extends EnumService> info, String field) {
        String msg = ((EnumService) info).getMessage();
        String code = ((EnumService) info).getCode();
        throw new BusinessException(code, String.format(msg, field));
    }


    private static void businessException(Enum<? extends EnumService> info) {
        String msg = ((EnumService) info).getMessage();
        String code = ((EnumService) info).getCode();
        throw new BusinessException(code, msg);
    }

    /**
     * 如果条件不匹配,则抛出异常
     *
     * @param bool
     * @param info
     */
    public static void check(boolean bool, Enum<? extends EnumService> info) {
        if (!bool) {
            businessException(info);
        }
    }


    public static void main(String[] args) {

    }

}