package com.lanyuanxiaoyao.service.web.controller.base;

import java.util.Map;

/**
 * Amis 组件结构化返回值
 *
 * @author lanyuanxiaoyao
 * @date 2022-09-21
 */
public class AmisResponse<T> {
    private static final int SUCCESS_STATUS = 0;
    private static final int ERROR_STATUS = 500;
    private static final String SUCCESS_MESSAGE = "OK";
    private static final String ERROR_MESSAGE = "ERROR";
    private Integer status;
    private String message;
    private T data;

    public static AmisResponse<Object> responseError() {
        AmisResponse<Object> response = new AmisResponse<>();
        response.setStatus(ERROR_STATUS);
        response.setMessage(ERROR_MESSAGE);
        return response;
    }

    public static AmisResponse<Object> responseError(String message) {
        AmisResponse<Object> response = new AmisResponse<>();
        response.setStatus(ERROR_STATUS);
        response.setMessage(message);
        return response;
    }

    public static AmisResponse<Object> responseSuccess() {
        AmisResponse<Object> response = new AmisResponse<>();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(SUCCESS_MESSAGE);
        return response;
    }

    public static AmisResponse<Object> responseSuccess(String message) {
        AmisResponse<Object> response = new AmisResponse<>();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(message);
        return response;
    }

    public static <E> AmisResponse<E> responseSuccess(String message, E data) {
        AmisResponse<E> response = new AmisResponse<>();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static <E> AmisResponse<E> responseSuccess(E data) {
        AmisResponse<E> response = new AmisResponse<>();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(SUCCESS_MESSAGE);
        response.setData(data);
        return response;
    }

    public static AmisMapResponse responseMapData() {
        AmisMapResponse response = new AmisMapResponse();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(SUCCESS_MESSAGE);
        return response;
    }

    public static AmisMapResponse responseMapData(Map<String, Object> data) {
        AmisMapResponse response = responseMapData();
        response.setData(data);
        return response;
    }

    public static AmisMapResponse responseMapData(String key, Object value) {
        AmisMapResponse response = responseMapData();
        response.setData(key, value);
        return response;
    }

    public static AmisCrudResponse responseCrudData(Iterable<?> data) {
        AmisCrudResponse response = new AmisCrudResponse();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(SUCCESS_MESSAGE);
        response.setData(data);
        return response;
    }

    public static AmisCrudResponse responseCrudData(Iterable<?> data, Integer total) {
        AmisCrudResponse response = responseCrudData(data);
        response.setTotal(total);
        return response;
    }

    public static AmisCrudResponse responseCrudData(Iterable<?> data, Long total) {
        AmisCrudResponse response = responseCrudData(data);
        response.setTotal(total);
        return response;
    }

    public static AmisDetailResponse responseDetailData(Object detail) {
        AmisDetailResponse response = new AmisDetailResponse();
        response.setDetail(detail);
        return response;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

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

    public T getData() {
        return data;
    }

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

    @Override
    public String toString() {
        return "AmisResponse{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
