package com.example.chemical.ui.common

import android.app.Dialog
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.viewbinding.ViewBinding
import com.example.chemical.ChemicalApp
import com.example.chemical.comm.Constants
import com.example.chemical.iml.IBankTeps
import com.example.chemical.ui.MainActivity
import com.example.chemical.utils.TimesUils
import com.example.chemical.utils.UiManager
import com.rc.core.ui.activity.RcBaseActivity
import com.rc.httpcore.client.ApiRepository
import com.tobey.dialogloading.DialogUtils

/**
 * info
 *
 * @author ReiChin_
 */
abstract class BaseCountDownActivity<VB : ViewBinding> : RcBaseActivity<VB>() {
    var mDialog: Dialog? = null
    private var mCounter = 0
    private var mIBankTeps: IBankTeps? = null
    companion object {
        private var MAX_TIME = 60    //注销时间必须大于自动返回时间   单位
        private const val WHAT_COUNT_DOWN = 1

    }


    // 在B Activity中设置回调
    fun seIBanks(callback: IBankTeps) {
        this.mIBankTeps = callback
    }

    protected fun stopCountDown() {
        mCounter = 0
        mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
    }

    protected fun startCountDown() {
        mCounter = 0
//        Log.d("======父页面返回","--------${ChemicalApp.confs!!.backTime}")
        mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
        if (null != ChemicalApp.userData) {
            Constants.OFFTIME_TIME = ChemicalApp.confs!!.offTime  //弹框返回时间
            mCountDownHandler.sendEmptyMessage(WHAT_COUNT_DOWN)
//            Log.d("======父页面返回","--------${ChemicalApp.confs!!.backTime}")
            if (ChemicalApp.confs!!.backTime != 0) {
                val secondsToMillisecondst =
                    TimesUils.secondsToMillisecondst(ChemicalApp.confs!!.backTime)
                MAX_TIME = ChemicalApp.confs!!.backTime
//                Log.d("======父页面返回","--------$MAX_TIME")
            }

        }
    }

    override fun onResume() {
        super.onResume()
//        Log.d("======父页面返回","--------232131---3231")
        startCountDown()
    }

    override fun onPause() {
        super.onPause()
        stopCountDown()
    }

    override fun onDestroy() {
        mCountDownHandler.removeCallbacksAndMessages(null)
        if (mDialog != null) {
            DialogUtils.Companion.closeDialog(mDialog!!)
        }
        if (Constants.VIEW_TYPE) {
            Constants.VIEW_TYPE = false
        }
        super.onDestroy()
    }

    //和页面交互时进行触发得
    override fun onUserInteraction() {
//        Log.d("===页面有交互","---------------")
        startCountDown()
    }

    private val mCountDownHandler = object : Handler(Looper.getMainLooper()) {

        override fun handleMessage(msg: Message) {
//            if (WHAT_COUNT_DOWN == msg.what) {
//
//                val countDown = MAX_TIME - mCounter
//                if (isDestroyed) return
//                if (countDown <= 0) {
//                    if (Constants.VIEW_TYPE) {
//                        //当前在废弃or领用页面
//                        mIBankTeps!!.onViewLint()
//                    } else {
//                        UiManager.switcherCashier(
//                            this@BaseCountDownActivity,
//                            MainActivity::class.java
//                        )
//                    }
//                } else {
//                    sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 1000)
//                }
//                mCounter++
//            }
        }
    }


    protected fun callLogoutApi(callback: ((success: Boolean) -> Unit)? = null) {
        showLoading("退出中...")
        val disposable = ApiRepository.loginOut()
            .subscribe({
                dismissLoading()
                callback?.invoke(true)
            }, {
                dismissLoading()
                callback?.invoke(false)
            })
        addDisposable(disposable)
    }


}