package de.lessvoid.nifty.examples.console;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.console.controller.ConsoleCommandHandler;
import de.lessvoid.nifty.controls.console.controller.ConsoleControl;
import de.lessvoid.nifty.effects.EffectEventId;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.input.mapping.DefaultInputMapping;
import de.lessvoid.nifty.screen.KeyInputHandler;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 * ConsoleDemoStartScreen.
 * @author void
 */
public class ConsoleSameScreenStartScreen implements ScreenController, KeyInputHandler {
  private Nifty nifty;
  private Screen screen;
  private boolean consoleVisible = false;
  private boolean allowConsoleToggle = true;
  private Element oldFocusElement;

  public void bind(final Nifty newNifty, final Screen newScreen) {
    nifty = newNifty;
    screen = newScreen;
    screen.addKeyboardInputHandler(new DefaultInputMapping(), this);
    final ConsoleControl control = screen.findControl("console", ConsoleControl.class);
    control.output("Nifty Console Demo\nVersion: 1.0");
    control.addCommandHandler(new ConsoleCommandHandler() {
      public void execute(final String line) {
        // just echo to the console
        control.output("your input was: " + line);
        if ("exit".equals(line.toLowerCase())) {
          back();
        }
      }
    });
  }

  public void onStartScreen() {
  }

  public void onEndScreen() {
  }

  public void back() {
    nifty.fromXml("all/intro.xml", "menu");
  }

  public boolean keyEvent(final NiftyInputEvent inputEvent) {
    if (inputEvent == NiftyInputEvent.ConsoleToggle) {
      toggleConsole();
      return true;
    } else {
      return false;
    }
  }

  private void toggleConsole() {
    if (allowConsoleToggle) {
      allowConsoleToggle = false;
      if (consoleVisible) {
        closeConsole();
      } else {
        openConsole();
      }
    }
  }

  private void openConsole() {
    Element consoleLayer = screen.findElementByName("consoleLayer");
    consoleLayer.showWithoutEffects();
    consoleLayer.startEffect(EffectEventId.onStartScreen, new EndNotify() {
      @Override
      public void perform() {
        oldFocusElement = screen.getFocusHandler().getKeyboardFocusElement();

        Element consoleElement = screen.findElementByName("console");
        consoleElement.setFocus();

        consoleVisible = true;
        allowConsoleToggle = true;
      }
    });
  }

  private void closeConsole() {
    final Element consoleLayer = screen.findElementByName("consoleLayer");
    consoleLayer.startEffect(EffectEventId.onEndScreen, new EndNotify() {
      @Override
      public void perform() {
        consoleVisible = false;
        allowConsoleToggle = true;
        if (oldFocusElement != null) {
          oldFocusElement.setFocus();
        }
        consoleLayer.hideWithoutEffect();
      }
    });
  }
}

@@ -923,16 +923,84 @@
 lement;%0D
+%0A  private Element consoleElement;%0D%0A  private Element consoleLayer;%0D
 %0A%0D%0A  pub
@@ -1176,24 +1176,149 @@
 (), this);%0D%0A
+%0D%0A    consoleElement = screen.findElementByName(%22console%22);%0D%0A    consoleLayer = screen.findElementByName(%22consoleLayer%22);%0D%0A%0D%0A
     final Co
@@ -1779,32 +1779,77 @@
 tartScreen() %7B%0D%0A
+    removeConsoleElementFromFocusHandler();%0D%0A
   %7D%0D%0A%0D%0A  public 
@@ -2418,78 +2418,8 @@
  %7B%0D%0A
-    Element consoleLayer = screen.findElementByName(%22consoleLayer%22);%0D%0A
     
@@ -2658,26 +2658,24 @@
 ent();%0D%0A
-%0D%0A
         
 Element 
@@ -2666,23 +2666,26 @@
         
-Element
+// add the
  console
@@ -2696,44 +2696,102 @@
 ent 
-= screen.findElementByName(%22console%22
+to the focushandler, when it's not yet added already%0D%0A        addConsoleElementToFocusHandler(
 );%0D%0A
@@ -2958,84 +2958,8 @@
  %7B%0D%0A
-    final Element consoleLayer = screen.findElementByName(%22consoleLayer%22);%0D%0A
     
@@ -3069,32 +3069,77 @@
 id perform() %7B%0D%0A
+        consoleLayer.hideWithoutEffect();%0D%0A%0D%0A
         consoleV
@@ -3183,32 +3183,34 @@
 Toggle = true;%0D%0A
+%0D%0A
         if (oldF
@@ -3283,24 +3283,26 @@
      %7D%0D%0A
+%0D%0A
         
 consoleL
@@ -3297,47 +3297,271 @@
     
-consoleLayer.hideWithoutEffect(
+removeConsoleElementFromFocusHandler();%0D%0A      %7D%0D%0A    %7D);%0D%0A  %7D%0D%0A%0D%0A  private void addConsoleElementToFocusHandler() %7B%0D%0A    if (screen.getFocusHandler().findElement(consoleElement.getId()) == null) %7B%0D%0A      screen.getFocusHandler().addElement(consoleElement
 );%0D%0A    
   %7D%0D
@@ -3552,34 +3552,141 @@
 ment);%0D%0A    
+%7D%0D%0A
   %7D%0D%0A
+%0D%0A
   
-  %7D
+private void removeConsoleElementFromFocusHandler() %7B%0D%0A    screen.getFocusHandler().remove(consoleElement
 );%0D%0A  %7D%0D%0A%7D%0D%0A

add fix for hidden elements getting the keyboard focus