package com.gh.gamecenter.gamecollection.choose

import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.gh.gamecenter.common.base.fragment.BaseLazyTabFragment
import com.gh.gamecenter.common.databinding.FragmentAddGamesBinding
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.utils.Utils

class NewAddGamesFragment: BaseLazyTabFragment() {
    private val binding by lazy {
        FragmentAddGamesBinding.inflate(layoutInflater, null, false)
    }
    private val chooseGamesViewModel: ChooseGamesViewModel by lazy { viewModelProvider(ChooseGamesViewModel.Factory()) }
    private val playedGameViewModel: AddUserPlayedGameViewModel by lazy {
        val userId = UserManager.getInstance().userId
        viewModelProviderFromParent(AddUserPlayedGameViewModel.Factory(userId, true), userId)
    }
    private val addUserPlayedGameFragment by lazy { AddUserPlayedGameFragment() }

    override fun getLayoutId(): Int = 0
    override fun getInflatedLayout(): View = binding.root
    override fun initFragmentList(fragments: MutableList<Fragment>) {
        fragments.add(addUserPlayedGameFragment)
        fragments.add(AddHotGamesFragment())
    }

    override fun initTabTitleList(tabTitleList: MutableList<String>) {
        tabTitleList.add("玩过游戏")
        tabTitleList.add("热门游戏")
    }

    override fun onFragmentFirstVisible() {
        super.onFragmentFirstVisible()
        playedGameViewModel.addAllPlayedGamesLiveData.observeNonNull(viewLifecycleOwner) { allPlayedGamesList ->
            if (allPlayedGamesList.isNotEmpty()) {
                val chooseGameList = chooseGamesViewModel.chooseGamesLiveData.value ?: arrayListOf()
                if (chooseGameList.isNotEmpty()) {
                    val chooseGameIdSet = chooseGameList.map { it.id }.toHashSet()
                    for (game in allPlayedGamesList) {
                        if (chooseGameList.size >= AddUserPlayedGameViewModel.ADD_GAMES_MAX_SIZE) {
                            Utils.toast(requireContext(), "已添加游戏到达上限")
                            break
                        } else if (!chooseGameIdSet.contains(game.id)) {
                            chooseGameList.add(game)
                        }
                    }
                    Utils.toast(requireContext(), "游戏已添加")
                } else {
                    chooseGameList.addAll(allPlayedGamesList)
                    Utils.toast(requireContext(), "游戏已添加")
                }
                chooseGamesViewModel.chooseGamesLiveData.postValue(chooseGameList)
                addUserPlayedGameFragment.notifyItemRangeChanged()
                playedGameViewModel.addAllPlayedGamesLiveData.postValue(null)
            }
        }
        binding.addAllTv.setOnClickListener {
            val chooseGameList = chooseGamesViewModel.chooseGamesLiveData.value ?: arrayListOf()
            if (chooseGameList.size >= AddUserPlayedGameViewModel.ADD_GAMES_MAX_SIZE) {
                Utils.toast(requireContext(), "已添加游戏到达上限")
            } else {
                playedGameViewModel.addAllPlayedGames()
            }
        }
    }

    fun setCurrentItem(position: Int) {
        binding.fragmentViewPager.currentItem = position
    }

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        binding.addAllTv.isVisible = position == PLAYED_GAMES_TAB_INDEX
    }

    companion object {
        const val PLAYED_GAMES_TAB_INDEX = 0
        const val HOT_GAMES_TAB_INDEX = 1
    }
}