package com.dskj.trustlend.api;

import android.content.Intent;

import com.dskj.trustlend.InitApp;
import com.dskj.trustlend.utils.LogUtils;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.text.ParseException;

import io.reactivex.observers.DisposableObserver;
import retrofit2.HttpException;

public abstract class BaseObserver<T> extends DisposableObserver<T> {
    /**
     * 解析数据失败
     */
    public static final int PARSE_ERROR = 1001;
    /**
     * 网络问题
     */
    public static final int BAD_NETWORK = 1002;
    /**
     * 连接错误
     */
    public static final int CONNECT_ERROR = 1003;
    /**
     * 连接超时
     */
    public static final int CONNECT_TIMEOUT = 1004;

    @Override
    public void onNext(T o) {
//        try {
            Result model = (Result) o;
        LogUtils.i("走了重启流程0"+model.code);

            if (model.code == 1) {
                onSuccess(o);
            } else {
                LogUtils.i("走了重启流程11111111111！！！");

                onError(model.code,model.msg);
                if(model.code == -996||model.code == -994||model.code == -995){
                    InitApp.setToken("");
                    InitApp.setUserToken("");
                    reStart();
                }
            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            onError(407,e.toString());
//
//        }

    }

    /*** 重新启动应用*/
    public void reStart(){
        Intent launch= InitApp.getAppContext().getPackageManager()
                .getLaunchIntentForPackage(InitApp.getAppContext()
                        .getPackageName());
        launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        InitApp.getAppContext().startActivity(launch);
        LogUtils.i("走了重启流程！！！");
    }

    @Override
    public void onError(Throwable e) {
//        LogUtils.i("走了重启流程onError！！！");

        if (e instanceof HttpException) {
            //   HTTP错误
            onException(BAD_NETWORK);
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {
            //   连接错误
            onException(CONNECT_ERROR);
        } else if (e instanceof InterruptedIOException) {
            //  连接超时
            onException(CONNECT_TIMEOUT);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            //  解析错误
            onException(PARSE_ERROR);
        } else {
            if (e != null) {
                onError(409,e.toString());
            } else {
                onError(407,"未知错误");
            }
        }

    }

    private void onException(int unknownError) {
        switch (unknownError) {
            case CONNECT_ERROR:
                onError(CONNECT_ERROR,"连接错误");
                break;

            case CONNECT_TIMEOUT:
                onError(CONNECT_TIMEOUT,"连接超时");
                break;

            case BAD_NETWORK:
                onError(BAD_NETWORK,"网络问题");
                break;

            case PARSE_ERROR:
                onError(PARSE_ERROR,"数据解析异常");
                break;

            default:
                break;
        }
    }

    @Override
    public void onComplete() {

    }

    public abstract void onSuccess(T o);

    public abstract void onError(int code,String msg);
}
