package kr.co.vividnext.sodalive.audio_content.playlist.create.add_content

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.audio_content.order.GetAudioContentOrderListItem
import kr.co.vividnext.sodalive.databinding.ItemPlaylistAddContentBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class PlaylistAddContentAdapter(
    private val selectedContentIdList: Set<Long>,
    private val onItemClick: (GetAudioContentOrderListItem, Boolean) -> Boolean
) : RecyclerView.Adapter<PlaylistAddContentAdapter.ViewHolder>() {

    var items = mutableListOf<GetAudioContentOrderListItem>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemPlaylistAddContentBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        private var isChecked = false

        fun bind(item: GetAudioContentOrderListItem) {
            if (selectedContentIdList.contains(item.contentId)) {
                binding.ivAdd.setImageResource(R.drawable.ic_check_blue)
                isChecked = true
            } else {
                binding.ivAdd.setImageResource(R.drawable.ic_playlist_add)
                isChecked = false
            }

            Glide
                .with(context)
                .load(item.coverImageUrl)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundedCorners(5.3f.dpToPx().toInt())
                    )
                )
                .into(binding.ivCover)

            binding.tvTitle.text = item.title
            binding.tvTheme.text = item.themeStr
            binding.tvDuration.text = item.duration

            binding.ivAdd.setOnClickListener {
                isChecked = !isChecked

                if (onItemClick(item, isChecked)) {
                    binding.ivAdd.setImageResource(
                        if (isChecked) {
                            R.drawable.ic_check_blue
                        } else {
                            R.drawable.ic_playlist_add
                        }
                    )
                } else {
                    isChecked = !isChecked
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        parent.context,
        ItemPlaylistAddContentBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount() = items.count()

    @SuppressLint("NotifyDataSetChanged")
    fun clear() {
        items.clear()
        notifyDataSetChanged()
    }
}
