package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.SearchActivity.Companion.toTrackSearchType
import com.gh.gamecenter.SearchType
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.doOnPageSelected
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.qa.dialog.ChooseForumContainerFragment
import com.google.android.material.tabs.TabLayout

class ForumOrUserSearchFragment : BaseFragment_TabLayout() {
    private var mSearchKey = ""
    private var mSearchType = SearchType.DEFAULT.value

    override fun initFragmentList(fragments: MutableList<Fragment>) {
        fragments.add(ChooseForumContainerFragment.getInstance(ChooseForumContainerFragment.ChooseForumType.SEARCH))
        fragments.add(ForumContentSearchListFragment())
        fragments.add(UserSearchListFragment())
    }

    override fun initTabTitleList(tabTitleList: MutableList<String>) {
        tabTitleList.add("论坛")
        tabTitleList.add("内容")
        tabTitleList.add("用户")
    }

    fun setSearchKeyAndType(searchKey: String, searchType: String) {
        mSearchKey = searchKey
        mSearchType = searchType
        if (mFragmentsList != null) {
            setSearchKeyToChildFragment()
        }
    }

    private fun setSearchKeyToChildFragment() {
        mFragmentsList.forEach {
            if (it is ChooseForumContainerFragment) {
                it.setSearchKeyAndType(mSearchKey, mSearchType)
            }
            if (it is ForumContentSearchListFragment) {
                it.setSearchKeyAndType(mSearchKey, mSearchType)
            }
            if (it is UserSearchListFragment) {
                it.setSearchKeyAndType(mSearchKey, mSearchType)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mTabLayout.tabMode = TabLayout.MODE_FIXED

        val viewpagerParams = mViewPager.layoutParams as LinearLayout.LayoutParams
        viewpagerParams.topMargin = 0.5f.dip2px()
        mViewPager.layoutParams = viewpagerParams

        setSearchKeyToChildFragment()

        mViewPager.doOnPageSelected {
            val tabName = mTabTitleList[it]
            NewFlatLogUtils.logClickSearchTab(mSearchKey, tabName)

            if (it == 1) {
                SensorsBridge.trackArticleSearchTabClick(mSearchKey, toTrackSearchType(mSearchType))
            }

            if (it < mFragmentsList.size) {
                val fragment = mFragmentsList.safelyGetInRelease(it)
                if (fragment is ForumContentSearchListFragment) {
                    fragment.refreshScrollHelper()
                }
            }
        }
    }
}