package com.gh.gamecenter.video.label

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.databinding.FragmentListBaseBinding
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.divider.HorizontalDividerItemDecoration
import com.gh.gamecenter.entity.ActivityLabelEntity
import com.gh.gamecenter.qa.dialog.ChooseActivityDialogFragment

class VideoLabelFragment : ToolbarFragment() {
    private lateinit var mBinding: FragmentListBaseBinding
    private var mAdapter: VideoLabelAdapter? = null
    private lateinit var mViewModel: VideoLabelViewModel

    override fun getLayoutId(): Int = R.layout.fragment_list_base

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding = FragmentListBaseBinding.bind(mCachedView)
        mBinding.listRefresh.isEnabled = false

        val selectTagActivityId = arguments?.getString(VideoLabelActivity.TAG_ACTIVITY_ID) ?: ""
        val location = arguments?.getString(EntranceConsts.KEY_LOCATION) ?: ""
        val bbsId = arguments?.getString(EntranceConsts.KEY_BBS_ID) ?: ""
        mViewModel = viewModelProvider(VideoLabelViewModel.Factory(location, bbsId))
        mBinding.listRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
            mAdapter = VideoLabelAdapter(requireContext(), selectTagActivityId) {
                if (requireParentFragment() is ChooseActivityDialogFragment) {
                    (requireParentFragment() as ChooseActivityDialogFragment).chooseActivityLabelCallback(it)
                }
            }
            val decoration = HorizontalDividerItemDecoration.Builder(requireContext())
                .size(1f.dip2px())
                .margin(20f.dip2px())
                .color(ContextCompat.getColor(requireContext(), R.color.background)).build()
            addItemDecoration(decoration)
            adapter = mAdapter
        }
        mViewModel.activityTags.observe(viewLifecycleOwner, Observer {
            mBinding.listRefresh.isRefreshing = false
            mBinding.reuseLlLoading.root.visibility = View.GONE
            if (it != null) {
                mBinding.reuseNoConnection.root.visibility = View.GONE
                if (it.isNotEmpty()) {
                    mBinding.listRv.visibility = View.VISIBLE
                    mBinding.reuseNoneData.root.visibility = View.GONE
                    mAdapter?.setListData(it)
                } else {
                    mBinding.reuseNoneData.root.visibility = View.VISIBLE
                }
            } else {
                mBinding.reuseNoneData.root.visibility = View.GONE
                mBinding.reuseNoConnection.root.visibility = View.VISIBLE
            }
        })
    }

    fun getSelectedActivity(): ActivityLabelEntity? {
        return mAdapter?.getSelectedActivity()
    }
}