package com.module.backstage.fragment.other

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.adapter.TestAdapter
import com.module.backstage.databinding.BackstageFragmentSystemSettingsBinding
import com.module.backstage.dialog.OKCancelDialog
import com.module.backstage.listener.UpdateClickListener
import com.module.backstage.vm.SystemSettingsFragmentVM
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import dagger.hilt.android.AndroidEntryPoint


/**
 * 系统设置 Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class SystemSettingsFragment :
    BaseFragment<BackstageFragmentSystemSettingsBinding, SystemSettingsFragmentVM>(),
    AdapterClickListener, UpdateClickListener {

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

    private var otherAdapter: TestAdapter? = null
    override val mViewModel: SystemSettingsFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentSystemSettingsBinding.inflate(layoutInflater)

    override fun onDestroyView() {
        mBinding.rvOtherSearch.adapter = null
        super.onDestroyView()
    }
    var functionIndex=0
    override fun BackstageFragmentSystemSettingsBinding.initView() {
        if (arguments!=null){
            functionIndex = arguments?.getInt("FUNCTION")!!
        }
        functionIndex?.let { initRcy(it) }

    }

    fun initRcy(functionIndex:Int) {
        mBinding.rvOtherSearch.let {
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@SystemSettingsFragment.context,
                    2,
                    androidx.recyclerview.widget.GridLayoutManager.VERTICAL,
                    false
                )
            it.layoutManager = gridLayoutManager


            otherAdapter = TestAdapter(mViewModel.getGeneralParamData(functionIndex))

            otherAdapter!!.setItemListener(this)
            it.adapter = otherAdapter
        //    it.addItemDecoration(HorizontalSpaceItemDecoration(100))
        }

        otherAdapter?.setOnLongClickListener(this)
    }


    override fun initObserve() {
    }


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

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

                    }

                    else -> {}
                }
            }

        })

        okCancelDialog?.show()
    }

    override fun onClickListener(view: View?, position: Int, data: String?) {
        if (data == null) {
            addAllKeyboardView(view)
            return
        }
        if ("".equals(data)) {
            com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil.switchToastStyleToWarn(
                com.quyunshuo.androidbaseframemvvm.common.util.UiUtil.getStringRes(com.module.backstage.R.string.base_backstage_data_null_tips)
            )
            return
        }
        showOkCancelDialog(position, data)
    }

    override fun setOnLongClickListener(type: Int, position: Int, data: String?) {
   /*     otherAdapter = TestAdapter(mViewModel.getGeneralParamData(functionIndex))
        rvOtherSearch.adapter = otherAdapter*/
        initRcy(functionIndex)
     //   otherAdapter?.setData(mViewModel.getGeneralParamData(functionIndex))
    }
}