package com.sketch.art.trace.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.sketch.art.trace.adapter.AdapterLike
import com.sketch.art.trace.databinding.ActivityLikeLayoutBinding
import com.sketch.art.trace.listener.OnLikeUpdateListener
import com.sketch.art.trace.mydata.BeanLocal
import com.sketch.art.trace.mydata.MyDataBase
import com.sketch.art.trace.tools.ComManager
import com.sketch.art.trace.tools.ItemDec

class AddLikeActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLikeLayoutBinding




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLikeLayoutBinding.inflate(layoutInflater)
        ComManager.setStatusBarTextColor(this, true)
        setContentView(binding.root)
        onInit()
        initClick()


    }

    private fun initClick() {
        binding.back.setOnClickListener {
            finish()
        }

    }

    private fun onInit() {
        val datumLocals = MyDataBase.queryAllLike()
        val listFavorite =
            AdapterLike(this)
        listFavorite.setData(datumLocals)
        updateUi(datumLocals)
        val itemDec = ItemDec(10, 10, 5)
        binding.recyclerview.addItemDecoration(itemDec)
        binding.recyclerview.adapter = listFavorite
        binding.recyclerview.layoutManager = GridLayoutManager(this@AddLikeActivity, 3)
        MyDataBase.addListener(object :
            OnLikeUpdateListener {
            override fun onAddLike() {
                val datumLocals = MyDataBase.queryAllLike()
                listFavorite.setData(datumLocals)
                updateUi(datumLocals)
            }

            override fun onDeleteLike() {
                val datumLocals = MyDataBase.queryAllLike()
                listFavorite.setData(datumLocals)
                updateUi(datumLocals)
            }
        })
    }

    private fun updateUi(datumLocals: List<BeanLocal>) {
        if (datumLocals.isNotEmpty()) {
            binding.recyclerview.visibility = View.VISIBLE
            binding.layoutNoFavorite.visibility = View.GONE
        } else {
            binding.recyclerview.visibility = View.GONE
            binding.layoutNoFavorite.visibility = View.VISIBLE
        }
    }




}