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.activity.MoPlaylistSongsActivity
import melody.offline.music.bean.PlaylistItem
import melody.offline.music.databinding.PlayListItemBinding
import melody.offline.music.media.MediaControllerManager

class PlaylistSongsAdapter(
    private val context: Context,
    private val list: List<PlaylistItem>,
) : RecyclerView.Adapter<PlaylistSongsAdapter.ViewHolder>() {

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

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

        holder.binding.moreBtn.setOnClickListener {
            if (moreClickListener != null) {
                moreClickListener?.onMoreClick(position)
            }
        }

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

        holder.itemView.setOnClickListener {
            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.name)
            intent.putExtra(
                MoPlayDetailsActivity.PLAY_DETAILS_COME_FROM, MoPlaylistSongsActivity::class.java
            )
            intent.putExtra(MoPlayDetailsActivity.PLAY_LIST_ID, bean.playlistId)
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: PlaylistItem) {

            binding.apply {
                Glide.with(context).load(bean.thumbnail).into(image)
                title.text = bean.title
                if (bean.name.isEmpty()) {
                    name.visibility = View.GONE
                } else {
                    name.visibility = View.VISIBLE
                    name.text = bean.name
                }
                if (bean.isOffline) {
                    size.visibility = View.VISIBLE
                    downloadCoImg.visibility = View.VISIBLE
                    downloadImg.setImageResource(R.drawable.download_done_icon)
                } else {
                    downloadCoImg.visibility = View.GONE
                    downloadImg.setImageResource(R.drawable.download_icon)
                    size.visibility = View.GONE
                }
                if (bean.size?.isNotEmpty() == true) {
                    size.text = bean.size
                    size.visibility = View.VISIBLE
                } else {
                    size.visibility = View.GONE
                }
            }
            val meController = MediaControllerManager.getController()
            if (meController != null && meController.currentMediaItem != null) {
                if (meController.currentMediaItem?.mediaId == bean.videoId) {
                    binding.listPlayView.visibility = View.VISIBLE
                    binding.title.setTextColor(context.getColor(R.color.green))
                    binding.name.setTextColor(context.getColor(R.color.green_60))
                    binding.size.setTextColor(context.getColor(R.color.green_60))
                } else {
                    binding.title.setTextColor(context.getColor(R.color.white))
                    binding.name.setTextColor(context.getColor(R.color.white_60))
                    binding.size.setTextColor(context.getColor(R.color.white_60))
                    binding.listPlayView.visibility = View.GONE
                }
            }
        }
    }

    private var itemDownloadClickListener: OnItemDownloadClickListener? = null

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

    interface OnItemDownloadClickListener {
        fun onDownloadItemClick(position: Int)
    }

    private var itemClickListener: OnItemClickListener? = null

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

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    private var moreClickListener: OnMoreClickListener? = null

    fun setOnMoreClickListener(listener: OnMoreClickListener) {
        moreClickListener = listener
    }

    interface OnMoreClickListener {
        fun onMoreClick(position: Int)
    }
}