package kr.co.vividnext.sodalive.audio_content.main.curation

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.audio_content.main.AudioContentMainContentAdapter
import kr.co.vividnext.sodalive.audio_content.main.GetAudioContentCurationResponse
import kr.co.vividnext.sodalive.audio_content.main.GetAudioContentMainItem
import kr.co.vividnext.sodalive.databinding.ItemAudioContentMainCurationBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class AudioContentMainCurationAdapter(
    private val onClickItem: (Long) -> Unit,
    private val onClickCreator: (Long) -> Unit,
    private val onClickCurationMore: (Long, String) -> Unit
) : RecyclerView.Adapter<AudioContentMainCurationAdapter.ViewHolder>() {

    private val items = mutableListOf<GetAudioContentCurationResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemAudioContentMainCurationBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentCurationResponse) {
            binding.tvTitle.text = item.title
            binding.tvDesc.text = item.description
            binding.ivAll.setOnClickListener { onClickCurationMore(item.curationId, item.title) }
            setAudioContentList(item.audioContents)
        }

        private fun setAudioContentList(audioContents: List<GetAudioContentMainItem>) {
            val adapter = AudioContentMainContentAdapter(onClickItem, onClickCreator)

            binding.rvCuration.layoutManager = LinearLayoutManager(
                context,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            if (binding.rvCuration.itemDecorationCount == 0) {
                binding.rvCuration.addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
                        super.getItemOffsets(outRect, view, parent, state)

                        when (parent.getChildAdapterPosition(view)) {
                            0 -> {
                                outRect.left = 0
                                outRect.right = 6.7f.dpToPx().toInt()
                            }

                            adapter.itemCount - 1 -> {
                                outRect.left = 6.7f.dpToPx().toInt()
                                outRect.right = 0
                            }

                            else -> {
                                outRect.left = 6.7f.dpToPx().toInt()
                                outRect.right = 6.7f.dpToPx().toInt()
                            }
                        }
                    }
                })
            }
            binding.rvCuration.adapter = adapter
            adapter.addItems(audioContents)
        }
    }

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

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

    override fun getItemCount() = items.size

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

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