package com.soundapp.soundgags.setting

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.soundapp.soundgags.R
import com.soundapp.soundgags.datame.Reflection
import com.soundapp.soundgags.ui.NowPlayingActivity
import com.bumptech.glide.Glide
import com.soundapp.soundgags.databinding.ItemFavSolidBinding

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelfViewHolder {
        context=parent.context
        val binding=ItemFavSolidBinding.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")
        holder.binding.imgLike.isSelected = myInfo.isLike
        if(type==0){
            Glide.with(context).load(covert).placeholder(R.drawable.pic_only_diy).error(R.drawable.pic_only_diy).into(holder.binding.ivImage)
            holder.binding.imgLike.setImageDrawable(context.getDrawable(R.drawable.me_add_love_selector))
            holder.binding.imgDel.visibility=View.GONE
            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.pic_only_diy).placeholder(R.drawable.pic_only_diy).error(R.drawable.pic_only_diy).into(holder.binding.ivImage)
            holder.binding.imgDel.setImageDrawable(context.getDrawable(R.drawable.del_red_icon))
            holder.binding.tint.visibility=View.GONE
            holder.binding.imgDel.visibility=View.VISIBLE
        }
        holder.binding.tvTitle.text = myInfo.name
        holder.binding.imgLike.setOnClickListener {
            val isSelected= holder.binding.imgLike.isSelected
            holder.binding.imgLike.isSelected=!isSelected
            this.listener.onMoreClick(myInfo,isSelected)
        }
        holder.binding.imgDel.setOnClickListener {
            this.listener.onCustomizationClick(myInfo)
        }
        holder.binding.item.setOnClickListener {
            val intent = Intent(context, NowPlayingActivity::class.java)
            intent.putExtra(NowPlayingActivity.KEY_LIST_DATA, myInfo)
            context.startActivity(intent)
        }
    }
    fun setList(list: List<Reflection>,type:Int){
        this.type=type
        this.infoList=list
    }
    fun setListener(listener: LikeUpClickListener){
        this.listener=listener
    }
}