package com.drawing.drawsketch.all

import android.content.Intent
import android.os.Bundle
import android.widget.FrameLayout
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.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.drawing.drawsketch.App
import com.drawing.drawsketch.ItemSpace
import com.drawing.drawsketch.R
import com.drawing.drawsketch.Utils
import com.drawing.drawsketch.main.MainAdapter
import com.drawing.drawsketch.preview.PreviewActivity

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_category_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
        }

        val recycler = findViewById<RecyclerView>(R.id.list_recyclerview)
        val title = findViewById<TextView>(R.id.tv_category_name)
        findViewById<FrameLayout>(R.id.icon_back).setOnClickListener {
            finish()
        }

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

    }

    private fun InitCategoryList(category: String): CategoryAllAdapter {
        val categoryAllAdapter = CategoryAllAdapter(category, this@CategoryListActivity){ path->
            startActivity(Intent(this@CategoryListActivity, PreviewActivity::class.java).apply {
                putExtra(PreviewActivity.IntentKeyPath,path)
            })

        }

        return categoryAllAdapter
    }
}