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.innertube.models.MusicShelfRenderer
import com.player.musicoo.util.convertMillisToMinutesAndSecondsString
import com.player.musicoo.util.getAudioDurationFromAssets

class SingerDetailsSongListAdapter(
    private val context: Context,
    private val list: List<MusicShelfRenderer.Content>,
) :
    RecyclerView.Adapter<SingerDetailsSongListAdapter.ViewHolder>() {

    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 watchEndpoint = bean.musicResponsiveListItemRenderer
            ?.flexColumns
            ?.firstOrNull()
            ?.musicResponsiveListItemFlexColumnRenderer
            ?.text
            ?.runs
            ?.firstOrNull()
            ?.navigationEndpoint
            ?.watchEndpoint
        val videoId = watchEndpoint?.videoId
        val playlistId = watchEndpoint?.playlistId
        val playlistSetVideoId = watchEndpoint?.playlistSetVideoId
        val params = watchEndpoint?.params
        val flexColumns = bean.musicResponsiveListItemRenderer
            ?.flexColumns
        val name = flexColumns?.get(0)?.musicResponsiveListItemFlexColumnRenderer?.text?.text
        val desc = flexColumns?.get(1)?.musicResponsiveListItemFlexColumnRenderer?.text?.text

        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, playlistId)
            intent.putExtra(
                MoPlayDetailsActivity.PLAY_DETAILS_PLAY_LIST_SET_VIDEO_ID,
                playlistSetVideoId
            )
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_PLAY_PARAMS, params)

            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, name)
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, desc)
            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: MusicShelfRenderer.Content) {

            binding.apply {

                val thumbnailUrl = bean.musicResponsiveListItemRenderer
                    ?.thumbnail
                    ?.musicThumbnailRenderer
                    ?.thumbnail
                    ?.thumbnails?.let {
                        it.getOrNull(2) ?: it.getOrNull(1) ?: it.getOrNull(0)
                    }?.url

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

                title.text = bean.musicResponsiveListItemRenderer
                    ?.flexColumns
                    ?.firstOrNull()
                    ?.musicResponsiveListItemFlexColumnRenderer
                    ?.text
                    ?.text
                val desc = bean.musicResponsiveListItemRenderer
                    ?.flexColumns
                    ?.drop(1)
                    ?.map { it.musicResponsiveListItemFlexColumnRenderer?.text?.text }
                    ?.joinToString(" ")
                if (desc.isNullOrEmpty()) {
                    name.visibility = View.GONE
                } else {
                    name.visibility = View.VISIBLE
                    name.text = desc
                }

            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

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