/*
 *   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 com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import de.steamwar.velocitycore.GameModeConfig;
import de.steamwar.velocitycore.VelocityCore;
import de.steamwar.velocitycore.commands.CheckCommand;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class SQLWrapperImpl implements SQLWrapper {
    private static final SimpleDateFormat deadlineFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    private static Date parseDeadline(String deadline) {
        if(deadline == null)
            return null;

        try {
            return deadlineFormat.parse(deadline);
        } catch (ParseException e) {
            throw new SecurityException(e.getMessage(), e);
        }
    }

    @Override
    public void loadSchemTypes(List<SchematicType> tmpTypes, Map<String, SchematicType> tmpFromDB) {
        GameModeConfig.loadAll(GameModeConfig.class, (file, config) -> {
            if(config.getSchematic() == null || tmpFromDB.containsKey(config.getSchemType().toLowerCase()))
                return;

            String shortcut = config.getSchematic().getShortcut();
            String material = config.getSchematic().getMaterial();

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

            SchematicType current = new SchematicType(config.getSchemType(), shortcut, config.getServer() != null ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, parseDeadline(config.getDeadline()), config.getSchematic().isManualCheck());
            tmpTypes.add(current);
            tmpFromDB.put(config.getSchemType().toLowerCase(), current);
            if(checktype != null)
                CheckCommand.addFightType(checktype, current);
        });
    }

    @Override
    public void additionalExceptionMetadata(StringBuilder builder) {
        builder.append("\nServers: ");
        for(RegisteredServer server : VelocityCore.getProxy().getAllServers()) {
            builder.append(server.getServerInfo().getName()).append("(");
            for(Player player : server.getPlayersConnected()) {
                builder.append(player.getUsername()).append(" ");
            }
            builder.append(") ");
        }
    }
}
