package com.gh.gamecenter.forum.search

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.divider.HorizontalDividerItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.view.SegmentedFilterView
import com.gh.gamecenter.databinding.FragmentForumContentSearchListBinding
import com.gh.gamecenter.feature.entity.AnswerEntity

class ForumContentSearchListFragment : ListFragment<AnswerEntity, ForumContentSearchListViewModel>() {

    private var bbsId = ""
    private var mSearchKey = ""
    private var mAdapter: ForumContentSearchListAdapter? = null
    private var mBinding: FragmentForumContentSearchListBinding? = null

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter
            ?: ForumContentSearchListAdapter(requireContext(), mListViewModel, "${mEntrance}+(搜索)").apply {
                mAdapter = this
            }
    }

    override fun provideListViewModel(): ForumContentSearchListViewModel {
        val factory = ForumContentSearchListViewModel.Factory(bbsId)
        return viewModelProvider(factory)
    }

    override fun getLayoutId() = 0

    override fun getInflatedLayout() =
        FragmentForumContentSearchListBinding.inflate(layoutInflater).apply { mBinding = this }.root

    override fun isAutomaticLoad(): Boolean = false

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

    override fun onRefresh() {
        if (isFilterViewShow()) {
            mBinding?.fixedTopFilterView?.filterView?.visibility = View.GONE
        }
        super.onRefresh()
    }

    override fun onLoadDone() {
        if (isFilterViewShow()) {
            mBinding?.fixedTopFilterView?.filterView?.visibility = View.VISIBLE
        }
        super.onLoadDone()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        bbsId = arguments?.getString(EntranceConsts.KEY_BBS_ID) ?: ""
        super.onCreate(savedInstanceState)
        mListRv.overScrollMode = View.OVER_SCROLL_NEVER
        mListRv.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
//        mReuseNoData?.findViewById<ImageView>(R.id.reuseNoneDataIv)?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.pic_no_data))
//        mReuseNoData?.findViewById<TextView>(R.id.reuse_tv_none_data)?.text = "搜索结果为空，换个搜索词试试"
        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white))
        mBinding?.run {
            fixedTopFilterView.run {
                orderSfv.setItemList(listOf("默认", "最新"), 0)
                commentHintTv.text = "搜索结果"
                divider.visibility = View.GONE
                commentHintCountTv.visibility = View.GONE
                orderSfv.setOnCheckedCallback(object : SegmentedFilterView.OnCheckedCallback {
                    override fun onItemCheck(position: Int) {
                        val sort = if (position == 0) "" else "latest"
                        mListViewModel?.updateSort(sort)
                    }
                })
            }

        }
    }

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

    override fun getItemDecoration(): RecyclerView.ItemDecoration? {
        return HorizontalDividerItemDecoration.Builder(requireContext())
            .size(DisplayUtils.dip2px(0.5f))
            .margin(DisplayUtils.dip2px(20f))
            .color(ContextCompat.getColor(requireContext(), R.color.background)).build()
    }

    private fun isFilterViewShow(): Boolean {
        return mEntrance == "论坛详情"
    }

    override fun addSyncPageObserver(): Boolean = true

    override fun provideSyncAdapter(): ForumContentSearchListAdapter = mAdapter!!

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