package com.bearya.kids.section

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.Section
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 SectionPagerAdapter(private val dirName: String?) : PagedListAdapter<Section, SectionPagerViewHolder>(object : DiffUtil.ItemCallback<Section>() {

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

    override fun areContentsTheSame(oldItem: Section, newItem: Section): Boolean =
            TextUtils.equals(oldItem.name, newItem.name) && oldItem.lock == newItem.lock

}) {

    var onItemClickListener: OnItemClickListener<Section>? = null

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

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

    fun getItemData(position:Int) = getItem(position)

}

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

class SectionPreviewAdapter(private val dirName: String?) : PagedListAdapter<Section, SectionPreviewViewHolder>(object : DiffUtil.ItemCallback<Section>() {

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

    override fun areContentsTheSame(oldItem: Section, newItem: Section): Boolean =
            TextUtils.equals(oldItem.name, newItem.name) && oldItem.lock == newItem.lock

}) {

    var onItemClickListener: OnItemClickListener<Section>? = null

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

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

    override fun onBindViewHolder(holder: SectionPreviewViewHolder, 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 SectionPreviewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val bindView: ItemSectionPreviewBinding? = DataBindingUtil.bind(itemView)
}