package com.photobooth.app

import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView

class FrameItemAdapter(
    private var items: List<FrameItem>,
    private val onFrameSelected: (FrameItem) -> Unit
) : RecyclerView.Adapter<FrameItemAdapter.ViewHolder>() {

    private var selectedPosition = 0 // Mặc định ở vị trí 0 (Mặc định - Không dùng khung)

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val cardSubFrame: MaterialCardView = view.findViewById(R.id.cardSubFrame)
        val imgFrameThumb: ImageView = view.findViewById(R.id.imgFrameThumb)
        val txtFrameName: TextView = view.findViewById(R.id.txtFrameName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_frame_sub, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        holder.txtFrameName.text = item.name

        // Set thumbnail
        if (item.id == "DEFAULT") {
            holder.imgFrameThumb.setImageResource(R.drawable.ic_block_white)
            holder.txtFrameName.text = holder.itemView.context.getString(R.string.default_frame)
        } else {
            try {
                val path = if (item.imageFileName.startsWith("frames/")) {
                    item.imageFileName
                } else {
                    "frames/${item.imageFileName}"
                }
                val inputStream = holder.itemView.context.assets.open(path)
                val bitmap = BitmapFactory.decodeStream(inputStream)
                holder.imgFrameThumb.setImageBitmap(bitmap)
            } catch (e: Exception) {
                holder.imgFrameThumb.setImageDrawable(null)
            }
        }

        val strokeWidthPx = (3 * holder.itemView.context.resources.displayMetrics.density).toInt()

        if (position == selectedPosition) {
            holder.cardSubFrame.strokeColor = ContextCompat.getColor(holder.itemView.context, R.color.theme_primary)
            holder.cardSubFrame.strokeWidth = strokeWidthPx
        } else {
            holder.cardSubFrame.strokeColor = ContextCompat.getColor(holder.itemView.context, R.color.theme_transparent)
            holder.cardSubFrame.strokeWidth = 0
        }

        holder.itemView.setOnClickListener {
            val previousSelected = selectedPosition
            selectedPosition = position
            notifyItemChanged(previousSelected)
            notifyItemChanged(selectedPosition)
            onFrameSelected(item)
        }
    }

    override fun getItemCount(): Int = items.size

    fun updateItems(newItems: List<FrameItem>) {
        items = newItems
        selectedPosition = 0 // Reset to default when switching stack
        notifyDataSetChanged()
    }
}
