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

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.databinding.FragmentArticleDetailRelatedContentListBinding

class ArticleDetailRelatedContentListFragment : ToolbarFragment() {

    private lateinit var binding: FragmentArticleDetailRelatedContentListBinding

    private var itemDecoration: VerticalItemDecoration? = null

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val entrance = arguments?.getString(EntranceConsts.KEY_ENTRANCE) ?: ""
        val adapter = ArticleDetailRelatedContentListAdapter(requireContext(), entrance)
        binding.rv.adapter = adapter
        binding.rv.layoutManager = LinearLayoutManager(context)
        VerticalItemDecoration(context, 24f, false, com.gh.gamecenter.common.R.color.ui_surface)
        itemDecoration = VerticalItemDecoration(context, 24f, false, com.gh.gamecenter.common.R.color.ui_surface).also {
            binding.rv.addItemDecoration(it)
        }
        parentViewModel.relatedContentListLiveData.observe(viewLifecycleOwner) {
            adapter.submitList(it)
        }
    }

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout(): View {
        return FragmentArticleDetailRelatedContentListBinding.inflate(layoutInflater).also { binding = it }.root
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        itemDecoration?.let {
            binding.rv.removeItemDecoration(it)
            itemDecoration = VerticalItemDecoration(context, 24f, false, com.gh.gamecenter.common.R.color.ui_surface).also { decoration ->
                binding.rv.addItemDecoration(decoration)
            }
        }
    }
}