package com.quyunshuo.module.home.dialog

import android.app.Dialog
import android.content.Context
import android.os.Handler
import com.hboxs.serialport.plc.DialogClickListener
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameDialog
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.BackstageDialogCoutDownBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference

/**
 * 倒计时的对话框
 */
class CountDownDialogFragment(mContext: Context) :
    BaseFrameDialog<BackstageDialogCoutDownBinding>(mContext) {
    private val CLOSE_TIME = 30
    private var backTime: Int = CLOSE_TIME
    var type1: Int = 1
    var type2: Int = 2
    private var myHandler: MyHandler? =null


    private var devOnoffDialoglistener: DialogClickListener? = null

    override fun BackstageDialogCoutDownBinding.initView() {
        myHandler= MyHandler(this@CountDownDialogFragment)

//        tvBack.setOnClickListener {
//            dismiss()
//        }
//        tvStartDev.setOnClickListener {
//            devOnoffDialoglistener?.onClickListener(type1, null)
//        }

        startRefreshime()
    }

    private var jobTimer: Job? = null

    private fun startRefreshime() {
        backTime = CLOSE_TIME
        stopRefreshTime()
        // 启动协程
        jobTimer = CoroutineScope(Dispatchers.IO).launch {
            while (true) {
                myHandler?.sendEmptyMessage(1)
                delay(1000) // 每隔1秒重复执行
            }
        }
    }

    private fun stopRefreshTime() {
        jobTimer?.cancel()
    }


    override fun dismiss() {
        stopRefreshTime()
        super.dismiss()
    }

    override fun createVB() = BackstageDialogCoutDownBinding.inflate(layoutInflater)


    override fun initObserve() {
    }

    override fun initRequestData() {
    }

    fun setListener(listener: DialogClickListener) {
        this.devOnoffDialoglistener = listener
    }

    fun setContent(content: String) {
        mBinding.tvTitle.text = content
    }

    internal class MyHandler(dialog: Dialog) : Handler() {
        var mWeakReference: WeakReference<Dialog> = WeakReference(dialog)

        override fun handleMessage(msg: android.os.Message) {
            //as用来强转
            var fragment: CountDownDialogFragment = mWeakReference.get() as CountDownDialogFragment
            when (msg.what) {
                1 -> {
                    fragment.mBinding.tvProgress.text = fragment.backTime.toString() + UiUtil.getStringRes(
                        R.string.base_second)
                    fragment.backTime--
                    if (fragment.backTime <= 0) {
                        fragment.devOnoffDialoglistener?.onClickListener(fragment.type1, null)
                        fragment.dismiss()
                    }
                }


                else -> {

                }
            }
        }

        fun disconnectListener() {
            removeCallbacksAndMessages(null)
        }
    }

}