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.MoLikedSongsActivity
import melody.offline.music.activity.MoPlayDetailsActivity
import melody.offline.music.bean.FavoriteBean
import melody.offline.music.databinding.LikedListItemBinding
import melody.offline.music.media.MediaControllerManager

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = LikedListItemBinding.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 {
            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,
                MoLikedSongsActivity::class.java
            )
            context.startActivity(intent)
        }
        holder.binding.favoritesBtn.setOnClickListener {
            if (itemFavoritesClickListener != null) {
                itemFavoritesClickListener?.onFavoritesItemClick(position)
            }
        }
        holder.binding.moreBtn.setOnClickListener {
            if (moreClickListener != null) {
                moreClickListener?.onMoreClick(position)
            }
        }
    }

    override fun getItemCount(): Int = list.size

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

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

            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.isFavorite) {
                    binding.favoritesImg.setImageResource(R.drawable.favorited_icon)
                } else {
                    binding.favoritesImg.setImageResource(R.drawable.not_favorited_icon)
                }
                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))
                    } else {
                        binding.title.setTextColor(context.getColor(R.color.white))
                        binding.name.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 itemFavoritesClickListener: OnItemFavoritesClickListener? = null

    fun setOnFavoritesItemClickListener(listener: OnItemFavoritesClickListener) {
        itemFavoritesClickListener = listener
    }

    interface OnItemFavoritesClickListener {
        fun onFavoritesItemClick(position: Int)
    }

    private var moreClickListener: OnMoreClickListener? = null

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

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