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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.view.GridSpacingItemDecoration
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.DialogTopCommunityCategoryBinding
import com.gh.gamecenter.qa.entity.TopCommunityCategory
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * 版主帖子置顶-置顶类型
 */
class TopCommunityCategoryDialog private constructor(): BottomSheetDialogFragment() {

    private val viewModel: TopCommunityCategoryViewModel by viewModels()

    private lateinit var viewBinding: DialogTopCommunityCategoryBinding

    private lateinit var topCommunityCategoryAdapter: TopCommunityCategoryAdapter

    private lateinit var callback: (TopCommunityCategory) -> Unit

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.getTopCommunityCategory()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = DialogTopCommunityCategoryBinding
        .inflate(
            inflater,
            container,
            false
        ).also {
            viewBinding = it
        }.root

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        with(viewBinding) {
            rv.adapter = TopCommunityCategoryAdapter{
                viewModel.submitTopCommunityCategory(it)
            }.also {
                topCommunityCategoryAdapter = it
            }
            rv.layoutManager = GridLayoutManager(context, 2)
            rv.addItemDecoration(GridSpacingItemDecoration(2, DisplayUtils.dip2px(8F), false))
        }

        viewModel.topCommunityCategoryList.observeNonNull(viewLifecycleOwner) {
            topCommunityCategoryAdapter.submitList(it)
        }

        viewModel.selectTopCommunityCategory.observe(viewLifecycleOwner) {
            if (it == null) {
                viewBinding.tvSubmit.isEnabled = false
                viewBinding.tvSubmit.alpha = 0.4F
            } else {
                viewBinding.tvSubmit.isEnabled = true
                viewBinding.tvSubmit.alpha = 1.0F
            }
        }

        viewBinding.ivClose.setOnClickListener {
            dismissAllowingStateLoss()
        }

        viewBinding.tvSubmit.setOnClickListener {
            callback.invoke(viewModel.selectTopCommunityCategory.value!!)
            dismissAllowingStateLoss()
        }
    }

    companion object {
        fun show(
            fragmentManager: FragmentManager,
            callback: (TopCommunityCategory) -> Unit
        ) = TopCommunityCategoryDialog().also {
            it.callback = callback
        }.show(fragmentManager, null)
    }
}