package com.app.brush.guitar.ink.icicle

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.app.brush.guitar.ink.App
import com.app.brush.guitar.ink.drama.DichotomyDetails
import com.app.brush.guitar.ink.databinding.PureFragmentKeyboardFavoriteBinding
import com.app.brush.guitar.ink.ballad.Brilliant
import com.app.brush.guitar.ink.ballad.Gratitude
import com.app.brush.guitar.ink.canvas.JubilantDeleteFavorite
import com.app.brush.guitar.ink.gallery.FavoriteElucidate
import kotlinx.coroutines.launch

class PragmaticFavoriteKeyboard : Fragment() {
    private lateinit var vb: PureFragmentKeyboardFavoriteBinding
    companion object {

        @JvmStatic
        fun newInstance() =
            PragmaticFavoriteKeyboard()
    }

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

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

    private fun init() {
        val mainAdapter = FavoriteElucidate(
            requireContext()
        ).apply {
            setRemoveLike(object : JubilantDeleteFavorite {
                override fun OnRemoveLike(data: DichotomyDetails) {
                     lifecycleScope.launch {
                         Gratitude.removeLike(data)
                     }
                }

            })
        }
        vb.likeRecycler.run {
            adapter = mainAdapter
            layoutManager = GridLayoutManager(requireContext(), 2)
        }

        Brilliant.Companion.baseDataBase.ThemesDao().queryAllLike().observe(viewLifecycleOwner) {
            Log.d(App.Companion.TAG, "---------it=${it?.size}")
            if(it.isNullOrEmpty()){
                vb.likeRecycler.isVisible = false
                vb.emptyTitle.isVisible = true
            }else{
                vb.likeRecycler.isVisible = true
                vb.emptyTitle.isVisible = false
                mainAdapter.setForYouList(it)
            }

        }
    }


}