package com.trace.drawing.people.page

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.trace.drawing.people.bean.BeanLocal
import com.trace.drawing.people.bean.MyDataBase
import com.trace.drawing.people.callbacl.OnLikeCallback
import com.trace.drawing.people.databinding.PageCollectionBinding
import com.trace.drawing.people.item.ItemCollectionAdapter
import com.trace.drawing.people.max.MaxManager
import com.trace.drawing.people.tools.Common
import com.trace.drawing.people.tools.ItemSpace

class CollectionPage : AppCompatActivity() {
    private lateinit var binding: PageCollectionBinding


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

        MaxManager.startShowMaxAd(this@CollectionPage) {

        }
    }

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

            MaxManager.startShowMaxAd(this@CollectionPage) {
                finish()
            }
        }

    }

    private fun onInit() {
        val datumLocals = MyDataBase.queryAllLike()
        val listFavorite =
            ItemCollectionAdapter(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@CollectionPage, 3)
        MyDataBase.addListener(object :
            OnLikeCallback {
            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
        }
    }


}