package com.grkj.iscs.view.fragment

import android.widget.ImageView
import com.grkj.iscs.R
import com.grkj.iscs.databinding.FragmentJobManagementBinding
import com.grkj.iscs.model.bo.PageChangeBO
import com.grkj.iscs.util.log.LogUtil
import com.grkj.iscs.view.activity.HomeActivity.Menu
import com.grkj.iscs.view.adapter.MenuAdapter
import com.grkj.iscs.view.base.BaseMvpFragment
import com.grkj.iscs.view.iview.IJobManagementView
import com.grkj.iscs.view.presenter.JobManagementPresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 作业管理页
 */
class JobManagementFragment :
    BaseMvpFragment<IJobManagementView, JobManagementPresenter, FragmentJobManagementBinding>() {

    private lateinit var mMenuList: MutableList<Menu>

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

    override fun initView() {
        mMenuList = mutableListOf(
            Menu(getString(R.string.workshop), R.mipmap.workshop, WorkshopFragment { pageChangeBO ->
                changePage(pageChangeBO)
            }),
            Menu(getString(R.string.technology_sop), R.mipmap.technology, TechnologySopFragment { pageChangeBO ->
                changePage(pageChangeBO)
            }),
            Menu(getString(R.string.job_execution), R.mipmap.job_execution, JobExecutionFragment { pageChangeBO ->
                changePage(pageChangeBO)
            })
        )

        mBinding?.vp?.isUserInputEnabled = false
        mBinding?.vp?.offscreenPageLimit = mMenuList.size
        mBinding?.vp?.adapter = MenuAdapter(requireActivity().supportFragmentManager, lifecycle, mMenuList)

        mBinding?.rvTab?.adapter = object : CommonAdapter<Menu>(activity, R.layout.item_rv_job_management, mMenuList) {
            override fun convert(holder: ViewHolder, tab: Menu, position: Int) {
                holder.getView<ImageView>(R.id.iv).setImageResource(tab.icon!!)
                holder.setText(R.id.tv_name, tab.title)
                holder.setOnClickListener(R.id.root) {
                    if (position == 0) {
                        mBinding?.vp?.currentItem = 0
                        (mMenuList[0].fragment as WorkshopFragment).refreshPage()
                        notifyDataSetChanged()
                    }
                }
                holder.setVisible(R.id.v_indicator, position == mBinding?.vp?.currentItem)
            }
        }
    }

    private fun changePage(pageChangeBO: PageChangeBO) {
        LogUtil.i("$pageChangeBO")
        when (pageChangeBO.pageIdx) {
            -1 -> (mMenuList[0].fragment as WorkshopFragment).refreshPage()
            1 -> (mMenuList[1].fragment as TechnologySopFragment).refreshPage(pageChangeBO)
            2 -> (mMenuList[2].fragment as JobExecutionFragment).refreshPage(pageChangeBO)
        }
        mBinding?.vp?.currentItem = if (pageChangeBO.pageIdx == -1) 0 else pageChangeBO.pageIdx
        mBinding?.rvTab?.adapter?.notifyDataSetChanged()
    }

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