/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  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.storage;

import de.steamwar.bausystem.features.simulator.data.Simulator;
import lombok.experimental.UtilityClass;
import yapion.hierarchy.output.FileOutput;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;

import java.io.File;
import java.io.IOException;

@UtilityClass
public class SimulatorSaver {

    public void saveSimulator(File directory, Simulator simulator) {
        if (!directory.exists()) {
            directory.mkdirs();
        }

        YAPIONObject simulatorObject = new YAPIONObject();
        simulatorObject.add("material", simulator.getMaterial().name());
        simulatorObject.add("autoTrace", simulator.isAutoTrace());

        YAPIONArray groups = new YAPIONArray();
        simulator.getGroups().forEach(group -> {
            YAPIONObject groupObject = new YAPIONObject();
            groupObject.add("material", group.getMaterial().name());
            groupObject.add("disabled", group.isDisabled());

            YAPIONArray elements = new YAPIONArray();
            group.getElements().forEach(element -> {
                YAPIONObject elementObject = new YAPIONObject();
                elementObject.add("type", element.getType());
                elementObject.add("material", element.getMaterial().name());
                elementObject.add("disabled", element.isDisabled());
                elementObject.add("x", element.getPosition().getX());
                elementObject.add("y", element.getPosition().getY());
                elementObject.add("z", element.getPosition().getZ());
                element.saveExtra(elementObject);

                YAPIONArray phases = new YAPIONArray();
                element.getPhases().forEach(phase -> {
                    YAPIONObject phaseObject = new YAPIONObject();
                    phaseObject.add("tickOffset", phase.getTickOffset());
                    phaseObject.add("lifetime", phase.getLifetime());
                    phaseObject.add("order", phase.getOrder());
                    phase.saveExtra(phaseObject);
                    phases.add(phaseObject);
                });
                elementObject.add("phases", phases);
                elements.add(elementObject);
            });
            groupObject.add("elements", elements);
            groups.add(groupObject);
        });
        simulatorObject.add("groups", groups);

        File file = new File(directory, simulator.getName() + ".sim");
        try {
            simulatorObject.toJSONLossy(new FileOutput(file)).close();
        } catch (IOException e) {
            // Ignore
        }
    }
}
