package com.themes.fancy.keyboard.keyboard.viewui

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 androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.themes.fancy.keyboard.keyboard.MyApplication
import com.themes.fancy.keyboard.keyboard.data.Class
import com.themes.fancy.keyboard.keyboard.data.KbSource
import com.themes.fancy.keyboard.keyboard.databinding.LayoutLikeFragmentBinding
import com.themes.fancy.keyboard.keyboard.listener.OnRemoveLikeCallback
import com.themes.fancy.keyboard.keyboard.myada.LikeAdapter
import com.themes.fancy.keyboard.keyboard.myada.TabAdapter
import com.themes.fancy.keyboard.keyboard.myroom.DbManager
import com.themes.fancy.keyboard.keyboard.myroom.MyDatabase
import kotlinx.coroutines.launch

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

    private lateinit var likeRecycler: RecyclerView
    private lateinit var viewContent: ViewPager
    lateinit var viewAllList: MutableList<Class>
    private lateinit var tabAdapter: TabAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        arguments?.let {
//            paramIndex = it.getInt(ARG_PARAM1)
//        }
    }

    companion object {

        @JvmStatic
        fun newInstance(index: Int) =
            FragmentLike().apply {
//                arguments = Bundle().apply {
//                    putInt(ARG_PARAM1, index)
//                }
            }
    }

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

        init()

        return vb.root
    }


    private fun init() {
        val mainAdapter = LikeAdapter(requireContext()).apply {
            setRemoveLike(object :OnRemoveLikeCallback{
                override fun OnRemoveLike(data: KbSource) {
                     lifecycleScope.launch {
                         DbManager.removeLike(data)
                     }
                }

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

        MyDatabase.myDatabase.ThemesDao().queryAllLike().observe(requireActivity()) {
            Log.d(MyApplication.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)
            }

        }
    }


}