package com.grkj.iscs.view.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.google.gson.Gson
import com.grkj.iscs.R
import com.grkj.iscs.databinding.FragmentSystemSettingBinding
import com.grkj.iscs.databinding.LayoutSelectableinputSpinnerBinding
import com.grkj.iscs.util.SPUtils
import com.grkj.iscs.util.byteToHexString
import com.grkj.iscs.view.base.BaseMvpFragment
import com.grkj.iscs.view.iview.ISystemSettingView
import com.grkj.iscs.view.presenter.SystemSettingPresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 系统设置页
 */
class SystemSettingFragment : BaseMvpFragment<ISystemSettingView, SystemSettingPresenter, FragmentSystemSettingBinding>() {

    private var mDockTypeList = mutableListOf<DockItem>()
    private var mPortList = mutableListOf<String>()
    private var mDropdownView: View? = null
    private var mTypePop: PopupWindow? = null
    private var mPortPop: PopupWindow? = null
    private var mDockList = mutableListOf<DockTestFragment.DockTestBean>()
    private var mSelectedBoardType: Byte? = null

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

    override fun initView() {
        mDockList.addAll(presenter?.getDockList()!!)

        mDockTypeList.addAll(presenter?.getDockTypeList()!!)

        mBinding?.rlSpinnerType?.setOnClickListener {
            showBoardTypeDropDown()
        }

        mBinding?.tvAdd?.setOnClickListener {
            presenter?.addBoard(mBinding?.etAddress?.text.toString(), mSelectedBoardType, mDockList) {
                mBinding?.rvBoard?.adapter?.notifyDataSetChanged()
            }
        }

        mBinding?.rvBoard?.adapter = object : CommonAdapter<DockTestFragment.DockTestBean>(
            requireActivity(),
            R.layout.item_rv_board,
            mDockList
        ) {
            override fun convert(holder: ViewHolder, t: DockTestFragment.DockTestBean, position: Int) {
                mDockTypeList.find { it.type == t.type }?.name?.let {
                    holder.setText(R.id.tv_board, it)
                } ?: {
                    holder.setText(R.id.tv_board, "")
                }
                holder.setText(R.id.tv_address, byteToHexString(t.address))
                holder.setText(R.id.tv_action, getString(R.string.delete))
                holder.setOnClickListener(R.id.tv_action) {
                    mDockList.removeAt(position)
                    mBinding?.rvBoard?.adapter?.notifyDataSetChanged()
                    SPUtils.saveDockConfig(requireActivity(), Gson().toJson(mDockList))
                }
            }
        }

        mPortList.addAll(presenter?.getSerialPorts()!!)

        mBinding?.rlSpinnerPort?.setOnClickListener {
            showPortDropDown()
        }

        mBinding?.tvPort?.text = SPUtils.getPortConfig(requireActivity())

        mBinding?.cbSave?.setOnClickListener {
            presenter?.saveConfig(requireContext(), mDockList, mBinding?.tvPort?.text.toString())
        }
    }

    private fun showBoardTypeDropDown() {
        mDropdownView ?: let {
            mDropdownView = LayoutInflater.from(requireActivity())
                .inflate(R.layout.layout_selectableinput_spinner, null)
        }

        mTypePop ?: let {
            mTypePop = PopupWindow(context)
            mTypePop?.contentView = mDropdownView
            mTypePop?.width = mBinding?.rlSpinnerType!!.width
            mTypePop?.height = ViewGroup.LayoutParams.WRAP_CONTENT
            mTypePop?.isOutsideTouchable = true
            mTypePop?.isFocusable = true

            val popBinding = LayoutSelectableinputSpinnerBinding.bind(mDropdownView!!)
            popBinding.rvOptions.adapter = object : CommonAdapter<DockItem>(
                requireActivity(),
                R.layout.item_rv_selectableinput_spinner,
                mDockTypeList
            ) {
                override fun convert(holder: ViewHolder?, option: DockItem?, position: Int) {
                    holder?.setText(R.id.tv_option, option?.name)
                    holder?.setOnClickListener(R.id.tv_option) {
                        mBinding?.tvBoardType?.text = option?.name
                        mTypePop?.dismiss()
                        mSelectedBoardType = option?.type
                    }
                }
            }
        }

        mTypePop?.showAsDropDown(mBinding?.rlSpinnerType)
    }

    private fun showPortDropDown() {
        mDropdownView ?: let {
            mDropdownView = LayoutInflater.from(requireActivity())
                .inflate(R.layout.layout_selectableinput_spinner, null)
        }

        mPortPop ?: let {
            mPortPop = PopupWindow(context)
            mPortPop?.contentView = mDropdownView
            mPortPop?.width = mBinding?.rlSpinnerType!!.width
            mPortPop?.height = ViewGroup.LayoutParams.WRAP_CONTENT
            mPortPop?.isOutsideTouchable = true
            mPortPop?.isFocusable = true

            val popBinding = LayoutSelectableinputSpinnerBinding.bind(mDropdownView!!)
            popBinding.rvOptions.adapter = object : CommonAdapter<String>(
                requireActivity(),
                R.layout.item_rv_selectableinput_spinner,
                mPortList
            ) {
                override fun convert(holder: ViewHolder?, option: String, position: Int) {
                    holder?.setText(R.id.tv_option, option)
                    holder?.setOnClickListener(R.id.tv_option) {
                        mBinding?.tvPort?.text = option
                        mPortPop?.dismiss()
                    }
                }
            }
        }

        mPortPop?.showAsDropDown(mBinding?.rlSpinnerPort)
    }

    override fun initPresenter(): SystemSettingPresenter {
        return SystemSettingPresenter()
    }

    data class DockItem(val name: String, val type: Byte)
}