package com.github.games647.healthname;

import com.github.games647.healthname.config.Config;
import com.github.games647.healthname.config.Settings;
import com.google.inject.Inject;

import java.io.File;

import me.flibio.updatifier.Updatifier;

import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;

import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.scoreboard.Scoreboard;
import org.spongepowered.api.scoreboard.critieria.Criteria;
import org.spongepowered.api.scoreboard.displayslot.DisplaySlots;
import org.spongepowered.api.scoreboard.objective.Objective;
import org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayModes;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

@Updatifier(repoOwner = "games647", repoName = "HealthName", version = "0.2.4")
@Plugin(id = "healthname", name = "HealthName", version = "0.2.4"
        , url = "https://github.com/games647/HealthName"
        , description = "A Sponge minecraft server plugin for displaying the health above an entity.")
public class HealthName {

    private final PluginContainer pluginContainer;
    private final Logger logger;
    private final Game game;

    private Scoreboard globalScoreboard;

    @Inject
    @DefaultConfig(sharedRoot = true)
    private File defaultConfigFile;

    @Inject
    @DefaultConfig(sharedRoot = true)
    private ConfigurationLoader<CommentedConfigurationNode> configManager;

    private Settings configuration;

    @Inject
    public HealthName(Logger logger, PluginContainer pluginContainer, Game game) {
        this.logger = logger;
        this.pluginContainer = pluginContainer;
        this.game = game;
    }

    @Listener //During this state, the plugin gets ready for initialization. Logger and config
    public void onPreInit(GamePreInitializationEvent preInitEvent) {
        logger.info("Loading {} v{}", pluginContainer.getName(), pluginContainer.getVersion());

        configuration = new Settings(configManager, defaultConfigFile, this);
        configuration.load();
    }

    @Listener //During this state, the plugin should finish any work needed in order to be functional. Commands register + events
    public void onInit(GameInitializationEvent initEvent) {
        //register events
        game.getEventManager().registerListeners(this, new DamageListener(this));
        game.getEventManager().registerListeners(this, new ConnectionListener(this));
    }

    @Listener
    public void onGameStarted(GameStartedServerEvent gameStartedServerEvent) {
        globalScoreboard = game.getServer().getServerScoreboard().get();

        //Scoreboards are loaded when the world is loaded so load it here
        if (getConfig().isNametagHealth()) {
            Objective objective = globalScoreboard.getObjective(pluginContainer.getId()).orElse(null);
            if (objective != null) {
                //clear all old values
                globalScoreboard.removeObjective(objective);
            }

            objective = Objective.builder()
                    .name(pluginContainer.getId())
                    .displayName(Text.of(TextColors.DARK_RED, "Health"))
                    .criterion(Criteria.DUMMY)
                    .objectiveDisplayMode(ObjectiveDisplayModes.INTEGER)
                    .build();
            globalScoreboard.addObjective(objective);
            globalScoreboard.updateDisplaySlot(objective, DisplaySlots.BELOW_NAME);
        }
    }

    public Scoreboard getGlobalScoreboard() {
        return globalScoreboard;
    }

    public Settings getConfigManager() {
        return configuration;
    }

    public Config getConfig() {
        return configuration.getConfiguration();
    }

    public PluginContainer getContainer() {
        return pluginContainer;
    }

    public Logger getLogger() {
        return logger;
    }

    public Game getGame() {
        return game;
    }
}

@@ -202,49 +202,8 @@
 e;%0A%0A
-import me.flibio.updatifier.Updatifier;%0A%0A
 impo
@@ -269,16 +269,16 @@
 onNode;%0A
+
 import n
@@ -698,24 +698,95 @@
 erverEvent;%0A
+import org.spongepowered.api.event.game.state.GameStoppingServerEvent;%0A
 import org.s
@@ -1292,88 +1292,8 @@
 s;%0A%0A
-@Updatifier(repoOwner = %22games647%22, repoName = %22HealthName%22, version = %220.2.4%22)%0A
 @Plu
@@ -1351,17 +1351,17 @@
  = %220.2.
-4
+5
 %22%0A      
@@ -3926,32 +3926,297 @@
        %7D%0A    %7D%0A%0A
+    @Listener%0A    public void onGameStarted(GameStoppingServerEvent gameStoppingServerEvent) %7B%0A        if (globalScoreboard != null) %7B%0A            globalScoreboard.getObjective(pluginContainer.getId()).ifPresent(globalScoreboard::removeObjective);%0A        %7D%0A    %7D%0A%0A
     public Score

Remove scoreboard on plugin disable