package com.younike.common.bean;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * @author OldMa
 * @version 1.0.0
 * @ClassName ApiResultCode.java
 * @Description TODO
 * @createTime 2021年06月10日 13:45:00
 */
public class ResultGenerator {


    /**
     * 成功不含参
     * @return
     */
    public static<T> APIResult<T> getSuccessResult() {
        APIResult<T> result = new APIResult<>();
        result.setCode(ApiResultCode.Success.getCode());
        return result;
    }

    /**
     * 成功不含参含成功信息
     * @return
     */
    public static<T> APIResult<T> getSuccessmsg(String msg) {
        APIResult result = new APIResult<>();
        result.setCode(ApiResultCode.Success.getCode());
        result.setMsg(msg);
        return result;
    }

    /**
     * 成功含参
     * @param data
     * @return
     */
    public static<T> APIResult getSuccessResult(Object data) {
        APIResult result = new APIResult<>();
        result.setCode(ApiResultCode.Success.getCode());
        result.setResult(data);
        return result;
    }

    /**
     * 成功含参
     * @param msg
     * @return
     */
    public static<T> APIResult getSuccessResult(int code ,String msg) {
        APIResult result = new APIResult<>();
        result.setCode(ApiResultCode.Success.getCode());
        result.setMsg(msg);
        return result;
    }

    /**
     * 成功含参
     * @param data
     * @return
     */
    public static APIResult getSuccessResult(Object data, String msg) {
        APIResult result = new APIResult<>();
        result.setCode(ApiResultCode.Success.getCode());
        result.setMsg(msg);
        result.setResult(data);
        return result;
    }

    /**
     * 失败不含参
     * @return
     */
    public static<T> APIResult<T> getFailResult() {
        APIResult<T> result = new APIResult<>();
        result.setCode(ApiResultCode.Fail.getCode());
        return result;
    }

    /**
     * 失败含msg
     * @return
     */
    public static<T> APIResult<T> getFailResult(String msg) {
        APIResult result = new APIResult();
        result.setMsg(msg);
        result.setCode(ApiResultCode.Fail.getCode());
        return result;
    }

    /**
     * 失败含msg
     * @return
     */
    public static<T> APIResult<T> getFailResult(Integer code, String msg) {
        APIResult result = new APIResult();
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    /**
·     * 失败含[data & msg]
     * @return
     */
    public static<T> APIResult<T> getFailResult(Object data, String msg) {
        APIResult result = new APIResult();
        result.setResult(data);
        result.setMsg(msg);
        result.setCode(ApiResultCode.Fail.getCode());
        return result;
    }


    public static<T> APIResult<T> getResultFromYouanju(ResponseEntity<JSONObject> responseEntity) {
        if (responseEntity.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
            APIResult objectAPIResult = new APIResult<>();
            objectAPIResult.setCode(ApiResultCode.Unauthorized.getCode());
            objectAPIResult.setMsg("请重新登录");

            return objectAPIResult;
        }

        JSONObject jsonObject = responseEntity.getBody();
        if (jsonObject == null) {
            return ResultGenerator.getFailResult();
        }
        if ("SUCCESS".equals(jsonObject.get("result"))) {
            APIResult info = ResultGenerator.getSuccessResult(jsonObject.get("info"));
            if (jsonObject.get("message") != null) {
                info.setMsg(jsonObject.get("message").toString());
            }
            return info;
        } else {
            APIResult failResult = ResultGenerator.getFailResult();
            if (jsonObject.get("message") != null) {
                failResult.setMsg(jsonObject.get("message").toString());
            }
            if (jsonObject.get("info") != null) {
                failResult.setResult(jsonObject.get("info"));
            }
            return failResult;
        }
    }
}
