package com.gh.gamecenter.simulatorgame

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.databinding.FragmentSimulatorListBinding
import com.gh.gamecenter.entity.SimulatorEntity
import com.gh.gamecenter.eventbus.EBPackage
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SimulatorManagementFragment : ListFragment<SimulatorEntity, SimulatorManagementViewModel>() {

    private lateinit var mSimulatorEntity: SimulatorEntity
    private lateinit var mBinding: FragmentSimulatorListBinding
    private var mAdapter: SimulatorManagementAdapter? = null

    //安装、卸载事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(busFour: EBPackage) {
        if (busFour.isInstalledOrUninstalled()) {
            mAdapter?.notifyDataSetChanged()
        }
    }

    override fun getInflatedLayout() =
        FragmentSimulatorListBinding.inflate(layoutInflater).apply { mBinding = this }.root

    override fun getItemDecoration() = SpacingItemDecoration(onlyDecorateTheFirstItem = true, top = 8F.dip2px())

    override fun onCreate(savedInstanceState: Bundle?) {
        arguments?.apply {
            mSimulatorEntity = getParcelable(EntranceConsts.KEY_SIMULATOR) ?: SimulatorEntity()
        }
        super.onCreate(savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initMenu(R.menu.menu_simulator_uninstall)
    }

    override fun onMenuItemClick(menuItem: MenuItem) {
        super.onMenuItemClick(menuItem)
        if (menuItem.itemId == R.id.menu_simulator_uninstall) {
            val unInstallTv = menuItem.actionView?.findViewById<TextView>(R.id.unInstallTv)
            val completeTv = menuItem.actionView?.findViewById<TextView>(R.id.completeTv)
            val showFlag = unInstallTv?.visibility == View.VISIBLE
            if (showFlag) {
                unInstallTv?.visibility = View.GONE
                completeTv?.visibility = View.VISIBLE
            } else {
                unInstallTv?.visibility = View.VISIBLE
                completeTv?.visibility = View.GONE
            }
            mAdapter?.showUnInstallWindowAndSelectIv(showFlag)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mBinding.blankView.setOnClickListener {
            mAdapter?.dismissPopWindow()
        }
    }

    override fun provideListAdapter() =
        mAdapter ?: SimulatorManagementAdapter(requireContext(), this).apply { mAdapter = this }

    override fun provideListViewModel() = viewModelProvider<SimulatorManagementViewModel>()


    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mAdapter?.updatePopupWindow()
    }
}