package com.drawing.drawsketch.collection

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 android.widget.ProgressBar
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.drawing.drawsketch.App
import com.drawing.drawsketch.DbBase
import com.drawing.drawsketch.ItemSpace
import com.drawing.drawsketch.R
import com.drawing.drawsketch.dbmodel.ResSvg
import com.drawing.drawsketch.listener.AddRemoveFavoriteListener
import com.drawing.drawsketch.preview.PreviewActivity

class CollectionActivity : 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_collection)
        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
        }


        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 = DbBase.queryAllLike()
        val collectionAdapter = CollectionAdapter(this@CollectionActivity){
            Log.d(App.TAG,"------------it=$it")
            startActivity(Intent(this@CollectionActivity, PreviewActivity::class.java).apply {
                putExtra(PreviewActivity.IntentKeyPath, it)
            })
        }
        collectionAdapter.updateData(datumLocals)
        updateUi(datumLocals)
        showEmpty(false)
        recyclerView.run {

            addItemDecoration(ItemSpace(5, 5, 5))
            layoutManager = GridLayoutManager(this@CollectionActivity, 2)
            adapter = collectionAdapter
        }

        DbBase.addListener(object : AddRemoveFavoriteListener {
            override fun AddFavorite() {
                val datumLocals = DbBase.queryAllLike()
                collectionAdapter.updateData(datumLocals)
                updateUi(datumLocals)
            }

            override fun RemoveFavorite() {
                val datumLocals = DbBase.queryAllLike()
                collectionAdapter.updateData(datumLocals)
                updateUi(datumLocals)
            }
        })
    }

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