package com.grkj.iscs_mc.view.fragment

import android.widget.ImageView
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialInstructionBinding
import com.grkj.iscs_mc.extentions.navigateTo
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.model.Constants.FILE_TYPE_MP4
import com.grkj.iscs_mc.model.Constants.FILE_TYPE_PDF
import com.grkj.iscs_mc.model.vo.material.MaterialInstructionListRespVO
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.iview.IMaterialInstructionView
import com.grkj.iscs_mc.view.presenter.MaterialInstructionPresenter
import com.grkj.iscs_mc.view.widget.ExpandableTabLayout
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资管理 - 物资使用说明
 */
class MaterialInstructionFragment :
    BaseMvpFragment<IMaterialInstructionView, MaterialInstructionPresenter, FragmentMaterialInstructionBinding>() {

    private var mInstructionList: MutableList<MaterialInstructionListRespVO>? = null
    private var mDetailList = mutableListOf<MaterialInstructionListRespVO.IsMaterialsInstructions>()

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

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

        mBinding?.rvInstruction?.adapter =
            object : CommonAdapter<MaterialInstructionListRespVO.IsMaterialsInstructions>(
                requireContext(),
                R.layout.item_rv_instruction,
                mDetailList
            ) {
                override fun convert(
                    holder: ViewHolder,
                    vo: MaterialInstructionListRespVO.IsMaterialsInstructions,
                    position: Int
                ) {
                    getInstructionPic(vo.fileType)?.let {
                        holder.getView<ImageView>(R.id.iv_type).setImageResource(it)
                    }
                    holder.setText(R.id.tv_name, vo.instructionsTitle)
                    holder.setOnClickListener(R.id.root) {
                        val url = if (vo.fileType == FILE_TYPE_PDF) vo.fileUrlImg else vo.fileUrl
                        url?.let {
                            navigateTo(MaterialInstructionFragmentDirections.actionInstructionFragmentToInstructionDetailFragment(it))
                        }
                    }
                }
        }

        mBinding?.etlType?.setOnSelectListener(object : ExpandableTabLayout.OnSelectListener {
            override fun onSelect(position: Int) {
                mDetailList.clear()
                mInstructionList?.get(position)?.instructions?.let {
                    mDetailList.addAll(it)
                }
                mBinding?.rvInstruction?.adapter?.notifyDataSetChanged()
            }
        })

        presenter?.getMaterialInstructionList {
            mInstructionList = it
            val list = mutableListOf<ExpandableTabLayout.ItemData>()
            it?.forEach { itData ->
                // TODO icon不对
                list.add(ExpandableTabLayout.ItemData("${itData.materialsTypeName}(${itData.number})", R.mipmap.safety_helmet))
            }
            mBinding?.etlType?.setData(list)

            mDetailList.clear()
            if (mInstructionList?.isNotEmpty() == true) {
                mInstructionList?.get(0)?.instructions?.let {
                    mDetailList.addAll(it)
                }
            }
            mBinding?.rvInstruction?.adapter?.notifyDataSetChanged()
        }
    }

    fun getInstructionPic(fileType: String?): Int? {
        return when (fileType) {
            FILE_TYPE_MP4 -> R.mipmap.instruction_mp4
            FILE_TYPE_PDF -> R.mipmap.instruction_pdf
            else -> null
        }
    }

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