/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024 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 org.bspfsystems.yamlconfiguration.file.YamlConfiguration
import java.io.File
import java.util.*
import java.util.stream.Collectors

fun loadSchematicTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) {
    val folder = File("/configs/GameModes")
    if (folder.exists()) {
        for (configFile in Arrays.stream(folder.listFiles { _, name -> name.endsWith(".yml") && !name.endsWith(".kits.yml") })
            .sorted().collect(Collectors.toList())) {
            val config: YamlConfiguration = YamlConfiguration.loadConfiguration(configFile)
            if (!config.isConfigurationSection("Schematic")) continue
            val type: String = config.getString("Schematic.Type")!!
            val shortcut = config.getString("Schematic.Shortcut")
            if (shortcut == null) {
                println("No shortcut for $type")
                continue
            }
            if (tmpFromDB!!.containsKey(type.lowercase(Locale.getDefault()))) continue
            var checktype: SchematicType? = null
            val material: String = config.getString("Schematic.Material", "STONE_BUTTON")!!
            if (!config.getStringList("CheckQuestions").isEmpty()) {
                checktype = SchematicType("C$type", "C$shortcut", SchematicType.Type.CHECK_TYPE, null, material, false)
                tmpTypes!!.add(checktype)
                tmpFromDB[checktype.toDB()] = checktype
            }
            val current = SchematicType(
                type,
                shortcut,
                if (config.isConfigurationSection("Server")) SchematicType.Type.FIGHT_TYPE else SchematicType.Type.NORMAL,
                checktype,
                material,
                false
            )
            tmpTypes!!.add(current)
            tmpFromDB[type.lowercase(Locale.getDefault())] = current
        }
    }
}

class SQLWrapperImpl: SQLWrapper {
    override fun loadSchemTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) = loadSchematicTypes(tmpTypes, tmpFromDB)

    override fun additionalExceptionMetadata(builder: StringBuilder) {
        builder.append("\n\nWebsiteApi")
    }
}