package com.colorful.keyboard.theme.net;

import android.content.Context;
import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

public abstract class ApiCallbackCommon <M> extends Subscriber<M> {
    //对应HTTP的状态码
    private static final int    UNAUTHORIZED          = 401;
    private static final int    FORBIDDEN             = 403;
    private static final int    NOT_FOUND             = 404;
    private static final int    REQUEST_TIMEOUT       = 408;
    private static final int    INTERNAL_SERVER_ERROR = 500;
    private static final int    BAD_GATEWAY           = 502;
    private static final int    SERVICE_UNAVAILABLE   = 503;
    private static final int    GATEWAY_TIMEOUT       = 504;
    //出错提示
    private String msg                   = "";

    public abstract void onSuccess(M model);

    public abstract void onFailure(ApiException ex);

    private Context context;


    private boolean isShowDialog = true; //默认请求网络加载Dialog

    private boolean isShowReloadLayout = false; //默认会触发 重新加载页面

    public ApiCallbackCommon(Context context) {
        this.context = context;
    }

   /* public ApiCallback(BaseTransparentActivity context, boolean isShowDialog) {
        this.context = context;
        this.isShowDialog = isShowDialog;
    }*/

    public ApiCallbackCommon(Context context, boolean isShowDialog) {
        this.context = context;
        this.isShowDialog = isShowDialog;
    }
    public ApiCallbackCommon(Context context, boolean isShowDialog,boolean isShowReloadLayout) {
        this.context = context;
        this.isShowDialog = isShowDialog;
        this.isShowReloadLayout = isShowReloadLayout;
    }


    @Override
    public void onStart() {
        super.onStart();

        // 显示进度条
        if (null != context) {

            showDialog();

        }

    }



    @Override
    public void onNext(M model) {
        onSuccess(model);
    }

    @Override
    public void onCompleted() {
        //关闭等待进度条
        disDialog();
    }

    @Override
    public void onError(Throwable e) {

        disDialog();

        if (e.getMessage().contains("500")||e.getMessage().contains("503")||e.getMessage().contains("Failed to connect")) {
            //ToastUtils.show("服务器异常");
            if (isShowReloadLayout){
//                context.changePageState(ToolbarActivity.PageState.ERROR);
            }
            onFailure(null);
            return;
        }

        Throwable throwable = e;
        //获取最根源的异常
        while (throwable.getCause() != null) {
            e = throwable;
            throwable = throwable.getCause();
        }

        ApiException ex;
        if (e instanceof HttpException) {             //HTTP错误
            HttpException httpException = (HttpException) e;
            ex = new ApiException(e, httpException.code());
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                    //onPermissionError(ex);          //权限错误，需要实现
                    onFailure(ex);
                    break;
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex.setDisplayMessage(msg);  //均视为网络错误

                    if (isShowReloadLayout){
//                        context.changePageState(ToolbarActivity.PageState.ERROR);
                    }
                    onFailure(ex);
                    break;
            }

        } else if (e instanceof ResultException) {    //服务器返回的错误
            ResultException resultException = (ResultException) e;
            ex = new ApiException(resultException, resultException.getErrCode());
            //onResultError(ex);
            if (isShowReloadLayout){
//                context.changePageState(ToolbarActivity.PageState.ERROR);
            }
            onFailure(ex);

        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ApiException(e, ApiException.PARSE_ERROR);
            ex.setDisplayMessage(msg);            //均视为解析错误

            onFailure(ex);
        } else {
            ex = new ApiException(e, ApiException.UNKNOWN);
            ex.setDisplayMessage(msg);          //未知错误

            onFailure(ex);
        }
    }

    public void showDialog() {
        if (isShowDialog) {


        }
    }

    public void disDialog() {

        if (isShowDialog) {

            //MProgressDialog.dismissProgress();

        }

    }

}

