package com.sketch.ar.artdrawing.like

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ad.toponlibrary.AdManager
import com.sketch.ar.artdrawing.MyApp
import com.sketch.ar.artdrawing.utils.ObjectBoxHelper
import com.sketch.ar.artdrawing.utils.ItemDec
import com.sketch.ar.artdrawing.dataBean.DataLocal
import com.sketch.ar.artdrawing.listener.AddLikeListener
import com.sketch.ar.artdrawing.info.InfoActivity
import com.sketch.ar.artdrawing.R

class LikeActivity : AppCompatActivity() {

    private lateinit var recyclerView:RecyclerView

    private lateinit var fmBack:FrameLayout

    private lateinit var emptyLayout: LinearLayout
    private lateinit var loadPb:FrameLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_like)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        AdManager.loadAllAd()
        AdManager.showTopOn(this) {  }
        recyclerView = findViewById(R.id.collection_recyclerview)
        fmBack = findViewById(R.id.frame_back)
        emptyLayout = findViewById(R.id.layout_empty)
        loadPb= findViewById(R.id.loading)
        showEmpty(true)
        initData()
        setClick()
    }

    private fun setClick(){
        fmBack.setOnClickListener {
            finish()
        }
    }

    private fun showEmpty(boolean: Boolean){
        if(boolean){
            loadPb.visibility = View.VISIBLE
            recyclerView.visibility = View.GONE
        }else{
            loadPb.visibility = View.GONE
            recyclerView.visibility = View.VISIBLE
        }
    }
    private fun initData(){
        val datumLocals = ObjectBoxHelper.queryAllLike()
        val likeAdapter = LikeAdapter(this@LikeActivity){
            Log.d(MyApp.Companion.TAG,"------------it=$it")
            startActivity(Intent(this@LikeActivity, InfoActivity::class.java).apply {
                putExtra(InfoActivity.Companion.IntentKeyPath, it)
            })
        }
        likeAdapter.updateData(datumLocals)
        updateUi(datumLocals)
        showEmpty(false)
        recyclerView.run {

            addItemDecoration(ItemDec(5, 5, 5))
            layoutManager = GridLayoutManager(this@LikeActivity, 2)
            adapter = likeAdapter
        }

        ObjectBoxHelper.addListener(object : AddLikeListener {
            override fun AddFavorite() {
                val datumLocals = ObjectBoxHelper.queryAllLike()
                likeAdapter.updateData(datumLocals)
                updateUi(datumLocals)
            }

            override fun RemoveFavorite() {
                val datumLocals = ObjectBoxHelper.queryAllLike()
                likeAdapter.updateData(datumLocals)
                updateUi(datumLocals)
            }
        })
    }

    private fun updateUi(datumLocals: List<DataLocal>) {
        if (datumLocals.isNotEmpty()) {
            recyclerView.visibility = View.VISIBLE
            emptyLayout.visibility = View.GONE
        } else {
            recyclerView.visibility = View.GONE
            emptyLayout.visibility = View.VISIBLE
        }
    }
}