package ru.dbotthepony.mc.otm.android

import net.minecraft.core.HolderLookup
import net.minecraft.nbt.CompoundTag
import net.neoforged.neoforge.common.util.INBTSerializable
import net.neoforged.neoforge.event.entity.living.LivingIncomingDamageEvent
import ru.dbotthepony.mc.otm.network.syncher.SynchableGroup
import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.mc.otm.capability.MatteryPlayer
import ru.dbotthepony.mc.otm.core.nbt.set

abstract class AndroidFeature(val type: AndroidFeatureType<*>, val android: MatteryPlayer) : INBTSerializable<CompoundTag> {
	val ply get() = android.ply
	val syncher = SynchableGroup()
	val syncherRemote = syncher.Remote()

	open var level by syncher.int(setter = setter@{ field, value ->
		if (value != field.get()) {
			field.accept(value)
			applyModifiers()
		}
	})

	open fun tickClient() {}
	open fun tickServer() {}

	/**
	 * Called when it is required to network everything again
	 */
	open fun invalidateNetwork() {
		syncherRemote.invalidate()
	}

	open fun applyModifiers() {}
	open fun removeModifiers() {}

	open fun onHurt(event: LivingIncomingDamageEvent) {}

	override fun serializeNBT(registry: HolderLookup.Provider): CompoundTag {
		return CompoundTag().also {
			it["level"] = level
		}
	}

	override fun deserializeNBT(registry: HolderLookup.Provider, nbt: CompoundTag) {
		level = nbt.getInt("level")
	}
}

class DummyAndroidFeature(type: AndroidFeatureType<*>, android: MatteryPlayer) : AndroidFeature(type, android) {
	override fun serializeNBT(registry: HolderLookup.Provider): CompoundTag {
		return CompoundTag()
	}

	override fun deserializeNBT(registry: HolderLookup.Provider, nbt: CompoundTag) {

	}
}

