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

import de.steamwar.bausystem.features.simulator.data.Simulator;
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.sql.SteamwarUser;
import org.bukkit.Material;
import org.bukkit.util.Vector;
import yapion.exceptions.YAPIONException;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.parser.YAPIONParser;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class YAPIONFormatSimulatorLoader implements SimulatorLoader {

    @Override
    public Optional<List<Simulator>> load(File file) {
        if (!file.getName().endsWith(".yapion")) {
            return Optional.empty();
        }

        YAPIONObject yapionObject;
        try {
            yapionObject = YAPIONParser.parse(file);
        } catch (YAPIONException | IOException e) {
            return Optional.empty();
        }

        String name = file.getName().substring(0, file.getName().length() - 7);
        SteamwarUser steamwarUser = SteamwarUser.get(Integer.parseInt(name));

        List<Simulator> simulators = new ArrayList<>();
        for (String s : yapionObject.getKeys()) {
            String newName = steamwarUser.getUserName() + (s.isEmpty() ? "" : "_" + s);
            YAPIONArray content = yapionObject.getArray(s);
            if (content.isEmpty()) continue;
            Simulator simulator = new Simulator(newName);
            for (YAPIONObject element : content.streamObject().collect(Collectors.toList())) {
                SimulatorGroup simulatorGroup = new SimulatorGroup();
                simulator.add(simulatorGroup);

                TNTElement tntElement = new TNTElement(new Vector(element.getDouble("positionX"), element.getDouble("positionY"), element.getDouble("positionZ")));
                tntElement.setDisabled(element.getBooleanOrDefault("disabled", false)); // IDK if this existed back then?
                tntElement.setMaterial(Material.valueOf(element.getStringOrDefault("material", "TNT")));
                simulatorGroup.add(tntElement);

                TNTPhase tntPhase = new TNTPhase();
                tntPhase.setLifetime(element.getIntOrDefault("fuseTicks", 80));
                tntPhase.setCount(element.getIntOrDefault("count", 1));
                tntPhase.setTickOffset(element.getIntOrDefault("tickOffset", 0));
                tntPhase.setXJump(element.getBooleanOrDefault("xVelocity", false));
                tntPhase.setYJump(element.getBooleanOrDefault("yVelocity", false));
                tntPhase.setZJump(element.getBooleanOrDefault("zVelocity", false));
                tntPhase.setOrder(element.getBooleanOrDefault("comparator", false) ? 2 : 0);
                tntElement.add(tntPhase);
            }
            simulators.add(simulator);
        }

        file.delete();
        return Optional.of(simulators);
    }
}
