package com.design.mischiefgo.ut

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.design.mischiefgo.R
import com.design.mischiefgo.entity.Duplicate
import com.design.mischiefgo.ui.UseSoundActivity
import com.bumptech.glide.Glide
import com.design.mischiefgo.databinding.ItemStartAndEndBinding

class StartAndEndAdapter: RecyclerView.Adapter<StartAndEndAdapter.SelfViewHolder>() {
    private lateinit var context:Context
    private var infoList :List<Duplicate> = arrayListOf()
    private var type:Int=0
    private lateinit var listener:OnMoreClickListener

    class SelfViewHolder(val binding: ItemStartAndEndBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelfViewHolder {
        context=parent.context
        val binding=ItemStartAndEndBinding.inflate(LayoutInflater.from(context),parent,false)
        return SelfViewHolder(binding)
    }
    override fun getItemCount(): Int {
        return infoList.size
    }
    override fun onBindViewHolder(holder: SelfViewHolder, position: Int) {
        if (position == itemCount - 1) {
            holder.binding.vBottom.visibility = View.VISIBLE
        } else {
            holder.binding.vBottom.visibility = View.GONE
        }
        val myInfo = infoList[position]
        val covert: String = myInfo.convert
        Log.d("covert",covert+"typre$type")
        if(type==0){
            Glide.with(context).load(covert).placeholder(R.drawable.music_default_copy).error(R.drawable.music_default_copy).into(holder.binding.ivImage)
            holder.binding.imgLike.setImageDrawable(context.getDrawable(R.drawable.punctate_selector))
            holder.binding.imgLike.isSelected=true
            holder.binding.tint.visibility=View.VISIBLE
            if(myInfo.isCustomization){
                holder.binding.tint.text="resource:upload"
            }else{
                holder.binding.tint.text="resource:original"
            }

        }else{
            Glide.with(context).load(R.drawable.music_default_copy).placeholder(R.drawable.music_default_copy).error(R.drawable.music_default_copy).into(holder.binding.ivImage)
            holder.binding.imgLike.setImageDrawable(context.getDrawable(R.drawable.remove_any_small))
            holder.binding.tint.visibility=View.GONE
        }
        holder.binding.tvTitle.text = myInfo.name
        holder.binding.imgLike.setOnClickListener {
            this.listener.onMoreClick(myInfo)
        }
        holder.binding.item.setOnClickListener {
            val intent = Intent(context, UseSoundActivity::class.java)
            intent.putExtra(UseSoundActivity.KEY_LIST_DATA, myInfo)
            context.startActivity(intent)
        }
    }
    fun setList(list: List<Duplicate>,type:Int){
        this.type=type
        this.infoList=list
    }
    fun setListener(listener: OnMoreClickListener){
        this.listener=listener
    }
}