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

import de.steamwar.sql.SteamwarUser;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import yapion.hierarchy.output.FileOutput;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.parser.InputStreamCharsets;
import yapion.parser.YAPIONParser;
import yapion.parser.options.FileOptions;

import java.io.File;
import java.util.List;

@UtilityClass
public class SimulatorData {

    private final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators");

    static {
        simulatorsDir.mkdirs();
    }

    @SneakyThrows
    public YAPIONObject getSimulator(Player player) {
        File file = getFile(player);
        if (!file.exists()) {
            return new YAPIONObject();
        }
        return YAPIONParser.parse(file, new FileOptions().charset(InputStreamCharsets.UTF_8));
    }

    @SneakyThrows
    private void internalSaveSimulator(Player player, YAPIONObject yapionObject) {
        File file = getFile(player);
        yapionObject.toYAPION(new FileOutput(file)).close();
    }

    @SneakyThrows
    void saveSimulator(SteamwarUser steamwarUser, YAPIONObject yapionObject) {
        File file = new File(simulatorsDir, steamwarUser.getId() + ".yapion");
        yapionObject.toYAPION(new FileOutput(file)).close();
    }

    public List<String> listSimulator(Player player) {
        List<String> strings = getSimulator(player).getKeys();
        strings.remove("");
        return strings;
    }

    public void saveSimulator(Player player, String name) {
        YAPIONObject yapionObject = getSimulator(player);
        yapionObject.put(name, yapionObject.getArray("").copy());
        internalSaveSimulator(player, yapionObject);
    }

    public void saveSimulator(Player player, YAPIONArray simulator) {
        YAPIONObject yapionObject = getSimulator(player);
        yapionObject.put("", simulator);
        internalSaveSimulator(player, yapionObject);
    }

    public void loadSimulator(Player player, String name) {
        YAPIONObject yapionObject = getSimulator(player);
        yapionObject.put("", yapionObject.getArray(name).copy());
        internalSaveSimulator(player, yapionObject);
    }

    public void removeSimulator(Player player, String name) {
        YAPIONObject yapionObject = getSimulator(player);
        yapionObject.remove(name);
        internalSaveSimulator(player, yapionObject);
    }

    private File getFile(Player player) {
        SteamwarUser steamwarUser = SteamwarUser.get(player.getUniqueId());
        return new File(simulatorsDir, steamwarUser.getId() + ".yapion");
    }
}
