/*
 * Copyright (C) 2014 Wolfgang Buecke
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package de.wbuecke.codec;

import java.util.LinkedHashMap;
import java.util.Map;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;

class Inputs {

	private final Map<Input, TextField> inputs = new LinkedHashMap<>();
	private final GridPane grid;
	int row = 0;

	Inputs() {
		grid = new GridPane();
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(25));
	}

	void add(Input input) {
		Label labelNode = new Label(input.getLabel());
		grid.add(labelNode, 0, row);

		TextField textField = new TextField();
		textField.setMinWidth(300);
		textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
			@Override public void handle(KeyEvent event) {
				String text = textField.getText();
				update(input.decode(text), input);
			}
		});
		grid.add(textField, 1, row);
		inputs.put(input, textField);
		row++;
	}

	void add(Node node) {
		grid.add(node, 0, row++, 2, 1);
	}
	
	void update(String plaintext, Input except) {
		for (Map.Entry<Input, TextField> entry : inputs.entrySet()) {
			if (entry.getKey() == except)
				continue;
			
			entry.getValue().setText(encodeAndSet(entry.getKey(), plaintext));
		}
	}
	
	private String encodeAndSet(Input input, String plaintext) {
		if (plaintext == null)
			return "(invalid input)";
		else
		{
			String encoded = input.encode(plaintext);
			return encoded != null ? encoded : "(unsupported)";
		}
	}
	
	Parent getNode() {
		return grid;
	}
}

@@ -678,43 +678,8 @@
 Map;
-%0A%0Aimport javafx.event.EventHandler;
 %0Aimp
@@ -838,44 +838,8 @@
 ld;%0A
-import javafx.scene.input.KeyEvent;%0A
 impo
@@ -988,16 +988,24 @@
  grid;%0A%09
+private 
 int row 
@@ -1336,175 +1336,65 @@
 sed(
-new EventHandler%3CKeyEvent%3E() %7B%0A%09%09%09@Override public void handle(KeyEvent event) %7B%0A%09%09%09%09String text = textField.getText();%0A%09%09%09%09update(input.decode(text), input);%0A%09%09%09%7D%0A%09%09%7D
+event -%3E update(input.decode(textField.getText()), input)
 );%0A%09
@@ -1528,18 +1528,17 @@
  1);%0A%09%7D%0A
-%09
 %0A
+
 %09void up
@@ -1691,12 +1691,9 @@
 ue;%0A
-%09%09%09
 %0A
+
 %09%09%09e
@@ -1765,18 +1765,17 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -2000,17 +2000,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09Parent

make private, use lambda