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

import com.velocitypowered.api.network.ProtocolVersion;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Getter
@AllArgsConstructor
public enum ServerVersion {
    SPIGOT_8("spigot-1.8.8.jar", 8, ProtocolVersion.MINECRAFT_1_8),
    SPIGOT_9("spigot-1.9.4.jar", 9, ProtocolVersion.MINECRAFT_1_9),
    SPIGOT_10("spigot-1.10.2.jar", 10, ProtocolVersion.MINECRAFT_1_10),
    SPIGOT_12("spigot-1.12.2.jar", 12, ProtocolVersion.MINECRAFT_1_12_2),
    SPIGOT_14("spigot-1.14.4.jar", 14, ProtocolVersion.MINECRAFT_1_14_4),
    SPIGOT_15("spigot-1.15.2.jar", 15, ProtocolVersion.MINECRAFT_1_15_2),

    PAPER_8("paper-1.8.8.jar", 8, ProtocolVersion.MINECRAFT_1_8),
    PAPER_10("paper-1.10.2.jar", 10, ProtocolVersion.MINECRAFT_1_10),
    PAPER_12("paper-1.12.2.jar", 12, ProtocolVersion.MINECRAFT_1_12_2),
    PAPER_18("paper-1.18.2.jar", 15, ProtocolVersion.MINECRAFT_1_18_2),
    PAPER_19("paper-1.19.3.jar", 19, ProtocolVersion.MINECRAFT_1_19_3),
    PAPER_20("paper-1.20.1.jar", 20, ProtocolVersion.MINECRAFT_1_20),
    PAPER_21("paper-1.21.3.jar", 21, ProtocolVersion.MINECRAFT_1_21_2);

    private static final Map<String, ServerVersion> chatMap = new HashMap<>();

    static {
        chatMap.put("21", ServerVersion.PAPER_21);
        chatMap.put("1.21", ServerVersion.PAPER_21);
        chatMap.put("1.21.3", ServerVersion.PAPER_21);

        chatMap.put("20", ServerVersion.PAPER_20);
        chatMap.put("1.20", ServerVersion.PAPER_20);
        chatMap.put("1.20.1", ServerVersion.PAPER_20);
        chatMap.put("as", ServerVersion.PAPER_20);
        chatMap.put("airship", ServerVersion.PAPER_20);
        chatMap.put("wg", ServerVersion.PAPER_20);
        chatMap.put("wargear", ServerVersion.PAPER_20);
        chatMap.put("ws", ServerVersion.PAPER_20);
        chatMap.put("warship", ServerVersion.PAPER_20);
        chatMap.put("mwg", ServerVersion.PAPER_20);
        chatMap.put("miniwargear", ServerVersion.PAPER_20);

        chatMap.put("19", ServerVersion.PAPER_19);
        chatMap.put("1.19", ServerVersion.PAPER_19);
        chatMap.put("1.19.2", ServerVersion.PAPER_19);

        chatMap.put("15", ServerVersion.SPIGOT_15);
        chatMap.put("1.15", ServerVersion.SPIGOT_15);
        chatMap.put("1.15.2", ServerVersion.SPIGOT_15);

        chatMap.put("12", ServerVersion.SPIGOT_12);
        chatMap.put("1.12", ServerVersion.SPIGOT_12);
        chatMap.put("1.12.2", ServerVersion.SPIGOT_12);
    }

    public static ServerVersion get(String chat) {
        return chatMap.get(chat.toLowerCase());
    }

    public static Set<String> chatVersions() {
        return chatMap.keySet();
    }

    private static final Map<Integer, ServerVersion> versionMap = new HashMap<>();

    static {
        for(ServerVersion version : values())
            versionMap.put(version.getVersionSuffix(), version);
    }

    public static ServerVersion get(int version) {
        return versionMap.get(version);
    }

    private final String serverJar;
    private final int versionSuffix;
    private final ProtocolVersion protocolVersion;

    public String getWorldFolder(String base) {
        return base + versionSuffix + "/";
    }

    public File getServerDirectory(String base) {
        return new File(ServerStarter.SERVER_PATH, base + versionSuffix);
    }
}
