/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2013-2016, Max Roncace <me@caseif.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.caseif.ttt;

import static net.caseif.ttt.util.Constants.MIN_FLINT_VERSION;
import static net.caseif.ttt.util.Constants.STEEL_CURSEFORGE_PROJECT_ID;
import static net.caseif.ttt.util.Constants.TTT_CURSEFORGE_PROJECT_ID;

import net.caseif.ttt.command.SpecialCommandManager;
import net.caseif.ttt.listeners.SpecialPlayerListener;
import net.caseif.ttt.util.FreshUpdater;

import net.caseif.rosetta.LocaleManager;
import net.gravitydevelopment.updater.Updater;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics;

import java.io.File;
import java.io.IOException;

public class TTTBootstrap extends JavaPlugin {

    public static TTTBootstrap INSTANCE;

    public static LocaleManager locale;
    public static boolean STEEL = true;

    public TTTBootstrap() {
        INSTANCE = this;
    }

    @Override
    public void onEnable() {
        STEEL = Bukkit.getPluginManager().isPluginEnabled("Steel");
        locale = new LocaleManager(this);
        initializeUpdater();
        initializeMetrics();
        if (!STEEL) {
            fail();
            return;
        }
        new TTTCore(this, locale).initialize();
    }

    @Override
    public void onDisable() {
        if (STEEL) {
            TTTCore.getInstance().deinitialize();
        }
    }

    @Override
    public File getFile() {
        return super.getFile();
    }

    public void fail() {
        locale.setDefaultLocale(getConfig().getString("locale"));
        getLogger().warning(locale.getLocalizable("error.plugin.flint")
                .withReplacements(MIN_FLINT_VERSION + "").localize());
        Bukkit.getPluginManager().registerEvents(new SpecialPlayerListener(), this);
        getCommand("ttt").setExecutor(new SpecialCommandManager());
    }

    private void initializeMetrics() {
        if (getConfig().getBoolean("enable-metrics")) {
            try {
                Metrics metrics = new Metrics(this);
                Metrics.Graph graph = metrics.createGraph("Steel Version");
                graph.addPlotter(new Metrics.Plotter(
                        Bukkit.getPluginManager().getPlugin("Steel").getDescription().getVersion()
                ) {
                    public int getValue() {
                        return 1;
                    }
                });
                metrics.addGraph(graph);
                metrics.start();
            } catch (IOException ex) {
                if (getConfig().getBoolean("verbose-logging")) {
                    getLogger().warning(locale.getLocalizable("error.plugin.mcstats").localize());
                }
            }
        }
    }

    private void initializeUpdater() {
        if (getConfig().getBoolean("enable-auto-update")) {
            new Updater(this, TTT_CURSEFORGE_PROJECT_ID, getFile(), Updater.UpdateType.DEFAULT,
                    new TTTUpdateCallback(), true);

            if (!STEEL) {
                new FreshUpdater(this, STEEL_CURSEFORGE_PROJECT_ID,
                        new File(getDataFolder().getParentFile(), "Steel.jar"), Updater.UpdateType.NO_VERSION_CHECK,
                        new SteelUpdateCallback(), true);
            }
        }
    }

    private class SteelUpdateCallback implements Updater.UpdateCallback {
        @Override
        public void onFinish(Updater updater) {
            if (updater.getResult() == Updater.UpdateResult.SUCCESS) {
                getLogger().info(locale.getLocalizable("info.plugin.installed-steel").localize());
            } else if (updater.getResult() == Updater.UpdateResult.FAIL_APIKEY
                    || updater.getResult() == Updater.UpdateResult.FAIL_BADID
                    || updater.getResult() == Updater.UpdateResult.FAIL_DBO
                    || updater.getResult() == Updater.UpdateResult.FAIL_DOWNLOAD
                    || updater.getResult() == Updater.UpdateResult.FAIL_NOVERSION) {
                getLogger().info(locale.getLocalizable("error.plugin.update-fail").localize());
            }
        }
    }

    private class TTTUpdateCallback implements Updater.UpdateCallback {
        @Override
        public void onFinish(Updater updater) {
            if (updater.getResult() == Updater.UpdateResult.SUCCESS) {
                getLogger().info(locale.getLocalizable("info.plugin.updated").localize());
            } else if (updater.getResult() == Updater.UpdateResult.FAIL_APIKEY
                    || updater.getResult() == Updater.UpdateResult.FAIL_BADID
                    || updater.getResult() == Updater.UpdateResult.FAIL_DBO
                    || updater.getResult() == Updater.UpdateResult.FAIL_DOWNLOAD
                    || updater.getResult() == Updater.UpdateResult.FAIL_NOVERSION) {
                getLogger().info(locale.getLocalizable("error.plugin.update-fail").localize());
            }
        }
    }

}

@@ -3111,32 +3111,65 @@
  Metrics(this);%0A
+                if (STEEL) %7B%0A    
                 
@@ -3224,24 +3224,28 @@
  Version%22);%0A
+    
             
@@ -3302,32 +3302,36 @@
                 
+    
 Bukkit.getPlugin
@@ -3405,20 +3405,28 @@
         
+ 
+   
 ) %7B%0A
+    
         
@@ -3457,24 +3457,28 @@
 etValue() %7B%0A
+    
             
@@ -3511,34 +3511,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -3557,32 +3557,36 @@
                 
+  
+  
 metrics.addGraph
@@ -3594,16 +3594,34 @@
 graph);%0A
+                %7D%0A
         

Fix plugin not gracefully failing without Steel