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

import de.steamwar.core.Core;
import de.steamwar.entity.REntityServer;
import de.steamwar.linkage.AbstractLinker;
import de.steamwar.linkage.SpigotLinker;
import de.steamwar.lobby.listener.AlphaWall;
import de.steamwar.lobby.map.CustomMap;
import de.steamwar.message.Message;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class LobbySystem extends JavaPlugin {

    private static Message message;
    private static LobbySystem plugin;
    private static Config config;
    private static REntityServer entityServer;
    private static REntityServer debugEntityServer;

    private static SpigotLinker spigotLinker;

    @Override
    public void onLoad() {
        plugin = this;
    }

    @Override
    public void onEnable() {
        message = new Message("de.steamwar.lobby.LobbySystem", getClassLoader());
        entityServer = new REntityServer();
        debugEntityServer = new REntityServer();
        Core.setServerName("Lobby");

        CustomMap.init();
        Fightserver.init();

        config = new Config(getConfig());
        spigotLinker = new SpigotLinker(this, message);
        try {
            spigotLinker.link();
        } catch (AbstractLinker.LinkException e) {
            e.printStackTrace();
            Bukkit.shutdown();
            return;
        }

        // EggHunt.init();

        new AlphaWall(l -> l.getX() > 999, AlphaWall.REFLECT_X);
        new AlphaWall(l -> l.getX() < 2977, AlphaWall.REFLECT_X);
        new AlphaWall(l -> l.getZ() > 892, AlphaWall.REFLECT_Z);
        new AlphaWall(l -> l.getZ() < 1794, AlphaWall.REFLECT_Z);
    }

    @Override
    public void onDisable() {
        spigotLinker.unlink();
    }

    public static LobbySystem getInstance() {
        return plugin;
    }

    public static Config config() {
        return config;
    }

    public static Message getMessage() {
        return message;
    }

    public static REntityServer getEntityServer(boolean debug) {
        return debug ? debugEntityServer : entityServer;
    }
}
