package com.apps.sketchmaster.adapter

import android.content.Context
import android.graphics.RenderEffect
import android.graphics.Shader
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.apps.sketchmaster.SketchApp
import com.apps.sketchmaster.databinding.ItemShowPageBinding
import com.apps.sketchmaster.event.DeleteClickListener
import com.apps.sketchmaster.utils.AllUtils

class TwoTypeAdapter: RecyclerView.Adapter<TwoTypeAdapter.DoubleViewHolder>() {
    private lateinit var context:Context
    private val nameList: List<String>? = SketchApp.baseNameList
    private lateinit var listener:DeleteClickListener
    class DoubleViewHolder(val binding: ItemShowPageBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DoubleViewHolder {
        context=parent.context
        val binding=ItemShowPageBinding.inflate(LayoutInflater.from(context),parent,false)
        return DoubleViewHolder(binding)
    }
    override fun getItemCount(): Int {
        return nameList?.size ?: 0
    }
    override fun onBindViewHolder(holder: DoubleViewHolder, position: Int) {
        val name = nameList!![position]
        val realName = name.substring(name.lastIndexOf("_") + 1)
        holder.binding.name.text = realName
        val showPic = AllUtils.getCoverPic(context, name)
        if(showPic!=null){
            val bitmap = AllUtils.switchTypeFromAssets(context, showPic)
            holder.binding.image.setImageBitmap(bitmap)
        }
        if (position == itemCount - 1) {
            holder.binding.vBottom.visibility = View.VISIBLE
        } else {
            holder.binding.vBottom.visibility = View.GONE
        }
        holder.binding.image.setOnClickListener {
            listener.onClick(name)
        }
//        setBlur(holder.binding)
    }
    fun setBlur(binding: ItemShowPageBinding){
        // 应用模糊效果
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            // Android 12及以上使用RenderEffect
            binding.image.setRenderEffect(RenderEffect.createBlurEffect(5f, 5f, Shader.TileMode.CLAMP));
        }
    }

    fun setListener(listener: DeleteClickListener){
        this.listener=listener
    }
}