package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.eventbus.EBUserFollow
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

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

    private var mSearchKey = ""
    private var mAdapter: UserSearchListAdapter? = null

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter
            ?: UserSearchListAdapter(requireContext(), mEntrance, mListViewModel).apply { mAdapter = this }
    }

    override fun isAutomaticLoad(): Boolean = false

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mListRv.overScrollMode = View.OVER_SCROLL_NEVER
        mListRv.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mListViewModel?.updateSearchKey(mSearchKey)
    }

    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(), R.color.background_white))
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
    }
}