package com.dahe.pass.net;

import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Window;

import com.dahe.dispatch.utils.UserUtils;
import com.dahe.mylibrary.net.CommonResponseBean;
import com.dahe.mylibrary.net.ResultException;
import com.dahe.mylibrary.utils.ToastUtils;
import com.dahe.pass.ui.account.LoginActivity;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLException;

/**
 * 统一的网络回调
 */

public abstract class RxHttpCallBack<T> {
    private ProgressDialog dialog;

    private void initDialog(Activity activity, String dialogMessage) {
        dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(dialogMessage);
    }

    public RxHttpCallBack(Activity activity, String dialogMessage) {
        initDialog(activity, TextUtils.isEmpty(dialogMessage) ? "网络请求中......" : dialogMessage);
    }

    public RxHttpCallBack(Activity activity) {
        initDialog(activity, "网络请求中......");
    }

    public RxHttpCallBack() {
    }

    /**
     * 订阅成功
     */
    public void onStart() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void onStop(){
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    public void onSuccess(CommonResponseBean<T> t) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    public void onCodeError(Context mContext, CommonResponseBean<T> t) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();

        }
        if (t.getCode() == 401) {
            UserUtils.Companion.getInstance().loginOut(mContext);
            Intent intent = new Intent(mContext, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
            ToastUtils.showToast(mContext,"您的账号再异地登录，请重新登录");
        }else{
            ToastUtils.showToast(mContext,t.getMsg());
        }

//        if (t.getMessage().getMsg_code() == 100 && SPUtils.hasUserInfo(mContext)) {
//            SPUtils.remove(mContext, SPUtils.USER_INFO_KEY);
//        } else {
//            ToastUtils.showToast(mContext, t.getMessage().getMsg_desc());
//        }
    }

    /**
     * 返回失败
     *
     * @param e
     * @throws Exception
     */
    public void onFailure(Context mContext, Throwable e) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (e instanceof ConnectException
                || e instanceof TimeoutException
                || e instanceof SocketTimeoutException
                || e instanceof SSLException
                || e instanceof NetworkErrorException
                || e instanceof UnknownHostException) {
            ToastUtils.showToast(mContext, "请检查您的网络~");
        } else if (e instanceof ResultException) {
            e.printStackTrace();
            //自定义的ResultException
            //由于返回200,300返回格式不统一的问题，自定义GsonResponseBodyConverter凡是300的直接抛异常
//            System.out.println("---------errorCode------->"+((ResultException) e).getErrCode());
        }else if (e instanceof SSLException){
            ToastUtils.showToast(mContext, "网络连接终端，请检查您的网络~");
        }else{
            ToastUtils.showToast(mContext, e.getMessage());
        }
    }
}
