package com.usoftchina.uas.office.dto;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Pro1
 * @date 2017/6/23.
 */
public class Result<T> {

    private boolean success;

    private Integer code;

    private String message;

    private T data;

    public boolean isSuccess() {
        return success;
    }

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

    public Integer getCode() {
        return code;
    }

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

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

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

    public static Result success() {
        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

    public static <T> Result success(T data) {
        Result<T> result = success();
        result.setData(data);
        return result;
    }

    public static Result error() {
        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

    public static Result error(String message) {
        Result result = error();
        result.setMessage(message);
        return result;
    }

    public static Result error(int code, String message) {
        Result result = error();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static Result error(int code, String message, Object... args) {
        Result result = error();
        result.setCode(code);
        result.setMessage(String.format(message, args));
        return result;
    }

    public static <T> void success(HttpServletResponse response, T data) throws IOException {
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString());
        PrintWriter printWriter = response.getWriter();
        printWriter.append(JSON.toJSONString(success(data)));
        printWriter.flush();
        printWriter.close();
    }

    public static <T> void error(HttpServletResponse response, int code, String message) throws IOException{
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString());
        PrintWriter printWriter = response.getWriter();
        printWriter.append(JSON.toJSONString(error(code, message)));
        printWriter.flush();
        printWriter.close();
    }

    public static <T> void error(HttpServletResponse response, String message) throws IOException {
        error(response, HttpStatus.BAD_REQUEST.value(), message);
    }

    public static <T> void error(HttpServletResponse response) throws IOException {
        error(response, HttpStatus.BAD_REQUEST.value(), null);
    }
}
