package com.sheep.gamegroup.model.util;

import android.content.Context;
import android.text.TextUtils;
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.Jump2View;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.NetUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;
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();
        //接下来可以检查网络连接等操作
        if(context == null)
            context = SheepApp.getInstance();
        if (!NetUtil.isConnected(context)) {

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

    @Override
    public void onError(Throwable e) {
        try{
            if(e instanceof Exception){
                //访问获得对应的Exception
                ExceptionHandle.ResponeThrowable throwable = ExceptionHandle.handleException(e);

                BaseMessage baseMessage = null;
                if(throwable.message.startsWith("{")){//json
                    baseMessage = JSONObject.parseObject(throwable.message, BaseMessage.class);
                }else if(ViewUtil.isH5Content(throwable.message)){//html error
                }else {//尝试解密
                    try {
                        baseMessage = JSONObject.parseObject(BaseMessageConverter.decrypt(throwable.message), BaseMessage.class);
                    } catch (Exception ignore) {
                    }
                }
                if(baseMessage == null){
                    baseMessage = new BaseMessage();
                    baseMessage.setMsg(throwable.message);
                }
                //token 过期
                if(throwable.code == 401){
                    SpUtils.saveToken(SheepApp.getInstance(), "");
                    Jump2View.getInstance().gotoLoginAgain(baseMessage.getMsg());
                    return;
                }
                //未绑定手机号
                if(throwable.code == 302 && baseMessage.getCode() == 30001){
                    Jump2View.getInstance().goBindPhone(SheepApp.getInstance(), baseMessage.getCode());
                    return;
                }else if(baseMessage.getCode() == 1002){
                    Jump2View.getInstance().gotoLoginAgain();
                }

                if(baseMessage.getCode() == 0)
                    baseMessage.setCode(throwable.code);
//                LogUtil.println("sheepApi", "onError", e.getMessage(), throwable.message, baseMessage);
                onError(baseMessage);
            }else {
                BaseMessage baseMessage = new BaseMessage();
                baseMessage.setMsg(e.getMessage());
                baseMessage.setCode(ExceptionHandle.ERROR.UNKNOWN);
                onError(baseMessage);
            }
        }catch (Exception er){
            BaseMessage baseMessage = new BaseMessage();
            baseMessage.setMsg(er.getMessage());
            baseMessage.setCode(ExceptionHandle.ERROR.UNKNOWN);
            onError(baseMessage);
        }
    }

    public abstract void onError(BaseMessage baseMessage);

    @Override
    public void onCompleted() {
    }
}
