package net.pokeranalytics.android.ui.view

import androidx.annotation.Nullable
import androidx.recyclerview.widget.DiffUtil
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable

class HistorySessionDiffCallback(var newRows: List<RowRepresentable>, var oldRows: List<RowRepresentable>) :
	DiffUtil.Callback() {

	override fun getOldListSize(): Int {
		return oldRows.size
	}

	override fun getNewListSize(): Int {
		return newRows.size
	}

	override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {

		if (oldRows[oldItemPosition] is Session && newRows[newItemPosition] is Session) {
			val session1 = oldRows[oldItemPosition] as Session
			val session2 = newRows[newItemPosition] as Session
			return session1.id == session2.id
		} else if (oldRows[oldItemPosition] is CustomizableRowRepresentable && newRows[newItemPosition] is CustomizableRowRepresentable) {
			val header1 = oldRows[oldItemPosition] as CustomizableRowRepresentable
			val header2 = newRows[newItemPosition] as CustomizableRowRepresentable
			return header1.title == header2.title
		}

		return false
	}

	override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
		// Force to update all the rows that were already there

		if (oldRows[oldItemPosition] is Session && newRows[newItemPosition] is Session) {
			return false
		} else if (oldRows[oldItemPosition] is CustomizableRowRepresentable && newRows[newItemPosition] is CustomizableRowRepresentable) {
			val header1 = oldRows[oldItemPosition] as CustomizableRowRepresentable
			val header2 = newRows[newItemPosition] as CustomizableRowRepresentable
			return header1.title == header2.title
		}

		return false
	}

	@Nullable
	override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
		//you can return particular field for changed item.
		return super.getChangePayload(oldItemPosition, newItemPosition)
	}
}