/*
 *   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 java.util.*

data class SchematicType(
    val name: String,
    val kuerzel: String,
    val type: Type,
    val checkType: SchematicType?,
    val material: String,
    val deadline: Date?,
    val manualCheck: Boolean,
) {
    constructor(
        name: String,
        kuerzel: String,
        type: Type,
        checkType: SchematicType?,
        material: String,
        manualCheck: Boolean
    ) : this(name, kuerzel, type, checkType, material, null, manualCheck)

    companion object {
        @JvmField
        val Normal = SchematicType("Normal", "", Type.NORMAL, null, "STONE_BUTTON", false)

        private val types: MutableList<SchematicType> = mutableListOf()
        private val fromDB: MutableMap<String, SchematicType> = mutableMapOf()

        init {
            GameModeConfig.init()
            init()
        }

        @JvmStatic
        fun init() {
            types.clear()
            fromDB.clear()

            types.add(Normal)
            fromDB[Normal.toDB()] = Normal

            for (gameModeConfig in GameModeConfig.getAll<Any>()) {
                val type = gameModeConfig.Schematic.Type
                    ?: continue
                if (fromDB.containsKey(type.toDB())) continue

                types.add(type)
                fromDB[type.toDB()] = type
                if (gameModeConfig.CheckQuestions.isNotEmpty() && type.checkType != null) {
                    types.add(type.checkType)
                    fromDB[type.checkType.toDB()] = type.checkType
                }
            }
        }

        @JvmStatic
        fun values() =
            types

        @JvmStatic
        fun fromDB(value: String) =
            fromDB[value.lowercase()]
    }

    fun name() =
        name

    fun toDB() =
        name.lowercase()

    fun check() =
        type == Type.CHECK_TYPE

    fun fightType() =
        type == Type.FIGHT_TYPE

    fun writeable() =
        type == Type.NORMAL

    fun checkType() =
        if (manualCheck) checkType else this

    fun isAssignable() =
        type == Type.NORMAL || (type == Type.FIGHT_TYPE && checkType != null) || !manualCheck

    enum class Type {
        NORMAL,
        CHECK_TYPE,
        FIGHT_TYPE
    }
}
