package com.gh.gamecenter.qa.dialog

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.forum.detail.ForumDetailActivity

class ChooseForumContainerFragment : ListFragment<ForumEntity, ChooseForumContainerViewModel>() {

    private var mAdapter: ChooseForumContainerAdapter? = null
    private var type: String = ""
    private var mSearchKey: String = ""
    private var mIsFirstIn = true

    override fun onCreate(savedInstanceState: Bundle?) {
        type = arguments?.getString(EntranceConsts.KEY_CHOOSE_FORUM_TYPE) ?: ""
        super.onCreate(savedInstanceState)
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
    }

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter ?: ChooseForumContainerAdapter(requireContext(), type, mListViewModel) {
            if (requireActivity() is ChooseForumActivity) {
                (requireActivity() as ChooseForumActivity).chooseSuccess(it)
                val entrance =
                    if (type == ChooseForumType.ATTENTION.value) "我的关注tab" else if (type == ChooseForumType.HOT.value) "热门论坛tab" else "搜索结果"
                val bbsType = if (it.type == "game_bbs") "游戏论坛" else "综合论坛"
                NewLogUtils.logChooseForumPanelClick("click_select_forum_panel_forum", entrance, it.id, bbsType)
            } else {
                requireActivity().startActivity(ForumDetailActivity.getIntent(requireContext(), it.id, "搜索论坛"))
            }
        }.apply {
            mAdapter = this
        }
    }

    override fun provideListViewModel(): ChooseForumContainerViewModel {
        return viewModelProvider(ChooseForumContainerViewModel.Factory(type, mSearchKey))
    }

    fun setSearchKey(searchKey: String) {
        mSearchKey = searchKey
        mListViewModel?.setSearchKeyAndRefresh(searchKey)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (type == ChooseForumType.SEARCH.value) {
            mListViewModel?.setSearchKeyAndRefresh(mSearchKey)
        }
    }

    override fun onChanged(ts: MutableList<ForumEntity>?) {
        super.onChanged(ts)
        if (type == ChooseForumType.ATTENTION.value && mIsFirstIn && ts.isNullOrEmpty()) {
            (requireActivity() as ChooseForumActivity).switchTab(1)
            mIsFirstIn = false
        }
    }

    override fun shouldLoadMore(): Boolean = type != ChooseForumType.ATTENTION.value

    override fun isAutomaticLoad(): Boolean = type != ChooseForumType.SEARCH.value

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
    }

    companion object {
        fun getInstance(type: ChooseForumType): Fragment {
            val bundle = bundleOf(EntranceConsts.KEY_CHOOSE_FORUM_TYPE to type.value)
            return ChooseForumContainerFragment().apply {
                arguments = bundle
            }
        }
    }

    enum class ChooseForumType(val value: String) {
        ATTENTION("attention"),
        HOT("hot"),
        SEARCH("search")
    }
}
