package com.grkj.iscs_mc.view.fragment

import android.view.View
import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialReplacementBinding
import com.grkj.iscs_mc.extentions.navigateTo
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.model.vo.replacement.ManualReplacementListRespVO
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.iview.IMaterialReplacementView
import com.grkj.iscs_mc.view.presenter.MaterialReplacementPresenter
import com.grkj.iscs_mc.view.widget.ExpandableTabLayout
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资管理 - 物资更换
 */
class MaterialReplacementFragment :
    BaseMvpFragment<IMaterialReplacementView, MaterialReplacementPresenter, FragmentMaterialReplacementBinding>() {

    private val mTypeList = mutableListOf<ManualReplacementListRespVO>()
    private val mDetailList = mutableListOf<ManualReplacementListRespVO.MaterialsPageVO>()

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

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

        mBinding?.rvMaterial?.adapter =
            object : CommonAdapter<ManualReplacementListRespVO.MaterialsPageVO>(
                requireContext(),
                R.layout.item_rv_replacement,
                mDetailList
            ) {
                override fun convert(
                    holder: ViewHolder,
                    vo: ManualReplacementListRespVO.MaterialsPageVO,
                    position: Int
                ) {
                    holder.setText(R.id.tv_name, vo.materialsName ?: "")
                    holder.setText(R.id.tv_rfid, vo.materialsRfid ?: "")
                    Glide.with(this@MaterialReplacementFragment).load(vo.materialsTypePicture).into(holder.getView(R.id.iv_icon))
                    holder.setText(R.id.tv_tip, getStatusStr(vo.status))
                    holder.setBackgroundRes(R.id.ll_tip, getStatusColor(vo.status))
                    holder.setImageResource(R.id.iv_tip, getStatusIcon(vo.status))
                }
            }

        mBinding?.etlType?.setOnSelectListener(object : ExpandableTabLayout.OnSelectListener {
            override fun onSelect(position: Int) {
                mDetailList.clear()
                mTypeList[position].materials?.let {
                    mDetailList.addAll(it)
                }
                mBinding?.tvTip?.visibility = if (mDetailList.isEmpty()) View.VISIBLE else View.GONE
                mBinding?.rvMaterial?.adapter?.notifyDataSetChanged()
            }
        })

        mBinding?.cbManualReplace?.setOnClickListener {
            navigateTo(MaterialReplacementFragmentDirections.actionReplacementFragmentToManualReplacementFragment())
        }

        presenter?.getManualReplacementList {
            it?.let {
                mTypeList.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 (mTypeList.isNotEmpty()) {
                mTypeList[0].materials?.let {
                    mDetailList.addAll(it)
                }
                mBinding?.tvTip?.visibility = if (mDetailList.isEmpty()) View.VISIBLE else View.GONE
            }
            mBinding?.rvMaterial?.adapter?.notifyDataSetChanged()
        }
    }

    fun getStatusStr(status: String?): String {
        return when (status) {
            "0" -> getString(R.string.normal)
            "1" -> getString(R.string.expired)
            "2" -> getString(R.string.damaged)
            else -> ""
        }
    }

    fun getStatusColor(status: String?): Int {
        return when (status) {
            "0" -> R.color.common_btn_green_bg
            "1" -> R.color.common_btn_yellow_bg
            "2" -> R.color.common_btn_red_bg
            else -> R.color.main_color
        }
    }

    fun getStatusIcon(status: String?): Int {
        return when (status) {
            "0" -> R.mipmap.ok_normal
            "1" -> R.mipmap.expired_normal
            "2" -> R.mipmap.damaged_normal
            else -> R.mipmap.ok_normal
        }
    }

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