package com.eshore.gringotts.web.configuration.amis;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * Amis 组件结构化返回值
 *
 * @author lanyuanxiaoyao
 * @date 2022-09-21
 */
@ToString
@Getter
@Setter
public class AmisResponse<T> {
    private static final int SUCCESS_STATUS = 0;
    private static final int ERROR_STATUS = 500;
    private static final String SUCCESS_MESSAGE = "成功";
    private static final String ERROR_MESSAGE = "错误";
    private Integer status;
    @JsonProperty("msg")
    private String message;
    private T data;

    public static AmisResponse<Object> responseError() {
        return responseError(ERROR_MESSAGE);
    }

    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() {
        return responseSuccess(SUCCESS_MESSAGE);
    }

    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) {
        return responseSuccess(SUCCESS_MESSAGE, data);
    }

    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 AmisListResponse responseListData(Iterable<?> data) {
        AmisListResponse response = new AmisListResponse();
        response.setStatus(SUCCESS_STATUS);
        response.setMessage(SUCCESS_MESSAGE);
        response.setData(data);
        return response;
    }

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

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

    public static AmisItemResponse responseItemData(Object detail) {
        AmisItemResponse response = new AmisItemResponse();
        response.setDetail(detail);
        return response;
    }
}
