package com.example.chemical.ui

import android.app.AlertDialog
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.hardware.usb.UsbManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.example.chemical.databinding.ActivityMainBinding
import com.example.chemical.receiver.TimeTickReceiver
import com.example.chemical.receiver.UsbReceiver
import com.example.chemical.ui.adapter.CabinetAdapter
import com.example.chemical.ui.plan.PlanAddActivity
import com.example.chemical.ui.discard.LedgerActivity
import com.example.chemical.ui.discard.WasteChemicalsActivity
import com.example.chemical.ui.inquiry.InquiryActivity
import com.example.chemical.ui.login.FacialCardActivity
import com.example.chemical.ui.login.FacialLoginActivity
import com.example.chemical.ui.login.ScanLoginActivity
import com.example.chemical.ui.login.SwipeActivity
import com.example.chemical.ui.still.ChemicalsAlsoActivity
import com.example.chemical.ui.still.MsdsActivity
import com.example.chemical.ui.uses.UseActivity
import com.example.chemical.ui.uses.WarningEventsActivity
import com.example.chemical.ui.warehousing.ChemicalLabelingActivity
import com.example.chemical.utils.RelativeLayoutDebouncer
import com.example.chemical.utils.SharedPreferencesHelper
import com.example.chemical.utils.TimeUpdater
import com.example.chemical.utils.UiManager
import com.example.chemical.weidith.CustomDialog
import com.lztek.toolkit.Lztek
import com.rc.core.log.RcLog
import com.rc.core.ui.activity.RcBaseActivity
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.HttpClient
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.exception.NetException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException

//首页 -  登录or 未登录
class MainActivity : RcBaseActivity<ActivityMainBinding>() {
    private lateinit var timeUpdater: TimeUpdater
    private val mAdapter by lazy { CabinetAdapter() }
    private var currentPosition = 0
    private lateinit var recyclerView: RecyclerView
    private var mUsbReceiver: UsbReceiver? = null  // 刷卡广播注册
    private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数
    private var mLztek: Lztek? = null
    private var mDeviceNum: String? = null        //设备唯一标识
    private var handler: Handler = Handler(Looper.getMainLooper())
    private lateinit var runnable: Runnable
    private lateinit var faceList: List<String>   //人员区间值
    private var mLoginType: String? = null        //登录方式

    //退出账号
    private var mCounter = 0
    private var MAX_TIME = 60    //返回时间   秒
    private val WHAT_COUNT_DOWN = 1


    private lateinit var layoutManager: LinearLayoutManager
    override fun createViewBinding() = ActivityMainBinding.inflate(LayoutInflater.from(this))

    override fun onResume() {
        super.onResume()

        if (ChemicalApp.userData != null) {
            //已登录
            viewBinding.inc.userLogin.visibility = View.GONE
            viewBinding.inc.loggedIn.visibility = View.VISIBLE
            viewBinding.inc.tvName.text = ChemicalApp.userData!!.userName
            val imageView = viewBinding.inc.imageName
            // 使用 Glide 加载网络图片
            Glide.with(this)
                .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .into(imageView)
            viewBinding.inc.imgE.setBackgroundResource(R.mipmap.cshrk_dl_tc)
            MAX_TIME = ChemicalApp.confs!!.signOutTime
            mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
            mCountDownHandler.sendEmptyMessage(WHAT_COUNT_DOWN)
        } else {
            viewBinding.inc.loggedIn.visibility = View.GONE
            viewBinding.inc.userLogin.visibility = View.VISIBLE
            viewBinding.inc.imgE.setBackgroundResource(R.mipmap.icon_login_img)
        }

        if (mAdapter.data != null && mAdapter.data.size > 1) {
//            startAutoScroll()
            startUpdating()
        }

        timeUpdater.startUpdating()
    }

    var timeTickReceiver: TimeTickReceiver = TimeTickReceiver()

    //必须在创建时注册信息
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 监听分钟广播
        registerReceiver(timeTickReceiver, IntentFilter(Intent.ACTION_TIME_TICK))


//        hPrints = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1)
        //注册广播
//        EventBus.getDefault().register(this)
        viewBinding.versionName.text = "版本号：${DeviceUtils.getVersionName(this)}"
        // 创建定时任务
        runnable = object : Runnable {
            override fun run() {
                // 每隔一个小时执行一次操作
                // 这里可以添加你需要执行的代码
                subAddData()
                // 重新启动定时任务
                handler.postDelayed(this, 3600000) // 3600000 毫秒为一小时
            }
        }

        // 第一次启动定时任务
        handler.post(runnable)

        viewBinding.test.setOnClickListener {
            showTextInputDialog("请输入内容") { input ->
                if (input != null) {
                    // 处理用户输入的逻辑
                    if (input.isNotEmpty()) {
                        var pwd = "123" //默认密码123

                        try {
                            val urlBase = SharedPreferencesHelper.getUrlBase(this)
                            if (urlBase != null && urlBase.pwd != null) {
                                RcLog.info("=====本地密码:${urlBase!!.pwd}")
                                pwd = urlBase.pwd!!
                            }
                        } catch (e: Exception) {

                        }

                        if (input == pwd) {
                            UiManager.switcher(this, SettingActivity::class.java)
                        } else {
                            showToast("密码错误")
                        }


                    }
                } else {
                    // 用户点击了取消按钮
                    showToast("用户取消了输入")
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        //获取基础配置
        basicConf()
    }

    //获取刷卡信息
    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
//        mPortScanHelper.dispatchKeyEvent(event)
        return super.dispatchKeyEvent(event)
    }

    override fun onStop() {
        super.onStop()
        RcLog.info("============onStop")
    }

    //停止 销毁广播传递
    override fun onPause() {
//        mPortScanHelper.onPause()
        RcLog.info("============onPause")
        try {
            stopUpdating()
            timeUpdater.stopUpdating()
            RcLog.info("========UI在刷新停止")
            mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
            mCountDownHandler.removeCallbacksAndMessages(null)
        } catch (e: Exception) {
        }
        super.onPause()
    }

//    //必须写这个方法  防止注册失败
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    fun onUpdateEventEvent(event: KeyEvent) {
//
//    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
//        registerUsbBroadcast() //注册广播
    }


    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        getUserIds()
        val handler = Handler(Looper.getMainLooper())
        timeUpdater = TimeUpdater(handler) { currentTime ->
            viewBinding.inc.nowTime.text = "$currentTime"
        }
        // 启动定时更新
        timeUpdater.startUpdating()

        initAdapter()
        viewBinding.imgLeft.setOnClickListener {
            if (currentPosition > 0) {
                currentPosition--
                recyclerView.smoothScrollToPosition(currentPosition)
            }
        }

        viewBinding.imgRight.setOnClickListener {
            if (currentPosition < mAdapter.data.size - 1) {
                currentPosition++
                recyclerView.smoothScrollToPosition(currentPosition)
            }
        }
        //登录
        RelativeLayoutDebouncer.setDebouncedOnClickListener(viewBinding.inc.userLogin, 1000L) {
            basicConfLog()
        }

//        viewBinding.inc.userLogin.setOnClickListener {
//            basicConfLog()
//        }
        viewBinding.addChemicals.setOnClickListener {
//            UiManager.switcher(this, PlanAddActivity::class.java)
            //新增
            if (ChemicalApp.userData != null) {
                if (ChemicalApp.administrators || ChemicalApp.responsibles) {
                    UiManager.switcher(this, PlanAddActivity::class.java)
                } else {
                    customDialogView(2, "当前人员无权限")
                }
            } else {
                basicConfLog()
            }
        }


        //待入库 TODO 暂时隐藏
//        viewBinding.reSto.setOnClickListener {
//            if (ChemicalApp.userData != null) {
//                UiManager.switcher(this, WarehousingActivity::class.java)
//            } else {
//                basicConfLog()
//            }
//        }

        //废弃
        viewBinding.reDis.setOnClickListener {
            if (ChemicalApp.userData != null) {
                UiManager.switcher(this, WasteChemicalsActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //查询
        viewBinding.reInq.setOnClickListener {
            val map = mutableMapOf<String, Any>()
            if (ChemicalApp.userData != null) {
                map["logIn"] = 1
            } else {
                map["logIn"] = 0
            }
            map["cabinetId"] = "a"
            map["doorId"] = "a"
            UiManager.switcher(this, map, InquiryActivity::class.java)
        }

        //领用
        viewBinding.reCla.setOnClickListener {
            if (ChemicalApp.userData != null) {
                UiManager.switcher(this, UseActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //归还
        viewBinding.reRet.setOnClickListener {
            if (ChemicalApp.userData != null) {
                UiManager.switcher(this, ChemicalsAlsoActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //电子台账
        viewBinding.rlLedger.setOnClickListener {
            if (ChemicalApp.userData != null) {
                UiManager.switcher(this, LedgerActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //标签管理
        viewBinding.reLedger.setOnClickListener {

            if (ChemicalApp.userData != null) {
//                UiManager.switcher(this, ElectronicLedgerActivity::class.java)
                //化学品标签
                UiManager.switcher(this, ChemicalLabelingActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //预警事件
        viewBinding.rlWarning.setOnClickListener {
            if (ChemicalApp.userData != null) {
                UiManager.switcher(this, WarningEventsActivity::class.java)
            } else {
                basicConfLog()
            }
        }

        //演示流程  -MSDS
        viewBinding.rlDem.setOnClickListener {
            //暂时不用演示流程  跳转MSDS
//            UiManager.switcher(this, DemonstrateActivity::class.java)
            UiManager.switcher(this, MsdsActivity::class.java)
        }

        viewBinding.inc.tvOutLogin.setOnClickListener {
            ChemicalApp.userData = null
//            ChemicalApp.subjectId = null
            HttpClient.token = null
            UiManager.switcherCashier(this, SplashActivity::class.java)
        }
        getCabinetIn()
    }

    /**
     * 查询柜子
     */
    private fun getCabinetIn() {
        showLoading("查询中...")
        val disposable = ApiRepository.getCabinetBySubId(ChemicalApp.subjectId!!)
            .subscribe({ data ->
                dismissLoading()
                mAdapter.setNewInstance(data.toMutableList())
                if (mAdapter.data != null && mAdapter.data.size > 1) {
                    // 初始化 Handler 和 Runnable
                    handlerBanner = Handler(Looper.getMainLooper())
                    updateRunnable = object : Runnable {
                        override fun run() {
                            // 更新 UI 操作
                            RcLog.info("========UI在刷新")
                            val currentPosition = layoutManager.findFirstVisibleItemPosition()
                            if (currentPosition != RecyclerView.NO_POSITION) {
                                val nextPosition = (currentPosition + 1) % mAdapter.itemCount
                                recyclerView.smoothScrollToPosition(nextPosition)
                            }
                            if (isUpdating) {
                                handlerBanner.postDelayed(this, 5000) // 5 秒后再次执行
                            }
                        }
                    }
                    startUpdating()
                }
            }, { throwable ->
                dismissLoading()
//                throwableView(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }


    private fun getUserIds() {
        if (ChemicalApp.subjectId != null) {
            showLoading("查询中...")
            val disposable = ApiRepository.getUserIds(ChemicalApp.subjectId!!)
                .subscribe({ data ->
                    dismissLoading()
                    faceList = data
                }, { throwable ->
                    dismissLoading()
//                throwableView(throwable)
                })
            addDisposable(disposable)
        }
    }

    private fun initAdapter() {
        recyclerView = viewBinding.relView
        layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = mAdapter
        recyclerView.setOnTouchListener { _, _ ->
            true
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            // 在这里处理子View的点击事件
            if (ChemicalApp.userData != null) {
                val map = mutableMapOf<String, Any>()
                map["logIn"] = 1
                when (view.id) {
                    R.id.lingOne -> {
                        map["cabinetId"] = mAdapter.data[position].cabinetId
                        map["doorId"] = mAdapter.data[position].cabinetDoorVoList[0].doorUniqueId
                        UiManager.switcher(this, map, InquiryActivity::class.java)
                    }

                    R.id.lintTwo -> {
                        map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[1].cabinetId
                        map["doorId"] = mAdapter.data[position].cabinetDoorVoList[1].doorUniqueId
                        UiManager.switcher(this, map, InquiryActivity::class.java)
                    }

                    R.id.lintThree -> {
                        map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[2].cabinetId
                        map["doorId"] = mAdapter.data[position].cabinetDoorVoList[2].doorUniqueId
                        UiManager.switcher(this, map, InquiryActivity::class.java)
                    }

                    R.id.lintFour -> {
                        map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[3].cabinetId
                        map["doorId"] = mAdapter.data[position].cabinetDoorVoList[3].doorUniqueId
                        UiManager.switcher(this, map, InquiryActivity::class.java)
                    }

                    R.id.imgViews -> {
                        map["cabinetId"] = "a"
                        map["doorId"] = "a"
                        UiManager.switcher(this, map, InquiryActivity::class.java)
                    }

                }
            } else {
                //进行登录
                basicConfLog()
            }

        }
    }

    private lateinit var handlerBanner: Handler
    private lateinit var updateRunnable: Runnable
    private var isUpdating = false

    private fun startUpdating() {
        if (!isUpdating) {
            isUpdating = true
            handlerBanner.post(updateRunnable)
        }
    }

    private fun stopUpdating() {
        if (isUpdating) {
            isUpdating = false
            handlerBanner.removeCallbacks(updateRunnable)
        }
    }

//    private val mPortScanHelper by lazy {
//        PortScanHelper(this, object : OnSerialScanListener {
//            override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
//                if (!mHandleScanEvent) {
//                    if (content.isNotBlank()) {
//                        RcLog.info("==========当前usb返回参数$content")
//                        mHandleScanEvent = true
//                        handleScanEvent(content)
//                    }
//
//                }
//            }
//
//        })
//    }

    //调用刷卡信息
    private fun handleScanEvent(cont: String) {
//        showLoading("查询中...")
//        val disposable = ApiRepository.querySubInfo()
//            .subscribe({ data ->
//                dismissLoading()
//
//            }, { throwable ->
//                dismissLoading()
//                showNetError(throwable)
//                throwable.printStackTrace()
//            })
//        addDisposable(disposable)

    }

    //刷卡usb链接
    private fun registerUsbBroadcast() {
        if (null == mUsbReceiver) {
            val filter = IntentFilter().apply {
                addAction(UsbReceiver.ACTION_USB_PERMISSION)
                addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
                addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
                addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
            }
            mUsbReceiver = UsbReceiver()
            registerReceiver(mUsbReceiver, filter)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(timeTickReceiver)
        try {
            // 停止定时更新
            timeUpdater.stopUpdating()
//        unregisterReceiver(mUsbReceiver)
//        EventBus.getDefault().unregister(this) //关闭广播
            // 移除未执行的定时任务
            handler.removeCallbacks(runnable)
            handler.removeCallbacksAndMessages(null)

            stopUpdating()
            mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
            mCountDownHandler.removeCallbacksAndMessages(null)
        } catch (e: Exception) {
        }
    }


    private fun subAddData() {
        //数据上报
        mLztek = Lztek.create(this)
        val toUpperCase = mLztek!!.ethMac.toUpperCase()
        mDeviceNum = toUpperCase.replace(":", "")
        val disposable = ApiRepository.monitor(mDeviceNum!!)
            .subscribe({ data ->

            }, { throwable ->
                showNetError(throwable)
            })
        addDisposable(disposable)
    }

    //loginType     登陆方式，1人脸 2刷卡 3扫码   4人脸+刷卡
    //verifyType    双人验证方式    1人脸 2刷卡 3扫码
    private fun basicConf() {
        val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
            .subscribe({ data ->
                ChemicalApp.confs = data
                mLoginType = data.loginType     //登陆方式
                if (data.levelName != null) {
                    val result = data.levelName!!.toCharArray().joinToString("\n")
                    viewBinding.tvNoName.text = result
                    // 创建一个GradientDrawable对象
                    val shapes = GradientDrawable()
                    // 设置形状为矩形
                    shapes.shape = GradientDrawable.RECTANGLE
                    // 设置背景色为红色（这里使用颜色资源）
                    shapes.setColor(Color.parseColor(data.levelColor))
                    shapes.cornerRadius = 5f
                    // 将GradientDrawable对象设置为View的背景
                    viewBinding.tvNoName.background = shapes
                } else {
                    viewBinding.tvNoName.visibility = View.GONE
                }
                viewBinding.subName.text = data.subName
                viewBinding.inc.deptName.text = "${data.deptName}"
                Glide.with(this)
                    .load("${HttpConfig.API_BASE_IMG_URL}${data.circularLogo}")
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                    .into(viewBinding.inc.circularLogo)
            }, { throwable ->
                showNetError(throwable)
            })
        addDisposable(disposable)
    }

    //loginType     登陆方式，1人脸 2刷卡 3扫码   4人脸+刷卡
    //verifyType    双人验证方式    1人脸 2刷卡 3扫码
    private fun basicConfLog() {
        val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
            .subscribe({ data ->
                LogUtils.json(data)
                ChemicalApp.confs = data
                mLoginType = data.loginType     //登陆方式
                if (mLoginType == null) {
                    customDialogView(2, "登录方式配置有误")
                } else {
                    if (mLoginType!!.contains("4")) {
                        //需要刷卡+人脸
                        UiManager.switcher(this, FacialCardActivity::class.java)
                    } else {
                        val map = mutableMapOf<String, String>()
                        //1人脸 2刷卡 3扫码   4人脸+刷卡
                        if (mLoginType!!.length == 1) {
                            when (mLoginType) {
                                "1" -> {
                                    //人脸
                                    map["faceList"] = faceList.toString()
                                    map["mtypes"] = "1"
                                    UiManager.switcher(this, map, FacialLoginActivity::class.java)
                                }

                                "2" -> {
                                    //刷卡
                                    map["mtypes"] = "1"
                                    UiManager.switcher(this, map, SwipeActivity::class.java)
                                }

                                else -> {
                                    //扫码
                                    map["mtypes"] = "1"
                                    UiManager.switcher(this, map, ScanLoginActivity::class.java)
                                }
                            }

                        } else {
                            val array = mLoginType!!.split(",").toTypedArray()
                            when (array.size) {
                                2 -> {
                                    //1-2(人脸+刷卡)  1-3(人脸+扫码)  2-3(刷卡+扫码)
                                    if (mLoginType == "1,2" || mLoginType == "2,1") {
                                        //人脸+刷卡
                                        map["faceList"] = faceList.toString()
                                        map["mtypes"] = "4"  //隐藏扫码
                                        UiManager.switcher(
                                            this,
                                            map,
                                            FacialLoginActivity::class.java
                                        )
                                    } else if (mLoginType == "1,3" || mLoginType == "3,1") {
                                        //人脸+扫码
                                        map["faceList"] = faceList.toString()
                                        map["mtypes"] = "5"  //隐藏刷卡
                                        UiManager.switcher(
                                            this,
                                            map,
                                            FacialLoginActivity::class.java
                                        )
                                    } else if (mLoginType == "2,3" || mLoginType == "3,2") {
                                        //刷卡+扫码
                                        map["mtypes"] = "6"  //隐藏人脸
                                        UiManager.switcher(this, map, SwipeActivity::class.java)
                                    } else {
                                        showToast("登录方式$mLoginType")
                                    }

                                }

                                else -> {
                                    //3条
                                    try {
                                        map["mtypes"] = "0"
                                        map["faceList"] = faceList.toString()
                                        UiManager.switcher(
                                            this,
                                            map,
                                            FacialLoginActivity::class.java
                                        )
                                    } catch (e: Exception) {
                                        map["mtypes"] = "0"
                                        UiManager.switcher(this, map, SwipeActivity::class.java)
                                    }
                                }
                            }
                        }

                    }
                }
            }, { throwable ->
                showNetError(throwable)
            })
        addDisposable(disposable)
    }


    /**
     * 0  没有图标 1 绿色(成功)   2红色（失败）
     *   失败或者成功的弹框
     */
    private fun customDialogView(types: Int, msg: String) {
        val customDialog = CustomDialog(this, types, msg)
        if (!this.isFinishing && !this.isDestroyed) {
            customDialog.show()
        }
    }

    /**
     * 异常处理
     */
    private fun throwableView(throwable: Throwable) {
        when (throwable) {
            is NetException -> {
                if (throwable.message.isNullOrEmpty()) {
                    "接口请求失败(${throwable.code})"
                } else {
                    throwable.message!!
                }
            }

            is SocketTimeoutException -> "请求超时，请稍后重试"
            is ConnectException -> "无法连接服务器，请检查网络"
            is HttpException -> "服务器繁忙，请稍后重试"
            else -> null
        }?.let { customDialogView(2, "$it") }
    }

    //只管注销

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

        override fun handleMessage(msg: Message) {
            if (WHAT_COUNT_DOWN == msg.what) {
                val countDown = MAX_TIME - mCounter
//                Log.d("======返回首页时间", "${countDown}")
                if (isDestroyed) return
                if (countDown <= 0) {
                    ChemicalApp.confs = null
                    ChemicalApp.subjectId = null
                    ChemicalApp.userData = null
                    callLogoutApis {
                        UiManager.switcherCashier(
                            this@MainActivity,
                            SplashActivity::class.java
                        )
                    }
                } else {
                    sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 1000)
                }
                mCounter++
            }
        }
    }

    override fun onUserInteraction() {
        mCounter = 0
    }

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


    private fun showTextInputDialog(title: String, callback: (String?) -> Unit) {


        val editText = EditText(this)
        val dialog = AlertDialog.Builder(this)
            .setTitle(title)
            .setView(editText)
            .setPositiveButton("确定") { _, _ ->
                val inputText = editText.text.toString()
                callback(inputText)
            }
            .setNegativeButton("取消") { _, _ ->
                callback(null)
            }
            .create()

        dialog.show()
    }

}
