package com.zhaohaoting.framework.requset.rxjava_retrofit;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.zhaohaoting.framework.abs.entity.ResultEntity;
import com.zhaohaoting.framework.utils.Logger;
import com.zhaohaoting.framework.utils.observer.ObserverManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.FlowableSubscriber;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/5/21.
 */

public abstract class RxBodyResultSubscriber<DATA, RESULT_DATA extends ResultEntity<DATA>> implements FlowableSubscriber<ResponseBody> {

    protected static final int FAILURE = -1;//失败
    protected static final int LOCAL_FAILURE = -1000;//本地错误
    public static final int REQUEST_SUCCESS = 0;//请求成功
    public static final int REQUEST_FREEZE = -2;//冻结
    public static final String OBSERVER_REQUEST_ERROR = "OBSERVER_REQUEST_ERROR";//
    public static final int NEED_LOGIN = 6666;//需要从新登录


    @Override
    public void onNext(ResponseBody requestBody) {
        try {
            String stringBody = requestBody.string();
            Type type = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
            JSONObject jsonObject = new JSONObject(stringBody);
            int code = jsonObject.optInt("code");
            String msg = jsonObject.optString("msg");
            if (code == REQUEST_SUCCESS) {
                RESULT_DATA resultEntity = new Gson().fromJson(stringBody, type);
                onSuccess(resultEntity.getData());
            } else {
                ObserverManager.getInstance().notifyObserver(OBSERVER_REQUEST_ERROR, code);
                onFailure(code, msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
            onFailure(FAILURE, "异常");
        } catch (JSONException e) {
            e.printStackTrace();
            onFailure(FAILURE, " 数据解析出错");
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        Logger.e("RxResultSubscriber", e.getMessage());
        String tipMsg;//错误提示
        if (e instanceof JsonSyntaxException) {
            tipMsg = "数据解析出错";
        } else if (e instanceof SocketTimeoutException) {
            tipMsg = "链接超时";
        } else if (e instanceof ConnectException) {
            if (e.getMessage().contains("Network is unreachable"))
                tipMsg = "无网络";
            else if (e.getMessage().contains("failed to connect to")) {
                tipMsg = "链接服务器失败";
            } else {
                tipMsg = "网络不给力啊";
            }
        } else {
            tipMsg = "异常";
        }
        onFailure(FAILURE, tipMsg);
    }

    protected void onFailure(int code, String tipMsg) {
    }

    protected abstract void onSuccess(DATA dataSource);

    @Override
    public void onComplete() {

    }
}
