package com.qmth.teachcloud.exchange.common.util;

import cn.hutool.http.HttpStatus;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.StringJoiner;

/**
 * @Description: 自定义消息工具
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2019/10/11
 */
public class ResultUtil {
    private final static Logger log = LoggerFactory.getLogger(ResultUtil.class);

    public static Result success(Object object) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), ExceptionResultEnum.SUCCESS.getMessage(), object);
    }

    public static Result success() {
        return success(null);
    }

    public static Result error(int code, String message) {
        return new Result(code, message);
    }

    public static Result error() {
        try {
            StringJoiner stringJoiner = new StringJoiner("");
            HttpServletRequest request = ServletUtil.getRequest();
            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getStatus().value())).add("\r\n");
            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getCode())).add("\r\n");
            stringJoiner.add("[message]:").add(ExceptionResultEnum.EXCEPTION_ERROR.getMessage());
            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
        } catch (Exception e) {
        }
        throw ExceptionResultEnum.EXCEPTION_ERROR.exception();
    }

    public static Result error(ApiException e, String message) {
        try {
            StringJoiner stringJoiner = new StringJoiner("");
            HttpServletRequest request = ServletUtil.getRequest();
            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
            stringJoiner.add("[status]:").add(String.valueOf(e.getStatus().value())).add("\r\n");
            stringJoiner.add("[code]:").add(String.valueOf(e.getCode())).add("\r\n");
            stringJoiner.add("[message]:").add(message);
            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
        } catch (Exception e1) {
        }
        throw new ApiException(e.getStatus(), e.getCode(), message, message);
    }

    public static Result error(ExceptionResultEnum e, int code, String message) {
        try {
            StringJoiner stringJoiner = new StringJoiner("");
            HttpServletRequest request = ServletUtil.getRequest();
            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
            stringJoiner.add("[status]:").add(String.valueOf(e.getStatus().value())).add("\r\n");
            stringJoiner.add("[code]:").add(String.valueOf(code)).add("\r\n");
            stringJoiner.add("[message]:").add(message);
            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
        } catch (Exception e1) {
        }
        throw e.exception(code, message);
    }

    public static Result error(String message) {
        try {
            StringJoiner stringJoiner = new StringJoiner("");
            HttpServletRequest request = ServletUtil.getRequest();
            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.ERROR.getStatus().value())).add("\r\n");
            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.ERROR.getCode())).add("\r\n");
            stringJoiner.add("[message]:").add(message);
            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
        } catch (Exception e) {
        }
        throw ExceptionResultEnum.ERROR.exception(message);
    }

    public static Result ok(String message) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), message);
    }

    public static Result ok(Object data) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), data, ExceptionResultEnum.SUCCESS.getMessage());
    }

    public static Result ok(boolean success) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), Collections.singletonMap(SystemConstant.SUCCESS, success), ExceptionResultEnum.SUCCESS.getMessage());
    }

    public static Result ok(Object data, String message) {
        return new Result(ExceptionResultEnum.SUCCESS.getStatus().value(), data, message);
    }

    public static Result ok() {
        return new Result(HttpStatus.HTTP_OK, Collections.singletonMap(SystemConstant.UPDATE_TIME, System.currentTimeMillis()), ExceptionResultEnum.SUCCESS.getMessage());
    }
}
