package ru.dbotthepony.mc.otm.triggers

import com.google.gson.JsonObject
import net.minecraft.advancements.critereon.*
import net.minecraft.advancements.critereon.MinMaxBounds.Doubles
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.core.set

object NanobotsArmorTrigger : SimpleCriterionTrigger<NanobotsArmorTrigger.Instance>() {
	val ID = ResourceLocation(OverdriveThatMatters.MOD_ID, "nanobots_armor")

	override fun getId(): ResourceLocation {
		return ID
	}

	override fun createInstance(
		p_66248_: JsonObject,
		p_286603_: ContextAwarePredicate,
		p_66250_: DeserializationContext
	): Instance {
		return Instance(
			Doubles.fromJson(p_66248_["predicate"])
		)
	}

	fun trigger(player: ServerPlayer, damageAbsorbed: Double) {
		trigger(player) { it.predicate.matches(damageAbsorbed) }
	}

	class Instance(val predicate: Doubles) : AbstractCriterionTriggerInstance(ID, ContextAwarePredicate.ANY) {
		override fun serializeToJson(p_16979_: SerializationContext): JsonObject {
			return super.serializeToJson(p_16979_).also {
				it["predicate"] = predicate.serializeToJson()
			}
		}
	}
}
