package net.mcreator.phayriosisreborn.meteor;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.saveddata.SavedData;

/** Persistent runtime state for the live meteor event. */
public final class PhayriosisMeteorEventData extends SavedData {
    public static final String DATA_NAME = "phayriosisreborn_meteor_event";
    public static final double CURRENT_SCHEMA = 3.0D;

    public double schemaVersion = CURRENT_SCHEMA;
    public boolean initialized = false;
    public double nextCheckGameTime = 0.0D;
    public double lastCheckGameTime = -1.0D;
    public double lastEligibleGameTime = -1.0D;
    public double eligibilityChecks = 0.0D;
    public double rollAttempts = 0.0D;
    public double successfulRolls = 0.0D;
    public boolean pendingMeteor = false;
    public double pendingSinceGameTime = -1.0D;

    // Phase 5 runtime flight state. Kept separate from configuration.
    public boolean activeMeteor = false;
    public double activeSinceGameTime = -1.0D;
    public double launchAttempts = 0.0D;
    public double successfulLaunches = 0.0D;
    public double impacts = 0.0D;
    public double lastLaunchGameTime = -1.0D;
    public double lastImpactGameTime = -1.0D;
    public double lastTargetX = 0.0D;
    public double lastTargetY = 0.0D;
    public double lastTargetZ = 0.0D;
    public double nextLaunchAttemptGameTime = 0.0D;

    public PhayriosisMeteorEventData() {}

    public static PhayriosisMeteorEventData load(CompoundTag tag) {
        PhayriosisMeteorEventData data = new PhayriosisMeteorEventData();
        data.read(tag);
        PhayriosisMeteorOneShotGate.onStateLoaded(data);
        return data;
    }

    public void read(CompoundTag tag) {
        if (tag.m_128441_("schemaVersion")) schemaVersion = tag.m_128459_("schemaVersion");
        if (tag.m_128441_("initialized")) initialized = tag.m_128471_("initialized");
        if (tag.m_128441_("nextCheckGameTime")) nextCheckGameTime = tag.m_128459_("nextCheckGameTime");
        if (tag.m_128441_("lastCheckGameTime")) lastCheckGameTime = tag.m_128459_("lastCheckGameTime");
        if (tag.m_128441_("lastEligibleGameTime")) lastEligibleGameTime = tag.m_128459_("lastEligibleGameTime");
        if (tag.m_128441_("eligibilityChecks")) eligibilityChecks = tag.m_128459_("eligibilityChecks");
        if (tag.m_128441_("rollAttempts")) rollAttempts = tag.m_128459_("rollAttempts");
        if (tag.m_128441_("successfulRolls")) successfulRolls = tag.m_128459_("successfulRolls");
        if (tag.m_128441_("pendingMeteor")) pendingMeteor = tag.m_128471_("pendingMeteor");
        if (tag.m_128441_("pendingSinceGameTime")) pendingSinceGameTime = tag.m_128459_("pendingSinceGameTime");

        if (tag.m_128441_("activeMeteor")) activeMeteor = tag.m_128471_("activeMeteor");
        if (tag.m_128441_("activeSinceGameTime")) activeSinceGameTime = tag.m_128459_("activeSinceGameTime");
        if (tag.m_128441_("launchAttempts")) launchAttempts = tag.m_128459_("launchAttempts");
        if (tag.m_128441_("successfulLaunches")) successfulLaunches = tag.m_128459_("successfulLaunches");
        if (tag.m_128441_("impacts")) impacts = tag.m_128459_("impacts");
        if (tag.m_128441_("lastLaunchGameTime")) lastLaunchGameTime = tag.m_128459_("lastLaunchGameTime");
        if (tag.m_128441_("lastImpactGameTime")) lastImpactGameTime = tag.m_128459_("lastImpactGameTime");
        if (tag.m_128441_("lastTargetX")) lastTargetX = tag.m_128459_("lastTargetX");
        if (tag.m_128441_("lastTargetY")) lastTargetY = tag.m_128459_("lastTargetY");
        if (tag.m_128441_("lastTargetZ")) lastTargetZ = tag.m_128459_("lastTargetZ");
        if (tag.m_128441_("nextLaunchAttemptGameTime")) nextLaunchAttemptGameTime = tag.m_128459_("nextLaunchAttemptGameTime");
    }

    @Override
    public CompoundTag m_7176_(CompoundTag tag) {
        tag.m_128347_("schemaVersion", CURRENT_SCHEMA);
        tag.m_128379_("initialized", initialized);
        tag.m_128347_("nextCheckGameTime", nextCheckGameTime);
        tag.m_128347_("lastCheckGameTime", lastCheckGameTime);
        tag.m_128347_("lastEligibleGameTime", lastEligibleGameTime);
        tag.m_128347_("eligibilityChecks", eligibilityChecks);
        tag.m_128347_("rollAttempts", rollAttempts);
        tag.m_128347_("successfulRolls", successfulRolls);
        tag.m_128379_("pendingMeteor", pendingMeteor);
        tag.m_128347_("pendingSinceGameTime", pendingSinceGameTime);

        tag.m_128379_("activeMeteor", activeMeteor);
        tag.m_128347_("activeSinceGameTime", activeSinceGameTime);
        tag.m_128347_("launchAttempts", launchAttempts);
        tag.m_128347_("successfulLaunches", successfulLaunches);
        tag.m_128347_("impacts", impacts);
        tag.m_128347_("lastLaunchGameTime", lastLaunchGameTime);
        tag.m_128347_("lastImpactGameTime", lastImpactGameTime);
        tag.m_128347_("lastTargetX", lastTargetX);
        tag.m_128347_("lastTargetY", lastTargetY);
        tag.m_128347_("lastTargetZ", lastTargetZ);
        tag.m_128347_("nextLaunchAttemptGameTime", nextLaunchAttemptGameTime);
        return tag;
    }

    public static PhayriosisMeteorEventData get(ServerLevel level) {
        return level.m_8895_().m_164861_(PhayriosisMeteorEventData::load, PhayriosisMeteorEventData::new, DATA_NAME);
    }
}
