package com.picture.ar.drawing.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.ad.toponlibrary.AdManager

import com.picture.ar.drawing.data.BeanLocal
import com.picture.ar.drawing.data.MyDataBase
import com.picture.ar.drawing.databinding.ActivityLikeBinding
import com.picture.ar.drawing.list.ItemCollectionList
import com.picture.ar.drawing.listener.LikeListener
import com.picture.ar.drawing.utils.ComUtils
import com.picture.ar.drawing.utils.ItemSpace

class LikeActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLikeBinding




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

        AdManager.showTopOn(this@LikeActivity){

        }

    }

    private fun initClick() {
        binding.back.setOnClickListener {
            AdManager.showTopOn(this@LikeActivity){
                finish()
            }


        }

    }

    private fun onInit() {
        val datumLocals = MyDataBase.queryAllLike()
        val listFavorite =
            ItemCollectionList(this)
        listFavorite.setData(datumLocals)
        updateUi(datumLocals)
        val itemSpace = ItemSpace(10, 10, 5)
        binding.recyclerview.addItemDecoration(itemSpace)
        binding.recyclerview.adapter = listFavorite
        binding.recyclerview.layoutManager = GridLayoutManager(this@LikeActivity, 3)
        MyDataBase.addListener(object :
            LikeListener {
            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
        }
    }




}