package com.grkj.iscs_mc.view.activity

import android.widget.ImageView
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.ActivityHomeBinding
import com.grkj.iscs_mc.view.adapter.MenuAdapter
import com.grkj.iscs_mc.view.base.BaseFragment
import com.grkj.iscs_mc.view.base.BaseMvpActivity
import com.grkj.iscs_mc.view.fragment.ExceptionHandleFragment
import com.grkj.iscs_mc.view.fragment.MaterialManagementFragment
import com.grkj.iscs_mc.view.fragment.SettingFragment
import com.grkj.iscs_mc.view.iview.IHomeView
import com.grkj.iscs_mc.view.presenter.HomePresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 首页
 */
class HomeActivity : BaseMvpActivity<IHomeView, HomePresenter, ActivityHomeBinding>() {

    private var mMenuList = mutableListOf<Menu>()

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

    override fun initView() {
        mMenuList.add(Menu(getString(R.string.material_management), R.mipmap.material_management, MaterialManagementFragment()))
        mMenuList.add(Menu(getString(R.string.exception_handle), R.mipmap.exception_handle, ExceptionHandleFragment()))
        mMenuList.add(Menu(getString(R.string.settings), R.mipmap.settings, SettingFragment()))

        mBinding?.itemSetting?.ivIcon?.setImageResource(R.mipmap.settings)
        mBinding?.itemSetting?.tvName?.text = getString(R.string.settings)
        mBinding?.itemSetting?.root?.setOnClickListener {
            mBinding?.vp?.currentItem = mMenuList.size - 1
            mBinding?.itemSetting?.root?.setBackgroundColor(getColor(R.color.common_bg_white_30))
            mBinding?.rvMenu?.adapter?.notifyDataSetChanged()
        }
        if (mMenuList.size == 1) {
            mBinding?.itemSetting?.root?.setBackgroundColor(getColor(R.color.common_bg_white_30))
        }

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

        mBinding?.rvMenu?.adapter = object : CommonAdapter<Menu>(this, R.layout.item_rv_menu, mMenuList) {
            override fun convert(holder: ViewHolder, data: Menu, position: Int) {
                holder.setVisible(R.id.root, data.title != getString(R.string.settings))
                holder.setText(R.id.tv_name, data.title)
                holder.getView<ImageView>(R.id.iv_icon).setImageResource(data.icon!!)
                holder.setOnClickListener(R.id.root) {
                    mBinding?.itemSetting?.root?.setBackgroundColor(0)
                    mBinding?.vp?.currentItem = position
                    notifyDataSetChanged()
                }
                holder.setBackgroundColor(R.id.root,
                    if (position == mBinding?.vp?.currentItem)
                        getColor(R.color.common_bg_white_30)
                    else 0)
            }
        }
    }

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

    data class Menu(val title: String? = null, val icon: Int? = null, val fragment: BaseFragment<*>)
}