package com.spaceshipproject;

import org.bukkit.plugin.java.JavaPlugin;

/**
 * Classe principale du plugin SpaceShipProject.
 *
 * Concept :
 *  - Chaque joueur peut posseder plusieurs spaceships personnels.
 *  - Chaque spaceship a sa propre telecommande (boussole).
 *  - Le spaceship est charge dans la meme map que le joueur, en plein ciel.
 *  - Seul le proprietaire peut le modifier.
 *  - Toutes les modifications sont sauvegardees en base au moment du dechargement.
 */
public class SpaceShipProject extends JavaPlugin {

    private ConfigManager configManager;
    private DatabaseManager databaseManager;
    private RemoteManager remoteManager;
    private SpaceshipManager spaceshipManager;
    private ModelManager modelManager;
    private EconomyHook economy;

    @Override
    public void onEnable() {
        getLogger().info("SpaceShipProject : demarrage...");

        saveDefaultConfig();
        configManager = new ConfigManager(this);

        databaseManager = new DatabaseManager(this);
        databaseManager.initialize();

        economy = new EconomyHook(this);
        economy.setup();

        remoteManager = new RemoteManager(this);
        modelManager = new ModelManager(this);
        spaceshipManager = new SpaceshipManager(this);

        getServer().getPluginManager().registerEvents(new RemoteListener(this), this);
        getServer().getPluginManager().registerEvents(new BlockProtectionListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
        SpaceshipGUIListener.register(this);

        SpaceShipCommand cmd = new SpaceShipCommand(this);
        if (getCommand("spaceship") != null) {
            getCommand("spaceship").setExecutor(cmd);
            getCommand("spaceship").setTabCompleter(cmd);
        }
        SpaceShipAdminCommand adminCmd = new SpaceShipAdminCommand(this);
        if (getCommand("spaceshipadmin") != null) {
            getCommand("spaceshipadmin").setExecutor(adminCmd);
            getCommand("spaceshipadmin").setTabCompleter(adminCmd);
        }

        getLogger().info("SpaceShipProject est actif !");
    }

    @Override
    public void onDisable() {
        if (modelManager != null) {
            try {
                modelManager.unloadAllOnShutdown();
            } catch (Exception ex) {
                getLogger().warning("Erreur shutdown ModelManager : " + ex.getMessage());
            }
        }
        if (spaceshipManager != null) {
            try {
                spaceshipManager.unloadAllOnShutdown();
            } catch (Exception ex) {
                getLogger().warning("Erreur lors du dechargement global : " + ex.getMessage());
            }
        }
        if (databaseManager != null) {
            databaseManager.close();
        }
        getLogger().info("SpaceShipProject est arrete.");
    }

    public ConfigManager getConfigManager() { return configManager; }
    public DatabaseManager getDatabaseManager() { return databaseManager; }
    public RemoteManager getRemoteManager() { return remoteManager; }
    public SpaceshipManager getSpaceshipManager() { return spaceshipManager; }
    public ModelManager getModelManager() { return modelManager; }
    public EconomyHook getEconomy() { return economy; }
}
