package com.player.musicoo.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.player.musicoo.R
import com.player.musicoo.databinding.PlayListItemBinding
import com.player.musicoo.media.MediaControllerManager

class PlayListAdapter(
    private val context: Context,
    private val list: List<MediaItem>,
) :
    RecyclerView.Adapter<PlayListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = PlayListItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position]
        holder.bind(bean)

        holder.itemView.setOnClickListener {
            val meController = MediaControllerManager.getController()
            if (meController != null && meController.currentMediaItem != null) {
                var index = holder.bindingAdapterPosition
                if (index > meController.mediaItemCount) {
                    index = 1
                }
                meController.seekTo(index, C.TIME_UNSET)
                if (!meController.isPlaying) {
                    meController.prepare()
                    meController.play()
                }
            }
        }
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(private val binding: PlayListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        @SuppressLint("SetTextI18n")
        fun bind(bean: MediaItem) {

            binding.apply {
                val meController = MediaControllerManager.getController()
                if (meController != null && meController.currentMediaItem != null) {
                    if (meController.currentMediaItem?.mediaId == bean.mediaId) {
                        binding.currentPlayingLayout.visibility = View.VISIBLE
                        binding.title.setTextColor(context.getColor(R.color.green))
                        binding.name.setTextColor(context.getColor(R.color.green_60))
                    } else {
                        binding.currentPlayingLayout.visibility = View.GONE
                        binding.title.setTextColor(context.getColor(R.color.white))
                        binding.name.setTextColor(context.getColor(R.color.white_60))
                    }
                }

                Glide.with(context)
                    .load(bean.mediaMetadata.artworkUri)
                    .into(image)
                title.text = bean.mediaMetadata.title
                if (bean.mediaMetadata.artist.isNullOrEmpty()) {
                    name.visibility = View.GONE
                } else {
                    name.visibility = View.VISIBLE
                    name.text = bean.mediaMetadata.artist
                }

            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        itemClickListener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}