package com.prank.sound.funnyapp.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.prank.sound.funnyapp.databinding.ActivityCollectionBinding
import com.prank.sound.funnyapp.myadapter.AdapterFavorite
import com.prank.sound.funnyapp.bean.DbManager
import com.prank.sound.funnyapp.mytool.Decoration
import com.prank.sound.funnyapp.mytool.Common

class CollectionActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var binding: ActivityCollectionBinding

    private var likeAdapter: AdapterFavorite? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCollectionBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Common.setStatusBarTextColor(this, true)
        binding.back.setOnClickListener(this)


        binding.list.run {
            layoutManager = GridLayoutManager(this@CollectionActivity, 2)
            val all = DbManager.getAllLike()
            if (all.size == 0) {
                binding.layoutEmpty.visibility = View.VISIBLE
                binding.list.visibility = View.GONE
            } else {
                binding.layoutEmpty.visibility = View.GONE
                binding.list.visibility = View.VISIBLE
            }
            likeAdapter = AdapterFavorite(
                this@CollectionActivity,
                all,
                false
            )
            val itemHelper =
                Decoration(10, 10, 5)
            addItemDecoration(itemHelper)
            adapter = likeAdapter
        }

    }

    override fun onClick(v: View?) {
        if (v == binding.back) {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        refresh()
    }

    fun refresh() {
        if (likeAdapter != null) {
            val all = DbManager.getAllLike()
            if (all.size == 0) {
                binding.layoutEmpty.visibility = View.VISIBLE
                binding.list.visibility = View.GONE
            } else {
                binding.layoutEmpty.visibility = View.GONE
                binding.list.visibility = View.VISIBLE
            }
            likeAdapter!!.updateData(all)
        }
    }
}