package com.grkj.iscs_mc.view.fragment

import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialManualReplacementBinding
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.model.vo.material.MaterialPropertyValuePageRespVO
import com.grkj.iscs_mc.model.vo.material.MaterialTypePageRespVO
import com.grkj.iscs_mc.model.vo.replacement.ManualReplacementListRespVO
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.iview.IMaterialManualReplacementView
import com.grkj.iscs_mc.view.presenter.MaterialManualReplacementPresenter
import com.grkj.iscs_mc.view.widget.ExpandableTabLayout
import com.grkj.iscs_mc.view.widget.SelectableInput
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资更换手动操作页
 */
class MaterialManualReplacementFragment :
    BaseMvpFragment<IMaterialManualReplacementView, MaterialManualReplacementPresenter, FragmentMaterialManualReplacementBinding>() {

    private val mMaterialList = mutableListOf<ManualReplacementListRespVO>()
    private val mDetailList = mutableListOf<ManualReplacementListRespVO.MaterialsPageVO>()
    private val mTypeList = mutableListOf<MaterialTypePageRespVO.Record>()
    private val mPropertyList = mutableListOf<MaterialPropertyValuePageRespVO.Record>()

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

    override fun initView() {
        mBinding?.cbBack?.setOnClickListener { navigateUp() }

        mBinding?.rvReplacement?.adapter =
            object : CommonAdapter<ManualReplacementListRespVO.MaterialsPageVO>(
                requireContext(),
                R.layout.item_rv_manual_replacement,
                mDetailList
            ) {
                override fun convert(
                    holder: ViewHolder,
                    vo: ManualReplacementListRespVO.MaterialsPageVO,
                    position: Int
                ) {
                    // 旧
                    holder.getView<SelectableInput>(R.id.ci_name_old).setText(vo.materialsName)
                    holder.getView<SelectableInput>(R.id.ci_type_old)
                        .setText(mTypeList.find { it.materialsTypeId == vo.materialsTypeId }?.materialsTypeName)
                    holder.getView<SelectableInput>(R.id.ci_model_old)
                        .setText(mPropertyList.find { it.recordId.toString() == vo.propertiesValueId }?.valueName)
                    holder.getView<SelectableInput>(R.id.ci_rfid_old).setText(vo.materialsRfid)
                    holder.getView<SelectableInput>(R.id.ci_date_old).setText(vo.expirationDate)
                    Glide.with(this@MaterialManualReplacementFragment).load(vo.materialsTypePicture)
                        .into(holder.getView(R.id.iv_old))

                    // 新
                    val ciName = holder.getView<SelectableInput>(R.id.ci_name_new)
                    ciName.setText(vo.newName)
                    ciName.setOnTextChangeListener(object : SelectableInput.OnTextChangeListener {
                        override fun onTextChange(str: String?) {
                            vo.newName = str
                        }
                    })

                    // 类型
                    val ciType = holder.getView<SelectableInput>(R.id.ci_type_new)
                    ciType.setOptionList(mTypeList.map { it.materialsTypeName!! }.toMutableList())
                    ciType.setText(mTypeList.find { it.materialsTypeId == vo.selectedTypeId }?.materialsTypeName)
                    ciType.setOnSpinnerSelectListener(object :
                        SelectableInput.OnSpinnerSelectListener {
                        override fun onSelect(str: String?, index: Int) {
                            if (vo.selectedTypeId != mTypeList[index].materialsTypeId) {
                                vo.selectedModelId = null
                            }
                            vo.selectedTypeId = mTypeList[index].materialsTypeId
                            mBinding?.rvReplacement?.adapter?.notifyItemChanged(position)
                        }
                    })

                    // 型号
                    val ciModel = holder.getView<SelectableInput>(R.id.ci_model_new)
                    ciModel.setOptionList(mPropertyList
                        .filter {
                            it.propertyId.toString() ==
                                    mTypeList.find { it.materialsTypeId == vo.selectedTypeId }?.propertyIds
                        }
                        .map { it.valueName!! }
                        .toMutableList())
                    ciModel.setText(mPropertyList.find { it.recordId == vo.selectedModelId }?.valueName)
                    ciModel.setOnSpinnerSelectListener(object :
                        SelectableInput.OnSpinnerSelectListener {
                        override fun onSelect(str: String?, index: Int) {
                            vo.selectedModelId = mPropertyList[index].recordId
                        }
                    })

                    val ciRfid = holder.getView<SelectableInput>(R.id.ci_rfid_new)
                    ciRfid.setText(vo.newRfid)
                    ciRfid.setOnTextChangeListener(object : SelectableInput.OnTextChangeListener {
                        override fun onTextChange(str: String?) {
                            vo.newRfid = str
                        }
                    })

                    val ciDate = holder.getView<SelectableInput>(R.id.ci_date_new)
                    ciDate.setText(vo.newDate)
                    ciDate.setOnTextChangeListener(object : SelectableInput.OnTextChangeListener {
                        override fun onTextChange(str: String?) {
                            vo.newDate = str
                        }
                    })

                    Glide.with(this@MaterialManualReplacementFragment)
                        .load(mTypeList.find { it.materialsTypeId == vo.selectedTypeId }?.materialsTypePicture)
                        .into(holder.getView(R.id.iv_new))

                    // 按钮
                    holder.setOnClickListener(R.id.tv_reset) {
                        vo.selectedTypeId = vo.materialsTypeId
                        vo.selectedModelId = vo.propertiesValueId?.toLong()
                        mBinding?.rvReplacement?.adapter?.notifyItemChanged(position)
                    }

                    holder.setOnClickListener(R.id.tv_confirm) {
                        // TODO 确定更换
                    }
                }
            }

        mBinding?.etlType?.setOnSelectListener(object : ExpandableTabLayout.OnSelectListener {
            override fun onSelect(position: Int) {
                mDetailList.clear()
                mMaterialList[position].materials?.let {
                    mDetailList.addAll(it)
                }
                mBinding?.rvReplacement?.adapter?.notifyDataSetChanged()
            }
        })

        presenter?.getManualReplacementList {
            it?.let {
                mMaterialList.addAll(it)
            }
            val list = mutableListOf<ExpandableTabLayout.ItemData>()
            it?.forEach { itData ->
                list.add(
                    ExpandableTabLayout.ItemData(
                        "${itData.materialsTypeName}(${itData.exNumber})",
                        iconUrl = itData.materialsTypeIcon
                    )
                )
            }
            mBinding?.etlType?.setData(list)

            mDetailList.clear()
            if (mMaterialList.isNotEmpty()) {
                mMaterialList[0].materials?.let {
                    mDetailList.addAll(it)
                }
                mDetailList.forEach {
                    it.newName = it.materialsName
                    it.newRfid = it.materialsRfid
                    it.newDate = it.expirationDate
                    it.selectedTypeId = it.materialsTypeId
                    it.selectedModelId = it.propertiesValueId?.toLong()
                }
            }
            mBinding?.rvReplacement?.adapter?.notifyDataSetChanged()
        }

        presenter?.getMaterialType {
            if (!it?.records.isNullOrEmpty()) {
                mTypeList.addAll(it?.records!!)
                mBinding?.rvReplacement?.adapter?.notifyDataSetChanged()
            }
        }

        presenter?.getMaterialProperty {
            if (!it.isNullOrEmpty()) {
                mPropertyList.addAll(it)
                mBinding?.rvReplacement?.adapter?.notifyDataSetChanged()
            }
        }
    }

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