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

import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.storage.IStorageComponent
import ru.dbotthepony.mc.otm.storage.IStorageEventConsumer
import ru.dbotthepony.mc.otm.storage.StorageStack
import java.math.BigInteger
import java.util.*
import java.util.function.Supplier

class FlowControlComponent<S : StorageStack<S>>(private val parent: IStorageComponent<S>, private val flow: Supplier<FlowDirection>) : IStorageComponent<S> by parent {
	override fun addListener(listener: IStorageEventConsumer<S>): Boolean {
		return parent.addListener(ListenerProxy(listener, this))
	}

	override fun removeListener(listener: IStorageEventConsumer<S>): Boolean {
		return parent.removeListener(ListenerProxy(listener, this))
	}

	override fun insertStack(stack: S, simulate: Boolean): S {
		if (!flow.get().input) {
			return storageType.empty
		}

		return parent.insertStack(stack, simulate)
	}

	override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): S {
		if (!flow.get().output) {
			return storageType.empty
		}

		return parent.extractStack(id, amount, simulate)
	}

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

	override fun hashCode(): Int {
		return parent.hashCode()
	}

	override fun toString(): String {
		return "FlowControlComponent[flow=${flow.get()}, $parent]"
	}
}
