package com.bearya.kids.analysis

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.databinding.DataBindingUtil
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bearya.data.entity.Analysis
import com.bearya.kids.R
import com.bearya.kids.databinding.ItemSectionBinding
import com.bearya.kids.databinding.ItemSectionPreviewBinding
import library.OnItemClickListener
import library.ext.assetsPath

class AnalysisAdapter(private val dirName: String?) : PagedListAdapter<Analysis, AnalysisViewHolder>(object : DiffUtil.ItemCallback<Analysis>() {

    override fun areItemsTheSame(oldItem: Analysis, newItem: Analysis): Boolean = oldItem.id == newItem.id

    override fun areContentsTheSame(oldItem: Analysis, newItem: Analysis): Boolean =
            TextUtils.equals(oldItem.name, newItem.name)

}) {

    var onItemClickListener: OnItemClickListener<Analysis>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnalysisViewHolder = AnalysisViewHolder(
            ItemSectionBinding.inflate(LayoutInflater.from(parent.context), parent, false).root
    )

    override fun onBindViewHolder(holder: AnalysisViewHolder, position: Int) {
        val item = getItem(position)
        holder.bindView?.image = "chapter/${dirName}/${item?.name}.webp".assetsPath()
        holder.itemView.setOnClickListener { onItemClickListener?.invoke(it, item, position) }
    }

}

class AnalysisViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val bindView: ItemSectionBinding? = DataBindingUtil.bind(itemView)
}

class AnalysisPreviewAdapter(private val dirName: String?) : PagedListAdapter<Analysis, AnalysisPreviewViewHolder>(object : DiffUtil.ItemCallback<Analysis>() {

    override fun areItemsTheSame(oldItem: Analysis, newItem: Analysis): Boolean = oldItem.id == newItem.id

    override fun areContentsTheSame(oldItem: Analysis, newItem: Analysis): Boolean =
            TextUtils.equals(oldItem.name, newItem.name)

}) {

    var onItemClickListener: OnItemClickListener<Analysis>? = null

    private var currentFocusItemIndex = 0
    private var recyclerView: RecyclerView? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnalysisPreviewViewHolder = AnalysisPreviewViewHolder(
            ItemSectionPreviewBinding.inflate(LayoutInflater.from(parent.context), parent, false).root
    )

    override fun onBindViewHolder(holder: AnalysisPreviewViewHolder, position: Int) {
        val item = getItem(position)

        val index = position + 1
        holder.bindView?.index = "$index"

        holder.bindView?.image = "chapter/${dirName}/${item?.name}.webp".assetsPath()

        holder.bindView?.cover?.setBorderColor(ResourcesCompat.getColor(holder.itemView.context.resources,
                if (position == currentFocusItemIndex) R.color.colorBlue else R.color.colorWhite, null))

        holder.itemView.setOnClickListener { v -> onItemClickListener?.invoke(v, item, position) }
    }

    fun setSelectedIndex(position: Int) {
        notifyItemChanged(currentFocusItemIndex)
        currentFocusItemIndex = position
        notifyItemChanged(currentFocusItemIndex)
        recyclerView?.smoothScrollToPosition(currentFocusItemIndex)
    }

    override fun onAttachedToRecyclerView(rv: RecyclerView) {
        super.onAttachedToRecyclerView(rv)
        recyclerView = rv
    }

    override fun onDetachedFromRecyclerView(rv: RecyclerView) {
        super.onDetachedFromRecyclerView(rv)
        recyclerView = null
    }

}

class AnalysisPreviewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val bindView: ItemSectionPreviewBinding? = DataBindingUtil.bind(itemView)
}