package de.longri.cachebox3.gui.stages;

import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import de.longri.cachebox3.gui.views.DescriptionView;
import de.longri.cachebox3.gui.views.MapView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Hoepfner on 25.08.2016.
 */
public class StageManager {
    final static Logger log = LoggerFactory.getLogger(StageManager.class);
    final static Array<NamedStage> stageList = new Array<NamedStage>(5);
    final static NamedStage toastStage = new NamedStage("toastStage");

    private static boolean debug = true;
    private static boolean writeDrawSequence = debug;

    private static NamedStage mainStage;
    private static InputMultiplexer inputMultiplexer;

    public static void draw() {

        if (stageList.size < 2) {
            mainStage.act();
            mainStage.draw();
            if (writeDrawSequence) log.debug("draw mainStage");
        }

        if (stageList.size == 1) {
            Stage stage = stageList.get(0);
            stage.act();
            stage.draw();
            if (writeDrawSequence) log.debug("draw Stage level 0");
        } else if (stageList.size >= 1) {
            for (int idx = stageList.size - 1; idx < stageList.size; idx++) {
                Stage stage = stageList.get(idx);
                stage.act();
                stage.draw();
                if (writeDrawSequence) log.debug("draw Stage level " + idx);
            }
        }

        // draw toastStage at last over all
        if (toastStage.getActors().size > 0) {
            toastStage.act();
            toastStage.draw();
            if (writeDrawSequence) log.debug("draw Toast Stage");
        }
        if (writeDrawSequence) log.debug("END stage drawing");
        writeDrawSequence = false;

    }

    public static void addToastActor(Actor actor) {
        toastStage.addActor(actor);
        if (debug) writeDrawSequence = true;
    }

    public static void showOnNewStage(final Actor actor) {


        if (stageList.size > 0) {
            NamedStage actStage = stageList.get(stageList.size - 1);

            String lastName = actStage.getName();
            if (lastName.equals(actor.getName())) {

                // don't show double
                return;
            }
        } else {
            if (mainStage instanceof ViewManager) {
                ViewManager viewManager = (ViewManager) mainStage;
                if (viewManager.getActView() instanceof MapView) {
                    // handle MapView on ViewManagerStage
                    MapView mapView = (MapView) viewManager.getActView();
                    mapView.setInputListener(false);
                    log.debug("remove input listener from MapView");
                } else if (viewManager.getActView() instanceof DescriptionView) {
                    // handle DescriptionView on ViewManagerStage
                    DescriptionView descriptionView = (DescriptionView) viewManager.getActView();
                    descriptionView.onHide();
                    log.debug("Call DescriptionView.onHide() for showing overlay stage");
                }
            }
        }


        NamedStage newStage = new NamedStage(actor.getName());
        newStage.addActor(actor);
        newStage.setKeyboardFocus(actor);
        newStage.setScrollFocus(actor);


        stageList.add(newStage);
        log.debug("add new Stage: " + newStage.getName());
        log.debug("Stage list: " + stageList.toString());

        //switch input processor to window stage

        if (stageList.size > 1) {
            inputMultiplexer.removeProcessor(stageList.get(stageList.size - 2));
        } else {
            inputMultiplexer.removeProcessor(mainStage);
        }


        addNonDoubleInputProzessor(newStage);

        log.debug("InputProzessors:" + inputMultiplexer.getProcessors().toString());

        if (debug) writeDrawSequence = true;
    }

    public static void showOnActStage(Actor actor) {
        Stage stage = stageList.get(stageList.size - 1);
        stage.addActor(actor);
    }

    public static void removeAllWithActStage() {
        if (stageList.size == 0) return;
        NamedStage stage = stageList.pop();

        log.debug("remove Stage: " + stage.getName());
        log.debug("Stage list: " + stageList.toString());

        //switch input processor to main stage
        inputMultiplexer.removeProcessor(stage);
        if (stageList.size > 0) {
            addNonDoubleInputProzessor(stageList.get(stageList.size - 1));
        } else {
            addNonDoubleInputProzessor(mainStage);
            {
                ViewManager viewManager = (ViewManager) mainStage;
                if (viewManager.getActView() instanceof MapView) {
                    // handle MapView on ViewManagerStage
                    MapView mapView = (MapView) viewManager.getActView();
                    mapView.setInputListener(true);
                    log.debug("Enable input listener for MapView");
                } else if (viewManager.getActView() instanceof DescriptionView) {
                    // handle DescriptionView on ViewManagerStage
                    DescriptionView descriptionView = (DescriptionView) viewManager.getActView();
                    descriptionView.onShow();
                    log.debug("Call DescriptionView.onShow() for restore view");
                }
            }
        }
        log.debug("InputProzessors:" + inputMultiplexer.getProcessors().toString());
        if (debug) writeDrawSequence = true;
    }

    public static void setMainStage(NamedStage stage) {
        mainStage = stage;

        // add mainStage to input processor
        if (inputMultiplexer != null) addNonDoubleInputProzessor(mainStage);
    }

    private static void addNonDoubleInputProzessor(InputProcessor processor) {
        if (inputMultiplexer.getProcessors().contains(processor, true)) return;
        inputMultiplexer.addProcessor(processor);
    }

    public static void setInputMultiplexer(InputMultiplexer newInputMultiplexer) {
        inputMultiplexer = newInputMultiplexer;
    }

    public static Batch getBatch() {
        return mainStage.getBatch();
    }

    public static void addMapMultiplexer(InputMultiplexer mapInputHandler) {
        if (!inputMultiplexer.getProcessors().contains(mapInputHandler, true)) {
            inputMultiplexer.addProcessor(mapInputHandler);
        }
    }

    public static void removeMapMultiplexer(InputMultiplexer mapInputHandler) {
        inputMultiplexer.removeProcessor(mapInputHandler);
    }
}

@@ -5583,32 +5583,84 @@
                %7D
+%0A%0A                viewManager.getActView().onShow();
 %0A            %7D%0A 
@@ -5790,32 +5790,33 @@
 equence = true;%0A
+%0A
     %7D%0A%0A    publi

call onShow with close Menu