package com.gh.gamecenter.subject

import android.os.Bundle
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.pagelevel.IPageLevelProvider
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.feature.entity.PageLocation

class AdSubjectListFragment : SubjectListFragment() {
    private var mSubjectData: SubjectData? = null
    override fun provideListViewModel(): SubjectListViewModel {
        val bottomTabName = arguments?.getString(EntranceConsts.KEY_BOTTOM_TAB_NAME, "") ?: ""
        val bottomTabIndex = arguments?.getInt(EntranceConsts.KEY_BOTTOM_TAB_INDEX, -1) ?: -1
        val tabIndex = arguments?.getInt(EntranceConsts.KEY_TAB_INDEX, -1) ?: -1
        val tabName = arguments?.getString(EntranceConsts.KEY_TAB_NAME, "") ?: ""
        val multiTabNavId = arguments?.getString(EntranceConsts.KEY_MULTI_TAB_NAV_ID, "") ?: ""
        val multiTabNavName = arguments?.getString(EntranceConsts.KEY_MULTI_TAB_NAV_NAME, "") ?: ""

        val precisePageLevelString = (activity as? IPageLevelProvider)
            ?.getPageLevel()
            ?.getTempNewPageLevel(topTabPosition = tabIndex, bottomTabPosition = bottomTabIndex)
            ?.toFormattedString(ignoreBottomTabPositionMap = true)
            ?: ""
        return viewModelProvider(
            AdSubjectListViewModel.Factory(
                mSubjectData!!,
                PageLocation(
                    bottomTab = bottomTabName,
                    bottomTabIndex = bottomTabIndex,
                    pageLevelString = precisePageLevelString,
                    severalTabPageId = multiTabNavId,
                    severalTabPageName = multiTabNavName,
                    tabPosition = tabIndex,
                    tabContent = tabName
                ),
                arguments?.getParcelableArrayList(EntranceConsts.KEY_EXPOSURE_SOURCE_LIST)
            )
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        mSubjectData = arguments?.getParcelable(EntranceConsts.KEY_SUBJECT_DATA)
        super.onCreate(savedInstanceState)
    }

    override fun onFragmentFirstVisible() {
        initMenu(R.menu.menu_download)
        super.onFragmentFirstVisible()
        setNavigationTitle(mSubjectData?.subjectName)
        showAdLabel(mSubjectData?.isAdData ?: false)
    }
}