package com.grkj.iscs.view.fragment

import android.view.View
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.grkj.iscs.R
import com.grkj.iscs.databinding.FragmentDockTestBinding
import com.grkj.iscs.modbus.ModBusController
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
import com.grkj.iscs.util.SPUtils
import com.grkj.iscs.view.base.BaseFragment
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 底座测试页
 */
class DockTestFragment : BaseFragment<FragmentDockTestBinding>() {

    private var mKeyDockList = mutableListOf<DockTestBean>()
    private var mLockDockList = mutableListOf<DockTestBean>()

    override val viewBinding: FragmentDockTestBinding
        get() = FragmentDockTestBinding.inflate(layoutInflater)

    override fun initView() {
        val dockConfigJson = SPUtils.getDockConfig(requireActivity())
        if (!dockConfigJson.isNullOrEmpty()) {
            val tempList: MutableList<DockTestBean> =
                Gson().fromJson(dockConfigJson, object : TypeToken<MutableList<DockTestBean>>() {}.type)
            if (tempList.isNotEmpty()) {
                mKeyDockList.addAll(tempList.filter { it.type == DOCK_TYPE_KEY })
                mLockDockList.addAll(tempList.filter { it.type == DOCK_TYPE_LOCK })
            }
            if (mKeyDockList.isEmpty()) {
                mBinding?.tvKeyTitle?.visibility = View.GONE
            }
            if (mLockDockList.isEmpty()) {
                mBinding?.tvLockTitle?.visibility = View.GONE
            }
            mKeyDockList.forEach { dock ->
                dock.deviceList = mutableListOf(1, 2)
            }
            mLockDockList.forEach { dock ->
                dock.deviceList = mutableListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            }
        }

        mBinding?.rvKey?.adapter = object :
            CommonAdapter<DockTestBean>(requireActivity(), R.layout.item_rv_dock_test, mKeyDockList) {
            override fun convert(holder: ViewHolder, dock: DockTestBean, position: Int) {
                holder.setText(R.id.tv_address, "0x${String.format("%02X", dock.address)}")
                val rvDevice = holder.getView<androidx.recyclerview.widget.RecyclerView>(R.id.rv_device)
                rvDevice.adapter = object : CommonAdapter<Int>(
                    requireActivity(), R.layout.item_rv_dock_test_child, dock.deviceList) {
                    override fun convert(holder: ViewHolder, deviceIndex: Int, position: Int) {
                        holder.setText(R.id.tv_name, getString(R.string.device_index, deviceIndex))
                        holder.setOnClickListener(R.id.tv_turn_on) {
                            ModBusController.controlKeyBuckle(true, deviceIndex == 1, dock.address)
                        }
                        holder.setOnClickListener(R.id.tv_turn_off) {
                            ModBusController.controlKeyBuckle(false, deviceIndex == 1, dock.address)
                        }
                    }
                }
            }
        }

        mBinding?.rvLock?.adapter = object :
            CommonAdapter<DockTestBean>(requireActivity(), R.layout.item_rv_dock_test, mLockDockList) {
            override fun convert(holder: ViewHolder, dock: DockTestBean, position: Int) {
                holder.setText(R.id.tv_address, "0x${String.format("%02X", dock.address)}")
                val rvDevice = holder.getView<androidx.recyclerview.widget.RecyclerView>(R.id.rv_device)
                rvDevice.adapter = object : CommonAdapter<Int>(
                    requireActivity(), R.layout.item_rv_dock_test_child, dock.deviceList) {
                    override fun convert(holder: ViewHolder, deviceIndex: Int, position: Int) {
                        holder.setText(R.id.tv_name, getString(R.string.device_index, deviceIndex))
                        holder.setOnClickListener(R.id.tv_turn_on) {
                            ModBusController.controlLockBuckle(true, dock.address, deviceIndex - 1)
                        }
                        holder.setOnClickListener(R.id.tv_turn_off) {
                            ModBusController.controlLockBuckle(false, dock.address, deviceIndex - 1)
                        }
                    }
                }
            }
        }

        mBinding?.cbOpen?.setOnClickListener {
            mKeyDockList.forEach { dock ->
                dock.deviceList?.forEach { deviceIndex ->
                    ModBusController.controlKeyBuckle(true, deviceIndex == 1, dock.address)
                }
            }
            mLockDockList.forEach { dock ->
                dock.deviceList?.forEach { deviceIndex ->
                    ModBusController.controlLockBuckle(true, dock.address, deviceIndex - 1)
                }
            }
        }

        mBinding?.cbClose?.setOnClickListener {
            mKeyDockList.forEach { dock ->
                dock.deviceList?.forEach { deviceIndex ->
                    ModBusController.controlKeyBuckle(false, deviceIndex == 1, dock.address)
                }
            }
            mLockDockList.forEach { dock ->
                dock.deviceList?.forEach { deviceIndex ->
                    ModBusController.controlLockBuckle(false, dock.address, deviceIndex - 1)
                }
            }
        }
    }

    data class DockTestBean(val address: Byte, val type: Byte, var deviceList: MutableList<Int>? = null)
}