package com.apps.quicksketchtrace.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.apps.quicksketchtrace.BApplication
import com.apps.quicksketchtrace.databinding.ItemMainCategoryBinding
import com.apps.quicksketchtrace.listener.MainClickListener
import com.apps.quicksketchtrace.util.OpenConvert

class DoubleLineAdapter: RecyclerView.Adapter<DoubleLineAdapter.DoubleViewHolder>() {
    private lateinit var context:Context
    private val nameList: List<String>? = BApplication.classNameList
    private lateinit var listener:MainClickListener
    class DoubleViewHolder(val binding:ItemMainCategoryBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DoubleViewHolder {
        context=parent.context
        val binding=ItemMainCategoryBinding.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 = OpenConvert.getShowPic(context, name)
        if(showPic!=null){
            val bitmap = OpenConvert.loadBitmapFromAssets(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)
        }
    }
    fun setListener(listener: MainClickListener){
        this.listener=listener
    }
}