package sk.mrtn.demo.pixi.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.logging.client.LogConfiguration;
import elemental.client.Browser;
import sk.mrtn.demo.pixi.client.buttons.IShapeButton;
import sk.mrtn.demo.pixi.client.common.IStage;
import sk.mrtn.demo.pixi.client.defaultdemo.DefaultDemo;
import sk.mrtn.demo.pixi.client.lastguardiandemo.LastGuardianDemo;
import sk.mrtn.demo.pixi.client.tokitori.TokiToriDemo;
import sk.mrtn.demo.pixi.client.unittests.UnitTests;
import sk.mrtn.library.client.utils.IUrlParametersManager;
import sk.mrtn.library.client.utils.stats.Stats;

import javax.inject.Inject;
import javax.inject.Provider;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by klaun on 25/04/16.
 * This class is supposed to be main entry point for whole project
 */
public class DemoPixi {


    public static IResources RES = IResources.impl;
    private static Logger LOG;
    static {
        if (LogConfiguration.loggingIsEnabled()) {
            LOG = Logger.getLogger(DemoPixi.class.getSimpleName());
            LOG.setLevel(Level.ALL);
        }
    }

    private final IUrlParametersManager urlParametersManager;
    private final Provider<DefaultDemo> defaultDemoProvider;
    private final Provider<LastGuardianDemo> lastGuardianDemoProvider;
    private final Provider<UnitTests> unitTestsProvider;
    private final Provider<TokiToriDemo> tokiToriDemoProvider;
    private final IStage stage;
    private final Stats stats;
    private final Menu menu;

    enum Type {
        PARTICLES("particles"),
        TOKI_TORI("tokitori"),
        LAST_GUARDIAN("lastguardian"),
        UNIT_TESTS("unittests"),
        DEFAULT("default");

        private final String name;

        Type(String name) {
            this.name = name;
        }

        public static Type parse(String name) {
            for (Type type : values()) {
                if (type.name.equals(name)) return type;
            }
            return DEFAULT;
        }
        public String toString() {
            return this.name;
        }
    }

    @Inject
    DemoPixi(
            final IStage stage,
            final Stats stats,
            final IUrlParametersManager urlParametersManager,
            final Menu menu,
            final Provider<DefaultDemo> defaultDemoProvider,
            final Provider<LastGuardianDemo> lastGuardianDemoProvider,
            final Provider<UnitTests> unitTestsProvider,
            final Provider<TokiToriDemo> tokiToriDemoProvider
    ){
        this.stage = stage;
        this.stats = stats;
        this.urlParametersManager = urlParametersManager;
        this.menu = menu;
        this.defaultDemoProvider = defaultDemoProvider;
        this.lastGuardianDemoProvider = lastGuardianDemoProvider;
        this.unitTestsProvider = unitTestsProvider;
        this.tokiToriDemoProvider = tokiToriDemoProvider;
    }

    public void initialize() {
        LOG.info("INJECTION STARTED");
        this.stage.initialize(1024,1024);
        RES.main().ensureInjected();
        if (urlParametersManager.getParameter("dstats") == "true") {
            Browser.getDocument().getBody().appendChild(stats.getDom());
        }
        this.menu.initialize(() -> onMenuInitialized());
    }

    private void onMenuInitialized() {
        menu.addButton(Type.PARTICLES.name(),DemoPixi.this::startParticles);
        menu.addButton(Type.TOKI_TORI.name(),DemoPixi.this::startTokiTori);
        menu.addButton(Type.LAST_GUARDIAN.name(),DemoPixi.this::startLastGuardian);
        menu.addButton(Type.UNIT_TESTS.name(),DemoPixi.this::startUnitTests);

        Type type = Type.parse(urlParametersManager.getParameter("autorun"));
        switch (type) {
            case PARTICLES:
                startParticles();
                break;
            case TOKI_TORI:
                startTokiTori();
                break;
            case LAST_GUARDIAN:
                startLastGuardian();
                break;
            case UNIT_TESTS:
                startUnitTests();
                break;
            case DEFAULT:
                menu.show();
        }
    }

    private void startLastGuardian() {
        lastGuardianDemoProvider.get().open(null);
        if (!this.lastGuardianDemoProvider.get().isInitialized()) {
            lastGuardianDemoProvider.get().getButton().addClickHandler(button -> menu.show());
        }
    }

    private void startUnitTests() {
        this.unitTestsProvider.get().open(null);
        if (!this.unitTestsProvider.get().isInitialized()) {
            unitTestsProvider.get().getButton().addClickHandler(button -> menu.show());
        }
    }

    private void startTokiTori() {
        this.tokiToriDemoProvider.get().open(null);
        if (!this.tokiToriDemoProvider.get().isInitialized()) {
            tokiToriDemoProvider.get().getButton().addClickHandler(button -> menu.show());
        }
    }

    private void startParticles() {
        this.defaultDemoProvider.get().open(null);
        if (!this.defaultDemoProvider.get().isInitialized()) {
            defaultDemoProvider.get().getButton().addClickHandler(button -> menu.show());
        }
    }

    public static native void log(Object object) /*-{
        $wnd.console.log(object);
    }-*/;
}

@@ -54,163 +54,37 @@
 gwt.
-core.client.GWT;%0Aimport com.google.gwt.logging.client.LogConfiguration;%0Aimport elemental.client.Browser;%0Aimport sk.mrtn.demo.pixi.client.buttons.IShapeButt
+logging.client.LogConfigurati
 on;%0A
@@ -362,16 +362,62 @@
 tTests;%0A
+import sk.mrtn.library.client.ticker.ITicker;%0A
 import s
@@ -514,16 +514,22 @@
 ts.Stats
+Loader
 ;%0A%0Aimpor
@@ -1414,27 +1414,25 @@
 e final 
-Stats stats
+Menu menu
 ;%0A    pr
@@ -1443,25 +1443,30 @@
 e final 
-Menu menu
+ITicker ticker
 ;%0A%0A    e
@@ -2051,16 +2051,50 @@
 moPixi(%0A
+            final ITicker ticker,%0A
         
@@ -2121,39 +2121,8 @@
 ge,%0A
-            final Stats stats,%0A
     
@@ -2483,21 +2483,23 @@
 his.
-stage = stage
+ticker = ticker
 ;%0A  
@@ -2516,18 +2516,18 @@
 .sta
-ts
+ge
  = sta
-ts
+ge
 ;%0A  
@@ -3008,151 +3008,78 @@
     
-if (urlParametersManager.getParameter(%22dstats%22) == %22true%22) %7B%0A            Browser.getDocument().getBody().appendChild(stats.getDom());%0A        %7D
+StatsLoader.Statics.initialize(this.ticker,this.urlParametersManager);
 %0A   

refactor due to stats injection removal in library