package kr.co.vividnext.sodalive.live.recommend_channel

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

class LiveRecommendChannelAdapter(
    private val onClick: (Long) -> Unit,
    private val onClickMore: () -> Unit,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var isFollowedCreatorLive = false

    private val items = mutableListOf<GetRecommendChannelResponse>()

    class FooterViewHolder(
        private val binding: ItemRecommendChannelBinding,
        private val onClickItem: () -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind() {
            binding.ivRecommendChannel.setImageResource(R.drawable.btn_item_more)
            binding.tvRecommendChannelNickname.text = "더보기"

            val layoutParams = binding.ivRecommendChannel.layoutParams as FrameLayout.LayoutParams
            layoutParams.width = 60f.dpToPx().toInt()
            layoutParams.height = 60f.dpToPx().toInt()

            binding.ivRecommendChannel.layoutParams = layoutParams
            binding.ivRecommendChannelBg.layoutParams = layoutParams

            binding.tvRecommendChannel.visibility = View.GONE
            binding.ivRecommendChannelBg.visibility = View.GONE
            binding.root.setOnClickListener { onClickItem() }
        }

        companion object {
            const val VIEW_TYPE = 1
        }
    }

    class ViewHolder(
        private val binding: ItemRecommendChannelBinding,
        private val onClickItem: (Long) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: GetRecommendChannelResponse) {
            binding.ivRecommendChannel.load(item.profileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(30f.dpToPx()))
            }
            binding.tvRecommendChannelNickname.text = item.nickname

            val layoutParams = binding.ivRecommendChannel.layoutParams as FrameLayout.LayoutParams
            layoutParams.width = 60f.dpToPx().toInt()
            layoutParams.height = 60f.dpToPx().toInt()

            binding.ivRecommendChannel.layoutParams = layoutParams
            binding.ivRecommendChannelBg.layoutParams = layoutParams

            if (item.isOnAir) {
                binding.tvRecommendChannel.visibility = View.VISIBLE
                binding.ivRecommendChannelBg.visibility = View.VISIBLE
            } else {
                binding.tvRecommendChannel.visibility = View.GONE
                binding.ivRecommendChannelBg.visibility = View.GONE
            }

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

        companion object {
            const val VIEW_TYPE = 0
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == FooterViewHolder.VIEW_TYPE) {
            FooterViewHolder(
                ItemRecommendChannelBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
                onClickItem = onClickMore
            )
        } else {
            ViewHolder(
                ItemRecommendChannelBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
                onClickItem = onClick
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is FooterViewHolder) {
            holder.bind()
        } else {
            (holder as ViewHolder).bind(items[position])
        }
    }

    override fun getItemCount(): Int {
        return if (isFollowedCreatorLive) {
            items.size + 1
        } else {
            items.size
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == items.size) {
            FooterViewHolder.VIEW_TYPE
        } else {
            ViewHolder.VIEW_TYPE
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(items: List<GetRecommendChannelResponse>) {
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}
