package com.app.keyboard.colorful.afragment

import android.os.Bundle
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.keyboard.colorful.ada.AdaLike
import com.app.keyboard.colorful.colorlistener.COnUpdateLikeListener
import com.app.keyboard.colorful.data.ChildBean
import com.app.keyboard.colorful.databinding.FragLikeBinding
import com.app.keyboard.colorful.dbbase.BaseDataBase
import com.app.keyboard.colorful.dbbase.DbHelper
import kotlinx.coroutines.launch

class FragmentLike : Fragment() {
    private lateinit var vb: FragLikeBinding

    private lateinit var adaLike: AdaLike


    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentLike()
    }

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

        init()

        return vb.root
    }


    private fun init() {
        adaLike = AdaLike(
            requireContext()
        ).apply {
            setRemoveLike(object : COnUpdateLikeListener {
                override fun onRemoveLike(data: ChildBean) {
                     lifecycleScope.launch {
                         DbHelper.removeLike(data)
                     }
                }

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

        BaseDataBase.baseDataBase.ThemesDao().queryAllLike().observe(requireActivity()) {
            if(it.isNullOrEmpty()){
                vb.likeRecycler.isVisible = false
                vb.emptyTitle.isVisible = true
            }else{
                vb.likeRecycler.isVisible = true
                vb.emptyTitle.isVisible = false
                adaLike.setForYouList(it)
            }

        }
    }



}