package kr.co.vividnext.sodalive.explorer

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
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.databinding.ItemExplorerBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class ExplorerAdapter(
    private val onClickItem: (Long) -> Unit
) : RecyclerView.Adapter<ExplorerAdapter.ViewHolder>() {

    private val items = mutableListOf<GetExplorerSectionResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemExplorerBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetExplorerSectionResponse) {
            setTitle(item)
            setDesc(item)
            setCreatorList(item)
        }

        private fun setTitle(item: GetExplorerSectionResponse) {
            binding.tvTitle.text = if (
                !item.coloredTitle.isNullOrBlank() &&
                !item.color.isNullOrBlank()
            ) {
                val spStr = SpannableString(item.title)

                try {
                    spStr.setSpan(
                        ForegroundColorSpan(
                            Color.parseColor("#${item.color}")
                        ),
                        item.title.indexOf(item.coloredTitle),
                        item.title.indexOf(item.coloredTitle) + item.coloredTitle.length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    )

                    spStr
                } catch (e: IllegalArgumentException) {
                    item.title
                }
            } else {
                item.title
            }
        }

        private fun setDesc(item: GetExplorerSectionResponse) {
            if (item.desc != null) {
                binding.llDesc.visibility = View.VISIBLE
                binding.tvDesc.text = item.desc
            } else {
                binding.llDesc.visibility = View.GONE
            }
        }

        private fun setCreatorList(item: GetExplorerSectionResponse) {
            val adapter = ExplorerSectionAdapter(
                onClickItem = onClickItem,
                isVisibleRanking = item.desc != null
            )

            binding.rvExplorerSection.layoutManager = LinearLayoutManager(
                context,
                LinearLayoutManager.HORIZONTAL,
                false
            )

            binding.rvExplorerSection.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.rvExplorerSection.adapter = adapter
            adapter.addItems(item.creators)
        }
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]

        if (item.creators.isNotEmpty()) {
            holder.bind(items[position])
        }
    }

    override fun getItemCount() = items.size

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