package ru.dbotthepony.mc.otm.config

import net.neoforged.neoforge.common.ModConfigSpec
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.util.Delegate

abstract class ObservedConfigValue<V : Any>(val parent: ModConfigSpec.ConfigValue<String>) : Delegate<V> {
	var rawValue: String by parent
	private var observedValue: String? = null
	private var cachedValue: V? = null

	protected abstract fun fromString(value: String): V?

	/**
	 * value [V] -> newRawValue [String], newValue [V]
	 */
	protected abstract fun toString(value: V): Pair<String, V>

	override fun get(): V {
		if (cachedValue == null || rawValue != observedValue) {
			var deserialized = fromString(rawValue)

			if (deserialized == null) {
				val default = parent.default
				deserialized = fromString(default)

				if (deserialized == null) {
					throw RuntimeException("Deserializer did not accept default value: $default")
				} else {
					LOGGER.error("Config value of <${parent.path.joinToString(".")}>: $rawValue is invalid, replacing with default $default")
				}

				rawValue = default
			}

			cachedValue = deserialized
			observedValue = rawValue
		}

		return cachedValue ?: throw ConcurrentModificationException()
	}

	override fun accept(value: V) {
		if (cachedValue == null || rawValue != observedValue) {
			cachedValue = fromString(rawValue)
			observedValue = rawValue
		}

		if (cachedValue != value) {
			val (newRaw, newValue) = toString(value)

			if (cachedValue != newValue) {
				rawValue = newRaw
				observedValue = rawValue
				cachedValue = newValue
			}
		}
	}

	companion object {
		private val LOGGER = LogManager.getLogger()
	}
}
