package com.gh.gamecenter.forum.list

import android.os.Bundle
import com.gh.common.util.*
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.common.baselist.ListFragment
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.entity.ForumEntity

class ForumListFragment : ListFragment<ForumEntity, ForumListViewModel>() {

    private var mAdapter: ForumListAdapter? = null
    private var mViewModel: ForumListViewModel? = null


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

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

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

    override fun onCreate(savedInstanceState: Bundle?) {
        val type = arguments?.getString(EntranceConsts.KEY_TYPE) ?: ""
        mViewModel = provideListViewModel()
        mViewModel?.type = type
        when (type) {
            ForumListActivity.TYPE_FOLLOW -> {
                setNavigationTitle("关注论坛")
                mViewModel?.setOverLimitSize(1000)
            }
            ForumListActivity.TYPE_HOT -> setNavigationTitle("热门论坛")
            ForumListActivity.TYPE_OFFICIAL -> setNavigationTitle("综合论坛")
        }

        super.onCreate(savedInstanceState)
    }

    override fun onBackPressed(): Boolean {
        var event = ""
        var location = ""
        when (mViewModel?.type) {
            ForumListActivity.TYPE_FOLLOW -> {
                event = "click_followforum_return"
                location = "关注论坛页"
            }

            ForumListActivity.TYPE_HOT -> {
                event = "click_hotforum_return"
                location = "热门论坛页"
            }

            ForumListActivity.TYPE_OFFICIAL -> {
                event = "click_multipleforum_return"
                location = "综合论坛页"
            }
        }
        NewLogUtils.logForumPageEnterOrReturnClick(event, location)
        return super.onBackPressed()
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mAdapter?.let { it.notifyItemRangeChanged(0, it.itemCount) }
    }
}