package com.gh.gamecenter.video.data

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseFragment
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.FragmentVideoDataBinding
import com.gh.gamecenter.common.mvvm.Status

class VideoDataFragment : BaseFragment<Any>() {
    private lateinit var mViewModel: VideoDataViewModel
    private var mAdapter: VideoDataAdapter? = null
    private lateinit var mBinding: FragmentVideoDataBinding

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout(): View {
        return FragmentVideoDataBinding.inflate(LayoutInflater.from(requireContext()), null, false).apply {
            mBinding = this
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.reuseToolbar.backBtn.setImageDrawable(R.drawable.ic_bar_back_light.toDrawable())
        mBinding.reuseToolbar.normalTitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
        mBinding.reuseToolbar.normalToolbar.setBackgroundColor(
            ContextCompat.getColor(
                requireContext(),
                R.color.transparent
            )
        )
        mBinding.reuseToolbar.normalTitle.text = "视频数据"

        mBinding.refreshView.setProgressViewOffset(
            false,
            0,
            80F.dip2px() + DisplayUtils.getStatusBarHeight(requireContext().resources)
        )
        mBinding.refreshView.setOnRefreshListener {
            mViewModel.loadData()
        }

        mViewModel = viewModelProvider()
        mAdapter = VideoDataAdapter(requireContext())
        mBinding.listRv.apply {
            layoutManager = FixLinearLayoutManager(requireContext())
            addItemDecoration(getItemDecoration())
            adapter = mAdapter
        }
        mViewModel.loadData()

        mViewModel.videoDataLiveData.observe(viewLifecycleOwner, Observer {
            mBinding.refreshView.isRefreshing = false
            mBinding.reuseLoading.root.visibility = View.GONE
            if (it.status == Status.SUCCESS) {
                mBinding.listRv.visibility = View.VISIBLE
                mBinding.reuseNoConnection.root.visibility = View.GONE
                mAdapter?.setListData(it.data)
                mAdapter?.notifyDataSetChanged()
            } else {
                loadErrorControl()
            }
        })
    }

    private fun getItemDecoration(): RecyclerView.ItemDecoration {
        return VerticalItemDecoration(requireContext(), 40F, false, R.color.background_white)
    }

    private fun loadErrorControl() {
        mBinding.listRv.visibility = View.GONE
        mBinding.reuseNoConnection.root.visibility = View.VISIBLE
        mBinding.reuseNoConnection.root.setOnClickListener {
            mBinding.reuseNoConnection.root.visibility = View.GONE
            mBinding.reuseLoading.root.visibility = View.VISIBLE
            mViewModel.loadData()
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (mBinding.listRv.itemDecorationCount > 0) {
            mBinding.listRv.removeItemDecorationAt(0)
            mBinding.listRv.addItemDecoration(getItemDecoration())
        }
    }
}