package com.example.myapplication.ui.shop.myskin

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.myapplication.R
import com.example.myapplication.network.themeStyle

class MySkinAdapter(
    private val onItemClick: (themeStyle) -> Unit,
    private val onSelectionChanged: (count: Int) -> Unit
) : RecyclerView.Adapter<MySkinAdapter.VH>() {

    private val items = mutableListOf<themeStyle>()
    private val selectedIds = mutableSetOf<Int>()

    var editMode: Boolean = false
        private set

    fun submitList(list: List<themeStyle>) {
        items.clear()
        items.addAll(list)
        selectedIds.clear()
        onSelectionChanged(0)
        notifyDataSetChanged()
    }

    fun enterEditMode() {
        if (editMode) return
        editMode = true
        selectedIds.clear()
        onSelectionChanged(0)
        notifyDataSetChanged()
    }

    fun exitEditMode() {
        if (!editMode) return
        editMode = false
        selectedIds.clear()
        onSelectionChanged(0)
        notifyDataSetChanged()
    }

    fun getSelectedIds(): List<Int> = selectedIds.toList()

    fun removeByIds(ids: Set<Int>) {
        items.removeAll { ids.contains(it.id) }
        selectedIds.removeAll(ids)
        onSelectionChanged(selectedIds.size)
        notifyDataSetChanged()
    }

    inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivPreview: ImageView = itemView.findViewById(R.id.ivPreview)
        val tvName: TextView = itemView.findViewById(R.id.tvName)
        val overlay: View = itemView.findViewById(R.id.overlay)
        val ivCheck: ImageView = itemView.findViewById(R.id.ivCheck)
    }

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

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = items[position]

        holder.tvName.text = item.themeName

        Glide.with(holder.itemView)
            .load(item.themePreviewImageUrl)
            .placeholder(R.drawable.default_avatar)
            .into(holder.ivPreview)

        val selected = selectedIds.contains(item.id)

        if (editMode) {
            holder.ivCheck.visibility = View.VISIBLE
            holder.overlay.visibility = if (selected) View.VISIBLE else View.GONE
            holder.ivCheck.alpha = if (selected) 1f else 0.0f
        } else {
            holder.ivCheck.visibility = View.GONE
            holder.overlay.visibility = View.GONE
        }

        holder.itemView.setOnClickListener {
            if (editMode) {
                if (selected) selectedIds.remove(item.id) else selectedIds.add(item.id)
                onSelectionChanged(selectedIds.size)
                notifyItemChanged(position)
            } else {
                // 跳转到主题详情页面
                val bundle = Bundle().apply {
                    putInt("themeId", item.id)
                }
                holder.itemView.findNavController().navigate(R.id.action_global_keyboardDetailFragment, bundle)
            }
        }
    }
}
