package com.module.backstage.fragment.param

import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.hboxs.serialport.plc.DialogClickListener
import com.hboxs.serialport.plc.message.Message
import com.hboxs.serialport.sbc.VBoxMessage
import com.hboxs.serialport.sbc.frame.VboxCommand
import com.module.backstage.R
import com.module.backstage.adapter.CloseReParamAdapter
import com.module.backstage.adapter.GeneralParamAdapter
import com.module.backstage.databinding.BackstageFragmentGeneralParamBinding
import com.module.backstage.databinding.CloseReFragmentGeneralParamBinding
import com.module.backstage.dialog.OKCancelDialog
import com.module.backstage.vm.CloseReParamFragmentVM
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.module.backstage.vm.GeneralParamFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 通用参数Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@RegisterEventBus
@AndroidEntryPoint
class CloseReminParamFragment :
    BaseFragment<CloseReFragmentGeneralParamBinding, CloseReParamFragmentVM>() {

    private val TAG = "GeneralParamFragment"
    var okCancelDialog: OKCancelDialog? = null
    var closeReParamAdapter: CloseReParamAdapter? = null
    override val mViewModel: CloseReParamFragmentVM by viewModels()
    override fun createVB() = CloseReFragmentGeneralParamBinding.inflate(layoutInflater)

    override fun onDestroyView() {
        mBinding.closerv.adapter = null
        super.onDestroyView()
    }

    override fun CloseReFragmentGeneralParamBinding.initView() {
        with(closerv) {
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@CloseReminParamFragment.context,
                    4,
                    androidx.recyclerview.widget.GridLayoutManager.VERTICAL,
                    false
                )
            layoutManager = gridLayoutManager

            closeReParamAdapter = CloseReParamAdapter(mViewModel.getCloseReParamData())
            closeReParamAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {


                    data?.let { showOkCancelDialog(position, it) }
                }

            })
            adapter = closeReParamAdapter
        }
        mViewModel.startData()

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun event(messageEvent: VBoxMessage) {
        // Log.d(TAG, "GeneralParamFragment: "+messageEvent.data)
        mViewModel.disposeData(messageEvent)
    }

    override fun initObserve() {
        lifecycle.addObserver(mViewModel.getThreadGeneralParam())
        observeLiveData(mViewModel.readAllDataSuccess, ::refreshStataPage)


    }
    private fun refreshStataPage(data: List<Int>) {
        closeReParamAdapter?.setData(data)
    }
    override fun initRequestData() {

    }

    private fun showOkCancelDialog(position: Int, data: String) {
        if (okCancelDialog == null) {
            okCancelDialog = this@CloseReminParamFragment.context?.let { OKCancelDialog(it) }
        }
        okCancelDialog?.setListener(object : DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    okCancelDialog?.type1 -> {
                        okCancelDialog?.dismiss()
                        Log.e("backinfo","屏蔽报警："+data)
                        mViewModel.sqWriteData(data)

                    }

                    else -> {}
                }
            }

        })

        okCancelDialog?.show()
    }

}