package ru.dbotthepony.mc.otm.core.util

import java.util.function.BooleanSupplier

/**
 * Represents something that can tick without any additional callback,
 * with sign whenever can it tick more
 */
fun interface IConditionalTickable {
	/**
	 * Function to be called by tick executors
	 *
	 * Once this returns false, ticker is considered finished, and should be removed from ticking list
	 */
	fun tick(): Boolean

	companion object {
		fun wrap(ticker: () -> Boolean): IConditionalTickable = IConditionalTickable { ticker.invoke() }
		fun wrap(ticker: BooleanSupplier): IConditionalTickable = IConditionalTickable { ticker.asBoolean }

		fun wrap(condition: () -> Boolean, ticker: () -> Unit): IConditionalTickable {
			return IConditionalTickable {
				ticker.invoke()
				condition.invoke()
			}
		}

		fun wrap(condition: BooleanSupplier, ticker: () -> Unit): IConditionalTickable {
			return IConditionalTickable {
				ticker.invoke()
				condition.asBoolean
			}
		}
	}
}
