package ru.dbotthepony.mc.otm.menu.input

import ru.dbotthepony.mc.otm.core.GetterSetter
import ru.dbotthepony.mc.otm.core.util.EnumValueCodec
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import kotlin.reflect.KMutableProperty0

inline fun <reified E : Enum<E>> EnumInputWithFeedback(menu: MatteryMenu) = EnumInputWithFeedback(menu, E::class.java)
inline fun <reified E : Enum<E>> EnumInputWithFeedback(menu: MatteryMenu, state: KMutableProperty0<E>?) = EnumInputWithFeedback(menu, E::class.java, state)
inline fun <reified E : Enum<E>> EnumInputWithFeedback(menu: MatteryMenu, state: GetterSetter<E>) = EnumInputWithFeedback(menu, E::class.java, state)

class EnumInputWithFeedback<E : Enum<E>>(menu: MatteryMenu, clazz: Class<E>) : AbstractPlayerInputWithFeedback<E>() {
	val codec = EnumValueCodec(clazz)
	private val default = codec.values.first()

	override val input = menu.PlayerInput(codec, false) { consumer?.invoke(it) }
	override val value by menu.mSynchronizer.ComputedField(getter = { supplier?.invoke() ?: default }, codec)

	constructor(menu: MatteryMenu, clazz: Class<E>, state: KMutableProperty0<E>?) : this(menu, clazz) {
		if (state != null) {
			with(state)
		}
	}

	constructor(menu: MatteryMenu, clazz: Class<E>, state: GetterSetter<E>) : this(menu, clazz) {
		with(state)
	}
}
