package com.sketch.ar.artdrawing.allImageList

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
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.utils.ItemDec
import com.sketch.ar.artdrawing.utils.CommonUtils
import com.sketch.ar.artdrawing.info.InfoActivity
import com.sketch.ar.artdrawing.R

class AllImageListActivity : AppCompatActivity() {
    companion object {
        val IntentKey_Title = "category_name"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_all_image_list)
        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) { }
        val recycler = findViewById<RecyclerView>(R.id.list_recyclerview)
        val title = findViewById<TextView>(R.id.tv_category_name)
        val backLayout = findViewById<ImageView>(R.id.image_back)


        val categoryName = intent.getStringExtra(IntentKey_Title)
        title.text = categoryName?.let { CommonUtils.getDisPlayName(it) }
        categoryName?.let {
            CommonUtils.listAssetFiles(this@AllImageListActivity, it).let { list ->
                recycler.run {
                    adapter = InitCategoryList(it).apply {
                        updateData(list)
                    }
                    layoutManager = GridLayoutManager(this@AllImageListActivity, 2)
                    addItemDecoration(ItemDec(15, 15, 0))
                }
            }
        }

        backLayout.setOnClickListener {
            Log.d("---------------------","============setOnClickListener")
            finish()
        }

    }

    private fun InitCategoryList(category: String): AllImageListAdapter {
        val allImageListAdapter = AllImageListAdapter(category, this@AllImageListActivity) { path ->
            startActivity(Intent(this@AllImageListActivity, InfoActivity::class.java).apply {
                putExtra(InfoActivity.Companion.IntentKeyPath, path)
            })

        }
        return allImageListAdapter
    }
}