/*
 *   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.execute;

import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement;
import de.steamwar.bausystem.features.simulator.data.tnt.TNTPhase;
import de.steamwar.bausystem.features.tracer.TraceRecorder;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

public class StabSetup extends StabStep {

    public StabSetup(StabData data) {
        super(data);
    }

    @Override
    protected void start() {
        TNTPhase tntPhase = data.simulator.getGroups().stream()
                .filter(simulatorGroup -> !simulatorGroup.isDisabled())
                .map(SimulatorGroup::getElements)
                .flatMap(List::stream)
                .filter(TNTElement.class::isInstance)
                .map(TNTElement.class::cast)
                .filter(tntElement -> !tntElement.isDisabled())
                .filter(tntElement -> data.tntElement != tntElement)
                .map(tntElement -> tntElement.getPhases().stream().max(Comparator.comparingInt(TNTPhase::getTickOffset)))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .peek(phase -> {
                    if (phase.getOrder() > TNTPhase.ORDER_LIMIT) {
                        phase.setOrder(TNTPhase.ORDER_LIMIT);
                    }
                })
                .filter(phase -> phase != data.phases.get(0))
                .max(Comparator.comparingInt(TNTPhase::getTickOffset))
                .orElse(null);
        if (tntPhase == null) {
            throw new SecurityException("");
        }

        TNTPhase phase = data.phases.get(0);
        data.phases.clear();
        data.phases.add(phase);
        phase.setCount(1);
        phase.setTickOffset(tntPhase.getTickOffset());
        phase.setOrder(100);

        TraceRecorder.instance.stopRecording(data.region);
        if (TraceRecorder.instance.isAutoTraceEnabledInRegion(data.region)) {
            TraceRecorder.instance.removeAutoTraceRegion(data.region);
        }
        data.clipboard = FlatteningWrapper.impl.copy(data.region.getTestblockArea().getMinPoint(true), data.region.getTestblockArea().getMaxPoint(true), data.region.getTestblockArea().getCopyPoint());

        new StabDirection(data);
    }

    @Override
    protected void bossbar(BauSystemBossbar bossbar, boolean finished) {
        bossbar.setProgress(0);
        bossbar.setTitle("§eSetting up Simulator");
    }
}
