package com.player.musicoo.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.player.musicoo.App
import com.player.musicoo.R
import com.player.musicoo.activity.MoPlayDetailsActivity
import com.player.musicoo.activity.PlayDetailsActivity
import com.player.musicoo.bean.Audio
import com.player.musicoo.databinding.DetailsListItemBinding
import com.player.musicoo.databinding.MusicResponsiveItemBinding
import com.player.musicoo.databinding.SoundsOfAppliancesLayoutBinding
import com.player.musicoo.databinding.SoundsOfNatureLayoutBinding
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.models.MusicCarouselShelfRenderer
import com.player.musicoo.util.convertMillisToMinutesAndSecondsString
import com.player.musicoo.util.getAudioDurationFromAssets

class DetailsListAdapter(
    private val context: Context,
    private val list: List<Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean>,
) :
    RecyclerView.Adapter<DetailsListAdapter.ViewHolder>() {

    companion object {
        const val FROM_TAG = "list_details_activity_to_adapter"
    }

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

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

        val videoId = bean.videoId

        holder.itemView.setOnClickListener {
            val intent = Intent(context, MoPlayDetailsActivity::class.java)
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_VIDEO_ID, videoId)
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_PLAY_LIST_ID, bean.playlistId)
            intent.putExtra(
                MoPlayDetailsActivity.PLAY_DETAILS_PLAY_LIST_SET_VIDEO_ID,
                bean.playlistSetVideoId
            )
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_PLAY_PARAMS, bean.params)

            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, bean.name)
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, bean.title)
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean) {

            binding.apply {
                if (!bean.thumbnailUrl.isNullOrEmpty()) {
                    image.visibility = View.VISIBLE
                    Glide.with(context)
                        .load(bean.thumbnailUrl)
                        .into(image)
                    sortTv.visibility = View.GONE
                } else {
                    image.visibility = View.GONE
                    sortTv.visibility = View.VISIBLE
                    sortTv.text = "${bindingAdapterPosition + 1}"
                }

                title.text = bean.title
                if (bean.name.isNullOrEmpty()) {
                    name.visibility = View.GONE
                } else {
                    name.visibility = View.VISIBLE
                    name.text = bean.name
                }

            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

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