package com.gh.gamecenter.qa.comment.base

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.view.SegmentedFilterView
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.common.view.vertical_recycler.SnappingLinearLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.gamecollection.detail.conversation.GameCollectionCommentConversationFragment
import com.gh.gamecenter.qa.comment.conversation.CommentConversationFragment

abstract class BaseCommentFragment<T, VM : BaseCommentViewModel> : ListFragment<T, VM>() {

    protected lateinit var fixedTopFilterView: View

    protected lateinit var commentHintTv: TextView
    protected lateinit var commentHintCountTv: TextView
    protected lateinit var skeletonView: View
    protected lateinit var orderSfv: SegmentedFilterView

    protected var mItemDecoration: RecyclerView.ItemDecoration? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        findView()
        mListRv.layoutManager = SnappingLinearLayoutManager(requireContext()).apply { mLayoutManager = this }
        mListRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (this@BaseCommentFragment is CommentConversationFragment
                    || this@BaseCommentFragment is GameCollectionCommentConversationFragment
                ) return
                val firstCompletelyVisiblePosition = mLayoutManager.findFirstCompletelyVisibleItemPosition()
                if (RecyclerView.NO_POSITION == firstCompletelyVisiblePosition) return

                val filterView = mLayoutManager.findViewByPosition(1)

                if (firstCompletelyVisiblePosition >= 2 && filterView == null) {
                    fixedTopFilterView.visibility = View.VISIBLE
                    updateFilterView()
                } else {
                    filterView?.let {
                        if (it.top <= 0 && fixedTopFilterView.visibility == View.GONE) {
                            fixedTopFilterView.visibility = View.VISIBLE
                            updateFilterView()
                        } else if (it.top > 0 && fixedTopFilterView.visibility == View.VISIBLE) {
                            fixedTopFilterView.visibility = View.GONE
                        }
                    }
                }
            }
        })

        orderSfv.setItemList(listOf("正序", "倒序"), 0)
        orderSfv.setOnCheckedCallback(object : SegmentedFilterView.OnCheckedCallback {
            override fun onItemCheck(position: Int) {
                getFilterVH()?.binding?.orderSfv?.performClick(position)
            }
        })
    }

    private fun findView() {
        fixedTopFilterView = mCachedView.findViewById(R.id.fixedTopFilterView)
        orderSfv = mCachedView.findViewById(R.id.orderSfv)
        commentHintTv = mCachedView.findViewById(R.id.commentHintTv)
        commentHintCountTv = mCachedView.findViewById(R.id.commentHintCountTv)
        skeletonView = mCachedView.findViewById(R.id.skeleton)
    }

    override fun onLoadRefresh() {
        showSkeleton(true)
    }

    override fun onLoadEmpty() {
        showSkeleton(false)
    }

    override fun onLoadDone() {
        showSkeleton(false)
        mReuseNoConn?.visibility = View.GONE
        mReuseNoData?.visibility = View.GONE
        mListLoading?.visibility = View.GONE

        mListRv.visibility = View.VISIBLE
        hideRefreshingLayout()

        mListRv.postDelayed({
            if (provideListAdapter().itemCount < theNumberNeededToFillAScreen()) {
                autoLoadMore()
            }
        }, autoLoadMoreDelay.toLong())
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {

        val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.divider_article_detail_comment)
        val itemDecoration = when {
            this !is CommentConversationFragment || mListViewModel.topCommentId.isNotBlank() -> {
                CustomDividerItemDecoration(
                    requireContext(),
                    notDecorateTheFirstTwoItems = true,
                    notDecorateTheLastItem = true
                )
            }
            else -> {
                CustomDividerItemDecoration(
                    requireContext(),
                    notDecorateTheFirstItem = true,
                    notDecorateTheLastItem = true
                )
            }
        }

        itemDecoration.setDrawable(drawable!!)
        mItemDecoration = itemDecoration
        return itemDecoration
    }

    fun updateFilterView() {
        getFilterVH()?.binding?.commentHintCountTv?.text = "${provideListViewModel().commentCount}"
        if (getFilterVH()?.binding?.orderSfv?.getCurrentPosition() != orderSfv.getCurrentPosition()) {
            orderSfv.setChecked(getFilterVH()?.binding?.orderSfv?.getCurrentPosition() ?: 0)
        }
        commentHintTv.text = getFilterVH()?.binding?.commentHintTv?.text
        commentHintCountTv.text = getFilterVH()?.binding?.commentHintCountTv?.text
    }

    private fun getFilterVH(): BaseCommentAdapter.CommentFilterViewHolder? {
        return (provideListAdapter() as BaseCommentAdapter).filterVH
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (mListRv != null) {
            mListRv.removeItemDecoration(mItemDecoration)
            mListRv.addItemDecoration(itemDecoration)
        }
    }

}