package ai.pai.lensman.box;

import android.support.annotation.NonNull;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Response;

public class Result {

    protected boolean success;

    public boolean isSuccess() {
        return success;
    }

    public static class Data<T> extends Result {

        private T data;

        public T getData() {
            return data;
        }

    }

    public static class Error extends Result {

        @SerializedName("error_msg")
        private String errorMessage;

        public String getErrorMessage() {
            return errorMessage;
        }

    }

    public static <T extends Result> Error buildError(@NonNull Response<T> response) {
        try {
            return ApiClient.gson.fromJson(response.errorBody().string(), Error.class);
        } catch (IOException | JsonSyntaxException e) {
            Error error = new Error();
            error.success = false;
            switch (response.code()) {
                case 400:
                    error.errorMessage = "请求参数有误";
                    break;
                case 403:
                    error.errorMessage = "请求被拒绝";
                    break;
                case 404:
                    error.errorMessage = "资源未找到";
                    break;
                case 405:
                    error.errorMessage = "请求方式不被允许";
                    break;
                case 408:
                    error.errorMessage = "请求超时";
                    break;
                case 422:
                    error.errorMessage = "请求语义错误";
                    break;
                case 500:
                    error.errorMessage = "服务器逻辑错误";
                    break;
                case 502:
                    error.errorMessage = "服务器网关错误";
                    break;
                case 504:
                    error.errorMessage = "服务器网关超时";
                    break;
                default:
                    error.errorMessage = response.message();
                    break;
            }
            return error;
        }
    }

    public static Error buildError(@NonNull Throwable t) {
        Error error = new Error();
        error.success = false;
        if (t instanceof UnknownHostException || t instanceof ConnectException) {
            error.errorMessage = "网络无法连接";
        } else if (t instanceof SocketTimeoutException) {
            error.errorMessage = "网络访问超时";
        } else if (t instanceof JsonSyntaxException) {
            error.errorMessage = "响应数据格式错误";
        } else {
            error.errorMessage = "未知错误：" + t.getLocalizedMessage();
        }
        return error;
    }


}
