package com.quyunshuo.module.home.dialog

import android.content.Context
import android.os.Handler
import android.view.View
import android.view.View.OnLongClickListener
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.hboxs.serialport.plc.DialogClickListener
import com.module.database.room.SZDatabase
import com.module.database.room.entity.WarringBean
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameDialog
import com.quyunshuo.androidbaseframemvvm.base.utils.getString
import com.quyunshuo.androidbaseframemvvm.common.util.LongClickUtils
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.BackstageDialogTipsBinding
import com.quyunshuo.module.home.databinding.BackstageDialogWarringBinding
import com.quyunshuo.module.home.databinding.CommonDialogBinding
import com.quyunshuo.module.home.utils.ErrorTextUtlis
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 报警 对话框
 */
class ErrorDialog(mContext: Context) : BaseFrameDialog<CommonDialogBinding>(mContext),
    LifecycleObserver {


    var type1: Int = 1
    var type2: Int = 2

    var errorType = ""
    private var devOnoffDialoglistener: DialogClickListener? = null

    override fun CommonDialogBinding.initView() {
        LongClickUtils.setLongClick(Handler(), ivWarringImg, 3000, object : OnLongClickListener {
            override fun onLongClick(v: View?): Boolean {
                if (errorType.startsWith("01")) {
                    devOnoffDialoglistener?.onClickListener(type1, null)
                } else if (errorType.startsWith("02")) {
                    devOnoffDialoglistener?.onClickListener(type2, null)
                }

                return true
            }

        })
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    override fun dismiss() {
        super.dismiss()
    }

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


    override fun initObserve() {
    }

    override fun initRequestData() {
    }

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

    var notType: String = ""
    fun setType(type: String) {
        errorType = type
        notType = ErrorTextUtlis.getErrorNO(type)
        var errorContext = ErrorTextUtlis.getErrorText(type)
        mBinding.content.text = errorContext
        CoroutineScope(Dispatchers.Main).launch {
            addWarringBean(notType, type)
        }


    }

    val warringDao = SZDatabase.getDatabase(BaseApplication.context).getWarringDao()
    suspend fun addWarringBean(notType: String, thingError: String) {
        warringDao.insert(
            WarringBean(
                time = System.currentTimeMillis(),
                thingError = thingError,
                remark = notType
            )
        )
    }

}