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

import de.steamwar.core.Core;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SQLWrapperImpl implements SQLWrapper {

    @Override
    public void loadSchemTypes(List<SchematicType> tmpTypes, Map<String, SchematicType> tmpFromDB) {
        File folder = new File(Core.getInstance().getDataFolder().getParentFile(), "FightSystem");
        if(folder.exists()) {
            for(File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) {
                YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
                if(!config.isConfigurationSection("Schematic"))
                    continue;

                String type = config.getString("Schematic.Type");
                assert type != null;
                String shortcut = config.getString("Schematic.Shortcut");
                if(tmpFromDB.containsKey(type.toLowerCase()))
                    continue;

                SchematicType checktype = null;
                String material = config.getString("Schematic.Material", "STONE_BUTTON");

                if(!config.getStringList("CheckQuestions").isEmpty()) {
                    checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true);
                    tmpTypes.add(checktype);
                    tmpFromDB.put(checktype.toDB(), checktype);
                }

                boolean manualCheck = config.getBoolean("Schematic.ManualCheck", true);

                SchematicType current = new SchematicType(type, shortcut, config.isConfigurationSection("Server") ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, manualCheck);
                tmpTypes.add(current);
                tmpFromDB.put(type.toLowerCase(), current);
            }
        }
    }

    private static final String SERVER_VERSION = Bukkit.getServer().getVersion();

    @Override
    public void additionalExceptionMetadata(StringBuilder builder) {
        builder.append("\nPlayers: ");
        for(Player player : Bukkit.getOnlinePlayers())
            builder.append(player.getName()).append(" ");
        builder.append("\nWorlds: ");
        for(World world : Bukkit.getWorlds())
            builder.append(world.getName()).append(" ");
        builder.append("\nServer: ").append(SERVER_VERSION);
    }
}
