package com.sundot.airport.exam.common;

import com.sundot.airport.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class HttpResult<T> implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(HttpResult.class);
    private static final int ERROR_CODE = -1;
    private static final String ERROR_MESSAGE = "系统繁忙，请稍候重试";
    private static final String BUSINESS_EXCEPTION = "捕获到业务异常流：{0}";
    private static final String ERROR = "捕获到异常：{0}";
    private boolean success;
    private String message = "";
    private int code = 200;
    private T model;

    public HttpResult() {
        this.success = false;
    }

    public HttpResult(Integer code, String message, Boolean success) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public HttpResult(Integer code, String message, Boolean success, T model) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.model = model;
    }

    public HttpResult(AbstractBaseExceptionEnum exception, Boolean success) {
        this.success = success;
        this.code = exception.getCode();
        this.message = exception.getMessage();
    }

    public HttpResult(T model) {
        this.success = true;
        this.model = model;
    }

    public static <T> HttpResult<T> error() {
        return new HttpResult<>(ERROR_CODE, ERROR_MESSAGE, false);
    }

    public static <T> HttpResult<T> error(String message) {
        return new HttpResult<>(ERROR_CODE, message, false);
    }

    public static <T> HttpResult<T> error(String message, T model) {
        return new HttpResult<>(ERROR_CODE, message, false, model);
    }

    public static <T> HttpResult<T> error(int code, String message) {
        return new HttpResult<>(code, message, false);
    }

    public static <T> HttpResult<T> error(AbstractBaseExceptionEnum exception, T model) {
        HttpResult<T> result = error(exception);
        result.setModel(model);
        return result;
    }

    public static <T> HttpResult<T> error(AbstractBaseExceptionEnum exception) {
        return new HttpResult<>(exception.getCode(), exception.getMessage(), false);
    }

    public static <T> HttpResult<T> success(T model) {
        return new HttpResult<>(model);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }

    public void fromException(Exception exp) {
        if (exp instanceof ServiceException) {
            logger.debug(BUSINESS_EXCEPTION, exp);

            setSuccess(false);
            setMessage(exp.getMessage());
            setCode(((ServiceException) (exp)).getCode());
            throw new ServiceException(exp.getMessage(), this.code);
        } else {
            logger.error(ERROR, exp);
            setSuccess(false);
            setMessage(ERROR_MESSAGE);
            setCode(ERROR_CODE);
            throw new RuntimeException(exp.getMessage());
        }
    }

}