package com.miya.fastcashier.net;

import android.util.Log;

import com.miya.fastcashier.BaseApplication;
import com.miya.fastcashier.R;
import com.miya.fastcashier.utils.BaseFunctionKt;

import java.net.SocketTimeoutException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 类描述：通用请求返回
 * 创建人：zpxiang
 * 创建时间：
 * 修改人：
 * 修改时间：
 */
public abstract class CommonCallback<T> implements Callback<BaseResponse<T>> {


    private static final String RESPONSE_MSG_UNKNOW = "未知异常";
    private static final String CALLBACK_SUCCEED_CODE = "200";

    interface ErrorType {
        int ERROR_TYPE_CUSTOM = 0;
        int ERROR_TYPE_HTTP = 1;
        int ERROR_TYPE_EXCEPTION = 2;
    }

    interface ResponseCode {
        String RESPONSE_CODE_FAILED = String.valueOf(-1); // 返回数据失败
        String RESPONSE_CODE_NETWORK_DISCONNECT = String.valueOf(-2); // 无网络
        String RESPONSE_CODE_NETWORK_TIMEOUT = String.valueOf(-3); // 超时
    }

    @Override
    public void onResponse(Call<BaseResponse<T>> call, Response<BaseResponse<T>> response) {
        if (response.isSuccessful() && response.body() != null) {
            String responseCode = response.body().getCode(); // 业务自定义Code
            if (CALLBACK_SUCCEED_CODE.equals(responseCode)) {
                onSuccess(response.body().getData());
            } else {
                onFailure(ErrorType.ERROR_TYPE_CUSTOM,
                        responseCode,
                        response.body().getError());
            }
        } else if (response.errorBody() != null) {
            onFailure(ErrorType.ERROR_TYPE_HTTP,
                    String.valueOf(response.code()),
                    response.errorBody().toString());
        } else {
            onFailure(ErrorType.ERROR_TYPE_HTTP,
                    ResponseCode.RESPONSE_CODE_FAILED,
                    RESPONSE_MSG_UNKNOW); // 理论上不会存在此种情况
        }
    }

    @Override
    public void onFailure(Call<BaseResponse<T>> call, Throwable t) {
        String temp = t.getMessage();
        Log.e("#### HttpLog", "请求失败：" + temp);
        String errorMessage;
        if (!BaseFunctionKt.isNetworkConnected(BaseApplication.Companion.getApplication())) {
            errorMessage = BaseApplication.Companion.getApplication().getString(R.string.common_prompt_network_error);
            onFailure(ErrorType.ERROR_TYPE_EXCEPTION, ResponseCode.RESPONSE_CODE_NETWORK_DISCONNECT, errorMessage);
            return;
        }
        if (t instanceof SocketTimeoutException) {
            errorMessage = BaseApplication.Companion.getApplication().getString(R.string.common_prompt_network_timeout);
            onFailure(ErrorType.ERROR_TYPE_EXCEPTION, ResponseCode.RESPONSE_CODE_NETWORK_TIMEOUT, errorMessage);
        } else {
            errorMessage = BaseApplication.Companion.getApplication().getString(R.string.common_prompt_data_error);
            onFailure(ErrorType.ERROR_TYPE_EXCEPTION, ResponseCode.RESPONSE_CODE_FAILED, errorMessage);
        }

    }

    public abstract void onSuccess(T data);

    public abstract void onFailure(int errorType, String errorCode, String errorMessage);
}
