package com.prank.app.color.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.prank.app.color.dbdata.DbTool
import com.prank.app.color.databinding.ActivityLikeBinding
import com.prank.app.color.list.AdapterLike
import com.prank.app.color.utils.Common
import com.prank.app.color.utils.ItemDecoration

class ActivityLike : AppCompatActivity(), View.OnClickListener {
    private lateinit var binding: ActivityLikeBinding

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


        binding.list.run {
            layoutManager = GridLayoutManager(this@ActivityLike, 2)
            val all = DbTool.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 = AdapterLike(
                this@ActivityLike,
                all,
                false
            )
            val itemHelper =
                ItemDecoration(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 = DbTool.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)
        }
    }
}