package com.player.musicoo.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.player.musicoo.activity.MoPlayDetailsActivity
import com.player.musicoo.databinding.MusicResponsiveItemBinding
import com.player.musicoo.innertube.models.MusicCarouselShelfRenderer
import com.player.musicoo.innertube.models.bodies.NextBody

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

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

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

        val url = bean.musicResponsiveListItemRenderer
            ?.thumbnail
            ?.musicThumbnailRenderer
            ?.thumbnail
            ?.thumbnails
            ?.let { it.getOrNull(1) ?: it.getOrNull(0) }
            ?.url
        val name = bean.musicResponsiveListItemRenderer
            ?.flexColumns?.get(0)
            ?.musicResponsiveListItemFlexColumnRenderer
            ?.text
            ?.runs
            ?.firstOrNull()
            ?.text
        val desc = bean.musicResponsiveListItemRenderer
            ?.flexColumns?.get(1)
            ?.musicResponsiveListItemFlexColumnRenderer
            ?.text
            ?.runs
            ?.firstOrNull()
            ?.text

        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

        holder.bind(url, name, desc)

        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)
            if (playlistSetVideoId != null) {
                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: MusicResponsiveItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(url: String?, name: String?, desc: String?) {

            binding.apply {
                Glide.with(context)
                    .load(url)
                    .into(image)
                nameTv.text = name
                descTv.text = desc
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

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