package ru.dbotthepony.mc.otm.storage.optics

import it.unimi.dsi.fastutil.HashCommon
import ru.dbotthepony.mc.otm.storage.IStorageEventConsumer
import ru.dbotthepony.mc.otm.storage.IStorageProvider
import ru.dbotthepony.mc.otm.storage.StorageStack
import java.util.*

class ListenerProxy<T : StorageStack<T>>(private val consumer: IStorageEventConsumer<T>, private val provider: IStorageProvider<T>) : IStorageEventConsumer<T> by consumer {
	init {
		require(consumer.storageType == provider.storageType) { "Consumer storage type does not match provider's: ${consumer.storageType} != ${provider.storageType}" }
	}

	override fun onStackAdded(stack: T, id: UUID, provider: IStorageProvider<T>) {
		consumer.onStackAdded(stack, id, this.provider)
	}

	override fun toString(): String {
		return "ListenerProxy[$consumer from $provider]"
	}

	override fun equals(other: Any?): Boolean {
		return other is ListenerProxy<*> && consumer == other.consumer && provider == other.provider
	}

	override fun hashCode(): Int {
		return HashCommon.mix(consumer.hashCode() * 31 + provider.hashCode())
	}
}
