package de.prob2.ui.consoles;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class ConsoleSearchHandler {
	
	protected boolean searchActive = false;
	protected List<SearchResult> searchResults;
	protected int currentSearchIndex = 0;
	private final Console parent;
	private final ResourceBundle bundle;
	
	public ConsoleSearchHandler(Console parent, ResourceBundle bundle) {
		this.searchResults = new ArrayList<>();
		this.parent = parent;
		this.bundle = bundle;
	}
	
	public boolean isActive() {
		return searchActive;
	}
	
	public void activateSearch() {
		currentSearchIndex = 0;
		searchActive = true;
		if (searchResults.isEmpty()) {
			searchResults.add(new SearchResult(getCurrentSearchResult(), false));
		}
	}
	
	public void deactivateSearch() {
		searchResults.clear();
		currentSearchIndex = 0;
		searchActive = false;
	}
	
	protected void searchResult(KeyEvent e) {
		searchResults.clear();
		String key = getSearchCurrent();
		if (e.getCode() == KeyCode.BACK_SPACE) {
			key = key.substring(0, Math.max(0, key.length() - 1));
		} else {
			key += e.getText();
		}
		for (int i = parent.getInstructions().size() - 1; i >= 0; i--) {
			if (parent.getInstructions().get(i).getInstruction().contains(key)) {
				searchResults.add(new SearchResult(parent.getInstructions().get(i).getInstruction(),true));
			}
		}
		if (searchResults.isEmpty()) {
			searchResults.add(new SearchResult(getCurrentSearchResult(), false));
		}
	}
	
	public String getSearchCurrent() {
		int posOfFirstQuotation = parent.getCurrentLine().indexOf(39);
		int posOfLastQuotation = parent.getCurrentLine().lastIndexOf(39);
		return parent.getCurrentLine().substring(posOfFirstQuotation + 1, posOfLastQuotation);
	}
	
	public String getCurrentSearchResult() {
		int posOfColon = parent.getCurrentLine().indexOf(':') + parent.getText().lastIndexOf('\n') + 5;
		return parent.getText().substring(posOfColon, parent.getText().length());
	}
	
	protected void handleKey(KeyEvent e) {
		if (isActive()) {
			currentSearchIndex = 0;
			searchResult(e);
			refreshSearch();
		}
	}
	
	protected void handleEnter() {
		if (isActive()) {
			deactivateSearch();
		}
	}
	
	protected boolean handleDeletion(KeyEvent e) {
		if (isActive()) {
			if (e.getCode() == KeyCode.DELETE) {
				parent.deactivateSearch();
				return true;
			} else if (parent.getCaretPosition() != parent.getLength() - parent.getCurrentLine().length() + parent.getCurrentLine().indexOf('\'') + 1) {
				handleKey(e);
			}
		}
		return false;
	}
	
	protected void refreshSearch() {
		final String format = bundle.getString(searchResults.get(0).getFound() ? "consoles.prompt.backwardSearch" : "consoles.prompt.backwardSearchFailed");
		final String addition = String.format(format, getSearchCurrent(), searchResults.get(currentSearchIndex).getResult());
		int posOfEnter = parent.getText().lastIndexOf('\n');
		parent.deleteText(posOfEnter + 1, parent.getText().length());
		parent.appendText(addition);
		int posOfColon = parent.getCurrentLine().indexOf(':') + parent.getText().lastIndexOf('\n') + 4;
		parent.moveTo(posOfColon -1);
		parent.scrollYToPixel(Double.MAX_VALUE);
	}
	
	protected void searchNext() {
		currentSearchIndex = Math.min(searchResults.size() - 1, currentSearchIndex + 1);
		refreshSearch();
	}

}

@@ -188,16 +188,22 @@
 %0Apublic 
+final 
 class Co
@@ -224,31 +224,29 @@
 dler %7B%0A%09%0A%09pr
-otec
+iva
 te
-d
  boolean sea
@@ -267,23 +267,27 @@
 lse;%0A%09pr
-otected
+ivate final
  List%3CSe
@@ -316,23 +316,21 @@
 lts;%0A%09pr
-otec
+iva
 te
-d
  int cur
@@ -948,39 +948,37 @@
  false;%0A%09%7D%0A%09%0A%09pr
-otec
+iva
 te
-d
  void searchResu
@@ -2034,34 +2034,24 @@
 h());%0A%09%7D%0A%09%0A%09
-protected 
 void handleK
@@ -2160,34 +2160,24 @@
 ;%0A%09%09%7D%0A%09%7D%0A%09%0A%09
-protected 
 void handleE
@@ -2238,26 +2238,16 @@
 %7D%0A%09%7D%0A%09%0A%09
-protected 
 boolean 
@@ -2574,31 +2574,29 @@
 se;%0A%09%7D%0A%09%0A%09pr
-otec
+iva
 te
-d
  void refres
@@ -3211,18 +3211,8 @@
 %0A%09%0A%09
-protected 
 void

Remove all protected modifiers from ConsoleSearchHandler