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.alibaba.android.arouter.launcher.ARouter
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.GeneralParamAdapter
import com.module.backstage.databinding.BackstageFragmentGeneralParamBinding
import com.module.backstage.dialog.OKCancelDialog
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.constant.RouteUrl
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 下午
 */
@AndroidEntryPoint
class GeneralParamFragment :
    BaseFragment<BackstageFragmentGeneralParamBinding, GeneralParamFragmentVM>() {

    private val TAG = "GeneralParamFragment"
    var okCancelDialog: OKCancelDialog? = null

    private var debugParamAdapter: GeneralParamAdapter? = null
    override val mViewModel: GeneralParamFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentGeneralParamBinding.inflate(layoutInflater)

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

    override fun BackstageFragmentGeneralParamBinding.initView() {
        /*  with(rvOtherSearch) {
              //设置布局排列方式，默认垂直排列
              val gridLayoutManager: GridLayoutManager =
                  GridLayoutManager(
                      this@GeneralParamFragment.context,
                      4,
                      GridLayoutManager.VERTICAL,
                      false
                  )
              layoutManager = gridLayoutManager

              debugParamAdapter = GeneralParamAdapter(mViewModel.getGeneralParamData())
              debugParamAdapter!!.setItemListener(object : AdapterClickListener {
                  override fun onClickListener(view: View?, position: Int, data: String?) {
                      if (data == null) {
                          addAllKeyboardView(view)
                          return
                      }
                      if ("".equals(data)) {
                          ToastUtil.switchToastStyleToWarn(UiUtil.getStringRes(R.string.backstage_data_null_tips))
                          return
                      }
                      showOkCancelDialog(position, data)
                  }

              })
              adapter = debugParamAdapter
          }*/
        //   mViewModel.startData()
        generalParamBt.setOnClickListener {
            ARouter.getInstance()
                .build(RouteUrl.Home.GeneralParamActivity)
                .navigation()
        }
        coreparameterBt.setOnClickListener {
            ARouter.getInstance()
                .build(RouteUrl.Home.SugarParametersActivity)
                .navigation()
        }
        advamceparameter.setOnClickListener {
            ARouter.getInstance()
                .build(RouteUrl.Home.AdvamceParametersActivity)
                .navigation()
        }
        actionParameters.setOnClickListener {  ARouter.getInstance()
            .build(RouteUrl.Home.ActionParametersActivity)
            .navigation() }
        rollingparameter.setOnClickListener {
            ARouter.getInstance()
                .build(RouteUrl.Home.RollingParamActivity)
                .navigation()
        }
    }

    /* @Subscribe(threadMode = ThreadMode.MAIN)
     fun event(messageEvent: Message) {
         Log.d(TAG, "messageEvent event: "+messageEvent.type)
         when (messageEvent.type) {
             Message.Type.ack -> {mViewModel.ack(messageEvent)}
             Message.Type.response -> {mViewModel.response(messageEvent)}
             Message.Type.connected -> {}
             Message.Type.disconnected -> {}
             Message.Type.sendError -> {}
             Message.Type.nak -> {}
         }
     }*/


    /* @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, ::readAllDataSuccess)
         observeLiveData(mViewModel.clickPosition, ::clickPosition)*/

    }

    private fun clickPosition(i: Int) {

    }

    private fun readAllDataSuccess(data: List<Int>) {

        debugParamAdapter?.setData(data);

    }

    override fun initRequestData() {
        mViewModel.getArticleData()
    }

    /* private fun showOkCancelDialog(position: Int, data: String) {
         if (okCancelDialog == null) {
             okCancelDialog = this@GeneralParamFragment.context?.let { OKCancelDialog(it) }
         }
         okCancelDialog?.setListener(object : DialogClickListener {
             override fun onClickListener(type: Int, text: String?) {
                 when (type) {
                     okCancelDialog?.type1 -> {
                         okCancelDialog?.dismiss()
                         mViewModel.sqWriteData(data)

                     }

                     else -> {}
                 }
             }

         })

         okCancelDialog?.show()
     }*/

}