package com.player.musicoo.adapter

import android.app.Activity
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.MoListDetailsActivity
import com.player.musicoo.activity.MoPlayDetailsActivity
import com.player.musicoo.activity.MoSingerDetailsActivity
import com.player.musicoo.databinding.MusicTowRowItemBinding
import com.player.musicoo.innertube.models.MusicCarouselShelfRenderer

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

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

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

        val browseEndpoint = bean.musicTwoRowItemRenderer
            ?.title
            ?.runs
            ?.firstOrNull()
            ?.navigationEndpoint
            ?.browseEndpoint

        val browseId = browseEndpoint?.browseId

        val pageType = browseEndpoint?.type

        val watchEndpoint = bean.musicTwoRowItemRenderer
            ?.navigationEndpoint
            ?.watchEndpoint

        val videoId = watchEndpoint?.videoId
        val playlistId = watchEndpoint?.playlistId
        val playlistSetVideoId = watchEndpoint?.playlistSetVideoId
        val params = watchEndpoint?.params


        val url = bean.musicTwoRowItemRenderer
            ?.thumbnailRenderer
            ?.musicThumbnailRenderer
            ?.thumbnail
            ?.thumbnails
            ?.let { it.getOrNull(1) ?: it.getOrNull(0) }
            ?.url ?: ""
        val name = bean.musicTwoRowItemRenderer
            ?.title
            ?.runs
            ?.firstOrNull()
            ?.text ?: ""
        val desc = bean.musicTwoRowItemRenderer
            ?.subtitle
            ?.runs
            ?.map { it.text }
            ?.joinToString("") ?: ""

        holder.bind(url = url, name = name, desc = desc)

        holder.itemView.setOnClickListener {
            if (browseId.isNullOrEmpty()) {
                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)
            } else {
                if (pageType == "MUSIC_PAGE_TYPE_ARTIST") {
                    val intent = Intent(context, MoSingerDetailsActivity::class.java)
                    intent.putExtra(
                        MoSingerDetailsActivity.SINGER_DETAILS_PAGE_BROWSE_ID,
                        browseId
                    )
                    context.startActivity(intent)
                    if (context is MoSingerDetailsActivity) {
                        context.finish()
                    }
                } else {
                    val intent = Intent(context, MoListDetailsActivity::class.java)
                    intent.putExtra(MoListDetailsActivity.PLAY_LIST_PAGE_BROWSE_ID, browseId)
                    context.startActivity(intent)
                }
            }
        }
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(private val binding: MusicTowRowItemBinding) :
        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)
    }
}