package kr.co.vividnext.sodalive.audio_content.series

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemSeriesListBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class SeriesListAdapter(
    private val itemWidth: Int,
    private val onClickItem: (Long) -> Unit,
    private val onClickCreator: (Long) -> Unit,
    private val isVisibleCreator: Boolean
) : RecyclerView.Adapter<SeriesListAdapter.ViewHolder>() {

    val items = mutableListOf<GetSeriesListResponse.SeriesListItem>()

    inner class ViewHolder(
        private val binding: ItemSeriesListBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(item: GetSeriesListResponse.SeriesListItem) {
            val lp = binding.ivCover.layoutParams as ConstraintLayout.LayoutParams
            lp.width = itemWidth
            lp.height = itemWidth * 432 / 306
            binding.ivCover.layoutParams = lp

            binding.ivCover.load(item.coverImage) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(RoundedCornersTransformation(5f.dpToPx()))
            }

            binding.tvTitle.text = item.title
            binding.tvSeriesContentCount.text = "총 ${item.numberOfContent}화"
            binding.tvPublishedDaysOfWeek.text = item.publishedDaysOfWeek

            binding.tvNew.visibility = if (item.isNew) {
                View.VISIBLE
            } else {
                View.GONE
            }

            binding.tvPopular.visibility = if (item.isPopular) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.isComplete) {
                binding.tvNew.visibility = View.GONE
                binding.tvComplete.visibility = View.VISIBLE
            } else {
                binding.tvComplete.visibility = View.GONE
            }

            if (isVisibleCreator) {
                binding.llCreator.visibility = View.VISIBLE
                binding.tvCreator.text = item.creator.nickname
                binding.ivCreator.load(item.creator.profileImage) {
                    crossfade(true)
                    placeholder(R.drawable.ic_place_holder)
                    transformations(CircleCropTransformation())
                }
                binding.llCreator.setOnClickListener { onClickCreator(item.creator.creatorId) }
            } else {
                binding.llCreator.visibility = View.GONE
            }

            binding.root.setOnClickListener { onClickItem(item.seriesId) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        ItemSeriesListBinding.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 addItems(items: List<GetSeriesListResponse.SeriesListItem>) {
        this.items.addAll(items)
        notifyDataSetChanged()
    }

    fun clear() {
        this.items.clear()
    }
}
