package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.SearchActivity
import com.gh.gamecenter.SearchType
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.eventbus.EBUserFollow
import com.gh.gamecenter.search.viewmodel.SearchTabViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class UserSearchListFragment : LazyListFragment<FollowersOrFansEntity, UserSearchListViewModel>() {

    private val parentTabViewModel by viewModels<SearchTabViewModel>(
        ownerProducer = { parentFragment ?: this }
    )

    private var mSearchKey = ""
    private var mAdapter: UserSearchListAdapter? = null
    private var mSearchType = SearchType.DEFAULT.value
    private var mIsAutoLoad = true

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter
            ?: UserSearchListAdapter(requireContext(), mEntrance, mListViewModel) { userId, name, position ->
                SensorsBridge.trackUserSearchResultClick(
                    GlobalActivityManager.getCurrentPageEntity().pageId,
                    GlobalActivityManager.getCurrentPageEntity().pageName,
                    mListViewModel.sourceEntrance,
                    mSearchKey,
                    SearchActivity.toTrackSearchType(mSearchType),
                    userId,
                    position
                )
                NewFlatLogUtils.logSearchUserClick(
                    SearchType.fromString(mSearchType).toChinese(),
                    mSearchKey,
                    userId,
                    name,
                    position + 1,
                    mListViewModel.sourceEntrance
                )
            }.apply { mAdapter = this }
    }

    override fun provideListViewModel(): UserSearchListViewModel {
        val sourceEntrance = activity?.intent?.getStringExtra(EntranceConsts.KEY_SOURCE_ENTRANCE) ?: ""
        val viewModel = viewModelProvider<UserSearchListViewModel>().apply {
            this.sourceEntrance = sourceEntrance
        }
        return viewModel
    }

    override fun isAutomaticLoad(): Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        mIsAutoLoad = arguments?.getBoolean(EntranceConsts.KEY_IS_AUTO_LOAD, true) ?: true
        super.onCreate(savedInstanceState)
    }

    fun setSearchKeyAndType(searchKey: String, searchType: String) {
        mSearchKey = searchKey
        mSearchType = searchType
        mListViewModel?.updateSearchKeyAndType(mSearchKey, mSearchType)
    }

    override fun onFragmentFirstVisible() {
        super.onFragmentFirstVisible()
        mListRv?.overScrollMode = View.OVER_SCROLL_NEVER
        mListRv?.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface))
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface))
    }

    override fun initRealView() {
        super.initRealView()
        if (mIsAutoLoad) {
            mListViewModel?.updateSearchKeyAndType(mSearchKey, mSearchType)
        }
        parentTabViewModel.searchKeyAndType.observe(viewLifecycleOwner) { (key, type) ->
            setSearchKeyAndType(key, type)
        }

    }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(follow: EBUserFollow?) {
        val index = mAdapter?.entityList?.indexOfFirst { it.id == follow?.userId } ?: 0
        mAdapter?.entityList?.get(index)?.me?.isFollower = follow?.isFollow ?: false
        mAdapter?.notifyItemChanged(index)
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mListRv?.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface))
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface))
    }
}