package com.grkj.iscs.view.fragment

import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.grkj.iscs.R
import com.grkj.iscs.databinding.FragmentTechnologySopBinding
import com.grkj.iscs.model.Constants.mSopTypeList
import com.grkj.iscs.model.bo.PageChangeBO
import com.grkj.iscs.model.vo.machinery.MachineryPageRespVO
import com.grkj.iscs.util.log.LogUtil
import com.grkj.iscs.view.base.BaseMvpFragment
import com.grkj.iscs.view.iview.ITechnologySopView
import com.grkj.iscs.view.presenter.TechnologySopPresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 工艺SOP页
 *
 * @param changePage 页面index、工作票ID
 */
class TechnologySopFragment(val changePage: (PageChangeBO) -> Unit) :
    BaseMvpFragment<ITechnologySopView, TechnologySopPresenter, FragmentTechnologySopBinding>() {

    private val mMachineryList = mutableListOf<MachineryPageRespVO.Record>()
    private var mMachineryIdx = -1
    private var mSopTypeIdx = -1

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

    override fun initView() {
        mBinding?.rvTechnology?.adapter = object : CommonAdapter<MachineryPageRespVO.Record>(
            requireActivity(),
            R.layout.item_rv_technology_sop,
            mMachineryList
        ) {
            override fun convert(holder: ViewHolder, data: MachineryPageRespVO.Record?, parPos: Int) {
                holder.getView<LinearLayout>(R.id.root).isSelected = parPos == mMachineryIdx
                holder.setText(R.id.tv_name, data?.machineryName)
                Glide.with(this@TechnologySopFragment).load(data?.machineryImg).into(holder.getView(R.id.iv_pic))
                holder.setOnClickListener(R.id.root) {
                    if (mMachineryIdx != parPos) {
                        mSopTypeIdx = -1
                    }
                    mMachineryIdx = parPos
                    refreshSelected()
                    notifyDataSetChanged()
                }

                val recyclerView = holder.getView<RecyclerView>(R.id.rv_type)
                recyclerView.adapter = object : CommonAdapter<MachineryPageRespVO.Record.SysDictData>(
                    requireActivity(),
                    R.layout.item_rv_technology_sop_type,
                    data?.sysDictDatas ?: mutableListOf()
                ) {
                    override fun convert(holder: ViewHolder, type: MachineryPageRespVO.Record.SysDictData?, childPos: Int) {
                        holder.getView<LinearLayout>(R.id.root).isSelected = (childPos == mSopTypeIdx && parPos == mMachineryIdx)
                        type?.dictLabel?.let {
                            holder.setText(R.id.tv_name, it)
                        }
                        mSopTypeList.find { it.type.toString() == type?.dictValue }?.icon?.let { icon ->
                            holder.getView<ImageView>(R.id.iv_type).setImageResource(icon)
                        }
                        holder.setOnClickListener(R.id.root) {
                            mMachineryIdx = parPos
                            mSopTypeIdx = childPos
                            mBinding?.rvTechnology?.adapter?.notifyDataSetChanged()
                            refreshSelected()
                            notifyDataSetChanged()
                        }
                    }
                }
            }
        }

        mBinding?.cbStart?.setOnClickListener {
            presenter?.createTicket(mMachineryIdx, mSopTypeIdx, mMachineryList) {
                it?.let {
                    changePage(PageChangeBO(2, ticketId = it, machineryId = mMachineryList[mMachineryIdx].machineryId))
                }
            }
        }
    }

    override fun refreshPage(pageChangeBO: PageChangeBO) {
        LogUtil.i("$pageChangeBO")
        mBinding?.tvTitle?.text = pageChangeBO.machineryName
        presenter?.getMachineryPage(pageChangeBO.workstationId!!) {
            mMachineryList.clear()
            it?.records?.let {
                mMachineryList.addAll(it)
            }
            mBinding?.rvTechnology?.adapter?.notifyDataSetChanged()
            mMachineryIdx = -1
            mSopTypeIdx = -1
            refreshSelected()
        }
    }

    private fun refreshSelected() {
        val sopName = if (mMachineryIdx == -1) {
            ""
        } else {
            "-${mMachineryList[mMachineryIdx].machineryName}"
        }
        val sopTypeName = if (mSopTypeIdx == -1) {
            ""
        } else {
            "-${mMachineryList[mMachineryIdx].sysDictDatas?.get(mSopTypeIdx)?.dictLabel}"
        }
        mBinding?.tvSelected?.text = getString(R.string.current_sop, "${mBinding?.tvTitle?.text}$sopName$sopTypeName")
    }

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