package com.gmail.nossr50.util.metrics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.HiddenConfig;
import com.gmail.nossr50.locale.LocaleLoader;

import com.turt2live.metrics.Metrics;
import com.turt2live.metrics.Metrics.Graph;

public class MetricsManager {
    private static boolean setup = false;

    public static void setup() {
        if (setup) {
            return;
        }

        if (Config.getInstance().getStatsTrackingEnabled()) {
            try {
                Metrics metrics = new Metrics(mcMMO.p);

                // Timings Graph
                Graph timingsGraph = metrics.createGraph("Percentage of servers using timings");

                if (mcMMO.p.getServer().getPluginManager().useTimings()) {
                    timingsGraph.addPlotter(new Metrics.Plotter("Enabled") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }
                else {
                    timingsGraph.addPlotter(new Metrics.Plotter("Disabled") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }

                // Donut Version Graph
                Graph versionDonutGraph = metrics.createGraph("Donut Version");

                boolean haveVersionInformation = false;
                boolean isOfficialBuild = false;
                String officialKey = "e14cfacdd442a953343ebd8529138680";

                String version = mcMMO.p.getDescription().getVersion();

                InputStreamReader isr = new InputStreamReader(mcMMO.p.getResource(".jenkins"));
                BufferedReader br = new BufferedReader(isr);
                char[] key = new char[32];
                br.read(key);
                if (officialKey.equals(String.valueOf(key))) {
                    isOfficialBuild = true;
                }

                if (version.contains("-")) {
                    String majorVersion = version.substring(0, version.indexOf("-"));
                    String subVersion = "";
                    if (isOfficialBuild) {
                        int startIndex = version.indexOf("-");
                        if (version.substring(startIndex + 1).contains("-")) {
                            subVersion = version.substring(startIndex, version.indexOf("-", startIndex + 1));
                        } else {
                            subVersion = "-release";
                        }
                    } else {
                        subVersion = "-custom";
                    }

                    version = majorVersion + "~=~" + subVersion;
                    haveVersionInformation = true;
                } else {
                    haveVersionInformation = false;
                }

                if (haveVersionInformation) {
                    versionDonutGraph.addPlotter(new Metrics.Plotter(version) {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }

                // Official v Custom build Graph
                Graph officialGraph = metrics.createGraph("Built by official ci");

                if (isOfficialBuild) {
                    officialGraph.addPlotter(new Metrics.Plotter("Yes") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }
                else {
                    officialGraph.addPlotter(new Metrics.Plotter("No") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }

                // Chunkmeta enabled Graph
                Graph chunkmetaGraph = metrics.createGraph("Uses Chunkmeta");

                if (HiddenConfig.getInstance().getChunkletsEnabled()) {
                    chunkmetaGraph.addPlotter(new Metrics.Plotter("Yes") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }
                else {
                    chunkmetaGraph.addPlotter(new Metrics.Plotter("No") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }

                // Storage method Graph
                Graph storageGraph = metrics.createGraph("Storage method");

                if (Config.getInstance().getUseMySQL()) {
                    storageGraph.addPlotter(new Metrics.Plotter("SQL") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }
                else {
                    storageGraph.addPlotter(new Metrics.Plotter("Flatfile") {
                        @Override
                        public int getValue() {
                            return 1;
                        }
                    });
                }

                // Locale Graph
                Graph localeGraph = metrics.createGraph("Locale");

                localeGraph.addPlotter(new Metrics.Plotter(LocaleLoader.getCurrentLocale().getDisplayLanguage(Locale.US)) {
                    @Override
                    public int getValue() {
                        return 1;
                    }
                });

                // GlobalMultiplier Graph
                Graph globalMultiplierGraph = metrics.createGraph("Global Multiplier Graph");

                globalMultiplierGraph.addPlotter(new Metrics.Plotter(Config.getInstance().getExperienceGainsGlobalMultiplier() + "") {
                    @Override
                    public int getValue() {
                        return 1;
                    }
                });

                // GlobalCurveModifier Graph
                Graph globalCurveModifierGraph = metrics.createGraph("Global Curve Modifier Graph");

                globalCurveModifierGraph.addPlotter(new Metrics.Plotter(Config.getInstance().getFormulaMultiplierCurve() + "") {
                    @Override
                    public int getValue() {
                        return 1;
                    }
                });

                metrics.start();
            }
            catch (IOException e) {
                mcMMO.p.getLogger().warning("Failed to submit stats.");
            }
        }
    }
}

@@ -318,16 +318,55 @@
 oader;%0A%0A
+import com.turt2live.metrics.EMetrics;%0A
 import c
@@ -698,16 +698,17 @@
     
+E
 Metrics 
 metr
@@ -703,16 +703,17 @@
 Metrics 
+e
 metrics 
@@ -714,24 +714,25 @@
 trics = new 
+E
 Metrics(mcMM
@@ -736,16 +736,73 @@
 cMMO.p);
+%0A                Metrics metrics = emetrics.getMetrics();
 %0A%0A      
@@ -6922,16 +6922,17 @@
         
+e
 metrics.
@@ -6936,16 +6936,23 @@
 cs.start
+Metrics
 ();%0A    

Add in EMetrics