package melody.offline.music.adapter

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 melody.offline.music.R
import melody.offline.music.activity.MoPlayDetailsActivity
import melody.offline.music.databinding.MusicResponsiveItemBinding
import melody.offline.music.innertube.models.MusicCarouselShelfRenderer
import melody.offline.music.media.MediaControllerManager
import melody.offline.music.util.AnalysisUtil
import melody.offline.music.util.LogTag

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, videoId)

        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)

            if (itemClickListener != null) {
                itemClickListener?.onItemClick(position)
            }
        }
        holder.binding.moreBtn.setOnClickListener {
            if (itemMoreClickListener != null) {
                itemMoreClickListener?.onItemMoreClick(position)
            }
        }
    }

    override fun getItemCount(): Int = list.size

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

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

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


                val meController = MediaControllerManager.getController()
                if (meController != null && meController.currentMediaItem != null) {
                    if (meController.currentMediaItem?.mediaId == videoID) {
                        binding.listPlayView.visibility = View.VISIBLE
                        binding.nameTv.setTextColor(context.getColor(R.color.green))
                        binding.descTv.setTextColor(context.getColor(R.color.green_60))
                    } else {
                        binding.nameTv.setTextColor(context.getColor(R.color.white))
                        binding.descTv.setTextColor(context.getColor(R.color.white_60))
                        binding.listPlayView.visibility = View.GONE
                    }
                }
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    private var itemMoreClickListener: OnItemMoreClickListener? = null

    fun setOnItemMoreClickListener(listener: OnItemMoreClickListener) {
        itemMoreClickListener = listener
    }

    interface OnItemMoreClickListener {
        fun onItemMoreClick(position: Int)
    }
}