package com.sheep.gamegroup.model.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.api.BaseMessageConverter;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.NetUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import rx.Subscriber;

/**
 * Created by ljy on 2018/3/19.
 */

public abstract class SheepSubscriber<T> extends Subscriber<T> {

    private Context context;

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

    @Override
    public void onStart() {
        super.onStart();
        Log.i("tag","MySubscriber.onStart()");
        //接下来可以检查网络连接等操作
        if (!NetUtil.isConnected(context)) {

            Toast.makeText(context, "当前网络不可用，请检查网络情况", Toast.LENGTH_SHORT).show();
            // 一定好主动调用下面这一句,取消本次Subscriber订阅
            if (!isUnsubscribed()) {
                unsubscribe();
            }
            return;
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.e("tag","MySubscriber.throwable ="+e.toString());
//        Log.e("tag","MySubscriber.throwable ="+e.getMessage());

        try{
            if(e instanceof Exception){
                //访问获得对应的Exception
                ExceptionHandle.ResponeThrowable throwable = ExceptionHandle.handleException(e);
                /**
                 * token 过期
                 */
                if(throwable.code == 401){
                    SpUtils.login(SheepApp.mContext,"");
                    G.showToast("请重新登录");
                    ActivityManager.getInstance().finishAllActivity();
                    Jump2View.getInstance().goLoginView(SheepApp.mContext, "");
                    return;
                }
                BaseMessage baseMessage;
                if(throwable.message.startsWith("{")){
                    baseMessage = JSONObject.parseObject(throwable.message, BaseMessage.class);
                }else {
                    try {
                        baseMessage = JSONObject.parseObject(BaseMessageConverter.decrypt(throwable.message), BaseMessage.class);
                    } catch (Exception ignore) {
                        baseMessage = new BaseMessage();
                        baseMessage.setMsg(throwable.message);
                    }
                }


                if(baseMessage.getCode() == 0)
                    baseMessage.setCode(throwable.code);
                onError(baseMessage);
            }else {
                //将Throwable 和 未知错误的status code返回
                onError(new ExceptionHandle.ResponeThrowable(e,ExceptionHandle.ERROR.UNKNOWN));
            }
        }catch (Exception er){
            BaseMessage baseMessage = new BaseMessage();
            baseMessage.setMsg(er.getMessage());
            baseMessage.setCode(ExceptionHandle.ERROR.UNKNOWN);
            onError(baseMessage);
            er.printStackTrace();
        }
    }

    public abstract void onError(BaseMessage baseMessage);

    @Override
    public void onCompleted() {
        Log.i("tag","MySubscriber.onComplete()");
    }
}
