package com.keyboard.journey

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.gyf.immersionbar.ktx.immersionBar
import com.keyboard.journey.adapter.CategoryDetailsDataAdapter
import com.keyboard.journey.bean.ItemDataBean
import com.keyboard.journey.databinding.FragmentLikeBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.libpag.PAGFile

class LikeFragment : Fragment() {
    private lateinit var binding: FragmentLikeBinding
    private var adapter: CategoryDetailsDataAdapter? = null
    private var contentBeans: MutableList<ItemDataBean> = mutableListOf()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentLikeBinding.inflate(layoutInflater)
        return binding.root
    }

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

    private fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarView(binding.view)
        }
    }

    override fun onResume() {
        super.onResume()
        initImmersionBar()
        initData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initImmersionBar()
        }
    }


    private fun initView() {
        adapter = CategoryDetailsDataAdapter(requireActivity(), contentBeans, "like")
        binding.rv.layoutManager = GridLayoutManager(requireActivity(), 2)
        binding.rv.adapter = adapter
    }

    private fun initData() {
        loadingPlay()
        binding.noDataLayout.visibility = View.GONE
        GlobalScope.launch {
            val beans = JourneyApp.databaseManager.getAllItemDataBeanFiles()
            if (beans.isNotEmpty()) {
                contentBeans.clear()
                contentBeans.addAll(beans)
                withContext(Dispatchers.Main) {
                    loadingClose()
                    binding.noDataLayout.visibility = View.GONE
                }
            } else {
                withContext(Dispatchers.Main) {
                    loadingClose()
                    binding.noDataLayout.visibility = View.VISIBLE
                }
            }
        }
    }

    private fun loadingPlay() {
        binding.loadingLayout.visibility = View.VISIBLE
        val pagFile = PAGFile.Load(requireActivity().assets, "loading.pag")
        binding.pagView.composition = pagFile
        binding.pagView.setRepeatCount(0)
        binding.pagView.play()
    }

    private fun loadingClose() {
        binding.loadingLayout.visibility = View.GONE
        if (binding.pagView.isPlaying) {
            binding.pagView.stop()
        }
    }
}