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.MoOfflineSongsActivity
import melody.offline.music.activity.MoPlayDetailsActivity
import melody.offline.music.bean.PlaylistItem
import melody.offline.music.databinding.OfflineListItemBinding
import melody.offline.music.media.MediaControllerManager

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = OfflineListItemBinding.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.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, MoOfflineSongsActivity::class.java
            )
            context.startActivity(intent)

        }
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(val binding: OfflineListItemBinding) :
        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
                }
                size.text = bean.size

                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 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)
    }
}