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.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.activity.MoListDetailsActivity
import com.player.musicoo.activity.MoPlayDetailsActivity
import com.player.musicoo.activity.MoSingerDetailsActivity
import com.player.musicoo.databinding.MusicTowRowItemBinding
import com.player.musicoo.databinding.PlayListItemBinding
import com.player.musicoo.databinding.SearchResultOtherItemBinding
import com.player.musicoo.databinding.SearchResultOtherLayoutBinding
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.media.MediaControllerManager
import com.player.musicoo.util.LogTag

class SingerMoreSongAdapter(
    private val context: Context,
    private val list: List<Innertube.BaseSingleColumnBrowseSongResults.SingleColumnBrowseSongResults>,
    private val type: String? = null,
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_ONE -> {
                val binding =
                    SearchResultOtherItemBinding.inflate(
                        LayoutInflater.from(context),
                        parent,
                        false
                    )
                ViewHolderOne(binding)
            }

            TYPE_TWO -> {
                val binding =
                    MusicTowRowItemBinding.inflate(LayoutInflater.from(context), parent, false)
                ViewHolderTow(binding)
            }

            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bean = list[position]
        when (holder.itemViewType) {
            TYPE_ONE -> {
                val typeOneHolder = holder as ViewHolderOne
                typeOneHolder.bind(bean)
                typeOneHolder.itemView.setOnClickListener {
                    val intent = Intent(context, MoPlayDetailsActivity::class.java)
                    intent.putExtra(
                        MoPlayDetailsActivity.PLAY_DETAILS_VIDEO_ID,
                        bean.videoId
                    )
                    intent.putExtra(
                        MoPlayDetailsActivity.PLAY_DETAILS_PLAY_LIST_ID,
                        bean.playlistId
                    )
                    intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, bean.title)
                    intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, bean.name)
                    context.startActivity(intent)
                }
            }

            TYPE_TWO -> {
                val typeTwoHolder = holder as ViewHolderTow
                typeTwoHolder.bind(bean)
                typeTwoHolder.itemView.setOnClickListener {
                    val intent = Intent(context, MoListDetailsActivity::class.java)
                    intent.putExtra(
                        MoListDetailsActivity.PLAY_LIST_PAGE_BROWSE_ID,
                        bean.browseId
                    )
                    context.startActivity(intent)
                }
            }
        }
    }

    private val TYPE_ONE = 1
    private val TYPE_TWO = 2
    override fun getItemViewType(position: Int): Int {
        return if (type == "MUSIC_PAGE_TYPE_ARTIST_DISCOGRAPHY") {
            TYPE_TWO
        } else {
            TYPE_ONE
        }
    }

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.BaseSingleColumnBrowseSongResults.SingleColumnBrowseSongResults) {

            binding.apply {
                Glide.with(context)
                    .load(bean.thumbnail)
                    .into(image)
                title.text = bean.title
                name.text = bean.name
            }
        }
    }

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.BaseSingleColumnBrowseSongResults.SingleColumnBrowseSongResults) {

            binding.apply {
                Glide.with(context)
                    .load(bean.thumbnail)
                    .into(image)
                nameTv.text = bean.title
                descTv.text = bean.name
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

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