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.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 SearchResultOtherAdapter(
    private val context: Context,
    private val list: List<Innertube.SearchDataPage.SearchResult>,
) :
    RecyclerView.Adapter<SearchResultOtherAdapter.ViewHolder>() {

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

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

        holder.itemView.setOnClickListener {
            LogTag.LogD(
                LogTag.VO_ACT_LOG,
                "SearchResultOtherAdapter bean.pageType->${bean.pageType}"
            )
            if (!bean.videoId.isNullOrEmpty()) {
                val intent = Intent(context, MoPlayDetailsActivity::class.java)
                intent.putExtra(
                    MoPlayDetailsActivity.PLAY_DETAILS_VIDEO_ID,
                    bean.videoId
                )
                intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, bean.title)
                intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, bean.desc)
                context.startActivity(intent)
            } else if (!bean.browseId.isNullOrEmpty()) {
                when (bean.pageType) {
                    "MUSIC_PAGE_TYPE_ARTIST" -> {
                        val intent = Intent(context, MoSingerDetailsActivity::class.java)
                        intent.putExtra(
                            MoSingerDetailsActivity.SINGER_DETAILS_PAGE_BROWSE_ID,
                            bean.browseId
                        )
                        context.startActivity(intent)
                    }

                    else -> {
                        val intent = Intent(context, MoListDetailsActivity::class.java)
                        intent.putExtra(
                            MoListDetailsActivity.PLAY_LIST_PAGE_BROWSE_ID,
                            bean.browseId
                        )
                        context.startActivity(intent)
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.SearchDataPage.SearchResult) {

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

    private var itemClickListener: OnItemClickListener? = null

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

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