package com.gh.gamecenter.search

import android.os.Bundle
import androidx.activity.viewModels
import com.gh.gamecenter.DisplayType
import com.gh.gamecenter.SearchActivity
import com.gh.gamecenter.search.fragment.SearchTabFragment
import com.gh.gamecenter.search.viewmodel.SearchTabActivityViewModel

class SearchTabActivity : SearchActivity() {

    private val viewModel by viewModels<SearchTabActivityViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.loadTabs()
    }

    override fun updateDisplayType(type: DisplayType) {
        viewModel.updateSearchKeyAndType(mSearchKey, mSearchType.value)
        mDisplayType = type
        // 如果上一次展示的是SearchTabFragment，并且当前需要展示的也是SearchTabFragment，则无需重复显示
        val topFragment = supportFragmentManager.fragments.lastOrNull()
        if (topFragment is SearchTabFragment && type != DisplayType.DEFAULT) {
            return
        }
        if (type == DisplayType.DEFAULT) {
            super.updateDisplayType(type)
        } else {
            showFragment(SearchTabFragment::class.java.name, {
                SearchTabFragment.newInstance(mSourceEntrance)
            })
                .commitAllowingStateLoss()
        }
        mDisplayType = type
    }
}