package com.gh.gamecenter.qa.article.detail

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.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.qa.comment.base.BaseCommentAdapter
import com.halo.assistant.HaloApp
import splitties.views.backgroundColor

class ArticleDetailCommentListFragment : ListFragment<CommentItemData, ArticleDetailCommentListViewModel>() {

    private var adapter: ArticleDetailCommentListAdapter? = null

    private val parentViewModel: ArticleDetailViewModel by viewModels(
        ownerProducer = { requireParentFragment() }
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mEntrance = arguments?.getString(EntranceConsts.KEY_ENTRANCE) ?: ""
        mListViewModel.total.observe(this) {
            parentViewModel.updateTotal(it)
        }
        parentViewModel.sortType.observe(this) {
            mListViewModel.changeSort(it)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_list_article_detail_comment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mListRefresh?.isEnabled = false
    }

    override fun provideListAdapter(): ListAdapter<*> = adapter ?: ArticleDetailCommentListAdapter(
        requireContext(),
        mListViewModel,
        BaseCommentAdapter.AdapterType.COMMENT,
        mEntrance
    ).apply {
        adapter = this
    }

    override fun provideListViewModel(): ArticleDetailCommentListViewModel {
        return viewModelProvider<ArticleDetailCommentListViewModel>(
            ArticleDetailCommentListViewModel.Factory(
                HaloApp.getInstance().application,
                parentViewModel.articleId,
                parentViewModel.communityId,
                parentViewModel.topCommentId
            )
        ).apply {
            topItemData = parentViewModel.topItemData
        }
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {

        val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.divider_article_detail_comment)
        val itemDecoration = CustomDividerItemDecoration(
            requireContext(),
            notDecorateTheFirstItem = true,
            notDecorateTheLastItem = true
        )

        itemDecoration.setDrawable(drawable!!)
        mItemDecoration = itemDecoration
        return itemDecoration
    }
    override fun addSyncPageObserver() = true

    override fun provideSyncAdapter() = adapter


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