package com.dahe.gldriver.net

import android.accounts.NetworkErrorException
import android.app.Activity
import android.app.ProgressDialog
import com.dahe.gldriver.utils.UserUtils.Companion.instance
import android.content.Context
import android.text.TextUtils
import android.view.Window
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.net.ResultException
import com.dahe.mylibrary.utils.ToastUtils
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.util.concurrent.TimeoutException
import javax.net.ssl.SSLException

/**
 * 统一的网络回调
 */
abstract class RxHttpCallBack<T> {
    private var dialog: ProgressDialog? = null
    private fun initDialog(activity: Activity, dialogMessage: String) {
        dialog = ProgressDialog(activity)
        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.setProgressStyle(ProgressDialog.STYLE_SPINNER)
        dialog!!.setMessage(dialogMessage)
    }

    constructor(activity: Activity, dialogMessage: String?) {
        initDialog(activity,
            (if (TextUtils.isEmpty(dialogMessage)) "网络请求中......" else dialogMessage)!!)
    }

    constructor(activity: Activity) {
        initDialog(activity, "网络请求中......")
    }

    constructor() {}

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

    fun onStop() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }

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

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    fun onCodeError(mContext: Context?, t: CommonResponseBean<T>) {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
        if (t.code == 401) {
            instance.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.msg)
        }

//        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
     */
    fun onFailure(mContext: Context?, e: Throwable) {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
        if (e is ConnectException
            || e is TimeoutException
            || e is SocketTimeoutException
            || e is SSLException
            || e is NetworkErrorException
            || e is UnknownHostException
        ) {
            ToastUtils.showToast(mContext, "请检查您的网络~")
        } else if (e is ResultException) {
            e.printStackTrace()
            //自定义的ResultException
            //由于返回200,300返回格式不统一的问题，自定义GsonResponseBodyConverter凡是300的直接抛异常
//            System.out.println("---------errorCode------->"+((ResultException) e).getErrCode());
        } else if (e is SSLException) {
            ToastUtils.showToast(mContext, "网络连接终端，请检查您的网络~")
        } else {
            ToastUtils.showToast(mContext, e.message)
        }
    }
}