/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.simulator.data.tnt;

import de.steamwar.bausystem.features.simulator.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator.execute.SimulatorAction;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.FreezeMode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector;
import yapion.hierarchy.types.YAPIONObject;

import java.util.function.BiConsumer;

@Getter
@Setter
@NoArgsConstructor
public final class TNTPhase extends SimulatorPhase {
    private int count = 1;
    private boolean xJump = false;
    private boolean yJump = false;
    private boolean zJump = false;

    public TNTPhase(int tickOffset) {
        this.tickOffset = tickOffset;
    }

    public boolean hasJump() {
        return xJump || yJump || zJump;
    }

    public void setJump(boolean jump) {
        xJump = jump;
        yJump = jump;
        zJump = jump;
    }

    @Override
    public void toSimulatorActions(Vector position, BiConsumer<Integer, SimulatorAction> tickStart, BiConsumer<Integer, SimulatorAction> tickEnd) {
        if (count <= 0) return;
        tickStart.accept(tickOffset, new SimulatorAction(order, count) {
            @Override
            public void accept(World world) {
                Location location = position.toLocation(world);
                if (Region.getRegion(location).getRegionData().get(Flag.FREEZE).isWithDefault(FreezeMode.ACTIVE)) {
                    return;
                }
                TNTPrimed tnt = world.spawn(location, TNTPrimed.class);
                if (!xJump) tnt.setVelocity(tnt.getVelocity().setX(0));
                if (!yJump) tnt.setVelocity(tnt.getVelocity().setY(0));
                if (!zJump) tnt.setVelocity(tnt.getVelocity().setZ(0));
                tnt.setFuseTicks(lifetime);
            }
        });
        tickEnd.accept(tickOffset + lifetime, new SimulatorAction(0, 1) {
            @Override
            public void accept(World world) {
            }
        });
    }

    @Override
    public void saveExtra(YAPIONObject phaseObject) {
        phaseObject.add("count", count);
        phaseObject.add("xJump", xJump);
        phaseObject.add("yJump", yJump);
        phaseObject.add("zJump", zJump);
    }

    @Override
    public void loadExtra(YAPIONObject phaseObject) {
        count = phaseObject.getPlainValue("count");
        xJump = phaseObject.getPlainValue("xJump");
        yJump = phaseObject.getPlainValue("yJump");
        zJump = phaseObject.getPlainValue("zJump");
    }
}
