package melody.offline.music.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.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import melody.offline.music.R
import melody.offline.music.activity.MoPlayDetailsActivity
import melody.offline.music.databinding.DetailsListItemBinding
import melody.offline.music.innertube.Innertube
import melody.offline.music.media.MediaControllerManager

class DetailsListAdapter(
    private val context: Context,
    private val list: List<Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean>,
) :
    RecyclerView.Adapter<DetailsListAdapter.ViewHolder>() {

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

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

        val videoId = bean.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, bean.playlistId)
            intent.putExtra(
                MoPlayDetailsActivity.PLAY_DETAILS_PLAY_LIST_SET_VIDEO_ID,
                bean.playlistSetVideoId
            )
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_PLAY_PARAMS, bean.params)

            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, bean.title)
            intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, bean.name)
            context.startActivity(intent)
        }

        holder.binding.downloadBtn.setOnClickListener {
            if (itemDownloadClickListener != null) {
                itemDownloadClickListener?.onItemDownloadClick(position)
            }
        }

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

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean) {
            binding.apply {
                if (!bean.thumbnailUrl.isNullOrEmpty()) {
                    image.visibility = View.VISIBLE
                    Glide.with(context)
                        .load(bean.thumbnailUrl)
                        .into(image)
                    sortTv.visibility = View.GONE
                } else {
                    image.visibility = View.GONE
                    sortTv.visibility = View.VISIBLE
                    sortTv.text = "${bindingAdapterPosition + 1}"
                }

                title.text = bean.title
                if (bean.name.isNullOrEmpty()) {
                    name.visibility = View.GONE
                } else {
                    name.visibility = View.VISIBLE
                    name.text = bean.name
                }
                val meController = MediaControllerManager.getController()
                if (meController != null && meController.currentMediaItem != null) {
                    if (meController.currentMediaItem?.mediaId == bean.videoId) {
                        binding.title.setTextColor(context.getColor(R.color.green))
                        binding.name.setTextColor(context.getColor(R.color.green_60))

                        binding.listPlayView.visibility = View.VISIBLE
                        binding.sortTv.visibility = View.GONE
                        if (image.visibility == View.GONE) {
                            binding.listPlayView.setBackgroundColor(context.getColor(R.color.transparent))
                        } else {
                            binding.listPlayView.setBackgroundColor(context.getColor(R.color.black_60))
                        }
                    } else {
                        binding.title.setTextColor(context.getColor(R.color.white))
                        binding.name.setTextColor(context.getColor(R.color.white_60))

                        binding.listPlayView.visibility = View.GONE
                        if (image.visibility == View.GONE) {
                            binding.sortTv.visibility = View.VISIBLE
                        }
                    }
                }
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    private var itemDownloadClickListener: OnItemDownloadClickListener? = null

    fun setOnItemDownloadClickListener(listener: OnItemDownloadClickListener) {
        itemDownloadClickListener = listener
    }

    interface OnItemDownloadClickListener {
        fun onItemDownloadClick(position: Int)
    }

    private var itemMoreClickListener: OnItemMoreClickListener? = null

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

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