package de.teleportsuite;

import de.teleportsuite.commands.*;
import de.teleportsuite.database.DatabaseManager;
import de.teleportsuite.listeners.*;
import de.teleportsuite.managers.*;
import de.teleportsuite.bungeemessaging.BungeeMessenger;
import org.bukkit.plugin.java.JavaPlugin;

public class TeleportSuite extends JavaPlugin {

    private static TeleportSuite instance;
    private DatabaseManager databaseManager;
    private HomeManager homeManager;
    private WarpManager warpManager;
    private PortalManager portalManager;
    private TeleportManager teleportManager;
    private SpawnManager spawnManager;
    private SavePointManager savePointManager;
    private BungeeMessenger bungeeMessenger;
    private ConfigManager configManager;

    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();
        configManager = new ConfigManager(this);

        databaseManager = new DatabaseManager(this);
        if (!databaseManager.connect()) {
            getLogger().severe("Datenbankverbindung fehlgeschlagen! Plugin wird deaktiviert.");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
        databaseManager.createTables();

        homeManager     = new HomeManager(this);
        warpManager     = new WarpManager(this);
        portalManager   = new PortalManager(this);
        teleportManager = new TeleportManager(this);
        spawnManager    = new SpawnManager(this);
        savePointManager = new SavePointManager(this);

        if (configManager.isBungeeEnabled()) {
            bungeeMessenger = new BungeeMessenger(this);
            bungeeMessenger.register();
            getLogger().info("BungeeCord-Unterstuetzung aktiviert.");
        }

        getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerMoveListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerRespawnListener(this), this);
        getServer().getPluginManager().registerEvents(new PortalListener(this), this);

        registerCommands();
        getLogger().info("TeleportSuite v" + getDescription().getVersion() + " erfolgreich geladen!");
    }

    @Override
    public void onDisable() {
        if (databaseManager != null) databaseManager.disconnect();
        if (bungeeMessenger != null) bungeeMessenger.unregister();
    }

    private void registerCommands() {
        getCommand("tp").setExecutor(new TpCommand(this));
        getCommand("tphere").setExecutor(new TpHereCommand(this));
        getCommand("tpa").setExecutor(new TpaCommand(this));
        getCommand("tpaccept").setExecutor(new TpAcceptCommand(this));
        getCommand("tpdeny").setExecutor(new TpDenyCommand(this));
        getCommand("tppos").setExecutor(new TpPosCommand(this));
        getCommand("tpall").setExecutor(new TpAllCommand(this));
        getCommand("tpworld").setExecutor(new TpWorldCommand(this));
        getCommand("back").setExecutor(new BackCommand(this));
        getCommand("deathback").setExecutor(new DeathBackCommand(this));
        getCommand("sethome").setExecutor(new SetHomeCommand(this));
        getCommand("home").setExecutor(new HomeCommand(this));
        getCommand("delhome").setExecutor(new DelHomeCommand(this));
        getCommand("homes").setExecutor(new HomesCommand(this));
        getCommand("setwarp").setExecutor(new SetWarpCommand(this));
        getCommand("warp").setExecutor(new WarpCommand(this));
        getCommand("delwarp").setExecutor(new DelWarpCommand(this));
        getCommand("warps").setExecutor(new WarpsCommand(this));
        getCommand("setportal").setExecutor(new SetPortalCommand(this));
        getCommand("delportal").setExecutor(new DelPortalCommand(this));
        getCommand("portals").setExecutor(new PortalsCommand(this));
        getCommand("setspawn").setExecutor(new SetSpawnCommand(this));
        getCommand("spawn").setExecutor(new SpawnCommand(this));
        getCommand("setfirstspawn").setExecutor(new SetFirstSpawnCommand(this));
        getCommand("setsavepoint").setExecutor(new SetSavePointCommand(this));
        getCommand("savepoint").setExecutor(new SavePointCommand(this));
        getCommand("entitytransport").setExecutor(new EntityTransportCommand(this));
    }

    public static TeleportSuite getInstance() { return instance; }
    public DatabaseManager getDatabaseManager() { return databaseManager; }
    public HomeManager getHomeManager() { return homeManager; }
    public WarpManager getWarpManager() { return warpManager; }
    public PortalManager getPortalManager() { return portalManager; }
    public TeleportManager getTeleportManager() { return teleportManager; }
    public SpawnManager getSpawnManager() { return spawnManager; }
    public SavePointManager getSavePointManager() { return savePointManager; }
    public BungeeMessenger getBungeeMessenger() { return bungeeMessenger; }
    public ConfigManager getConfigManager() { return configManager; }
}
