package com.sheep.gamegroup.model.util;

import android.app.Activity;
import android.content.Context;
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.module.login.LoginAct;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.NetUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


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

public abstract class SheepSubscriber<T> implements Observer<T> {

    private Context context;

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

    @Override
    public void onSubscribe(Disposable d) {
        if (!NetUtil.isConnected(context)) {
            G.shortToast("当前网络不可用，请检查网络情况");
            // 一定好主动调用下面这一句,取消本次Subscriber订阅
            if (!d.isDisposed()) {
                d.dispose();
            }
            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){
                    Activity curAct = ActivityManager.getInstance().currentActivity();
                    if(curAct instanceof LoginAct || UMConfigUtils.isNotNeedAct(curAct)){//本身就是登录界面 或者可以不用登录就存在的界面，如：支付界面
                    } else {
                        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 onComplete() {
    }
}
