package com.keyboard.bubble.skin.keyboard.fagment

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.keyboard.bubble.skin.keyboard.App
import com.keyboard.bubble.skin.keyboard.bean.ChildBean
import com.keyboard.bubble.skin.keyboard.databinding.LayoutLikeFragmentBinding
import com.keyboard.bubble.skin.keyboard.db.DbOpManager
import com.keyboard.bubble.skin.keyboard.db.MyDb
import com.keyboard.bubble.skin.keyboard.myada.AdapterLike
import com.keyboard.bubble.skin.keyboard.mylistener.CancelLikeCallback
import kotlinx.coroutines.launch

class LikePage : Fragment() {
    private lateinit var vb: LayoutLikeFragmentBinding


    companion object {

        @JvmStatic
        fun newInstance() =
            LikePage()
    }

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

        init()

        return vb.root
    }


    private fun init() {
        val mainAdapter = AdapterLike(
            requireContext()
        ).apply {
            setRemoveLike(object : CancelLikeCallback {
                override fun OnRemoveLike(data: ChildBean) {
                     lifecycleScope.launch {
                         DbOpManager.removeLike(data)
                     }
                }

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

        MyDb.myDb.ThemesDao().queryAllLike(true).observe(requireActivity()) {
            Log.d(App.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)
            }

        }
    }


}