package ui.view;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Languages {

	static int languageVar = 0;

	// Language Array Positions:
	// ToolBar (0-16)
	// Tables (17-24)
	// Graph (25-26)
	// Warning PopUps (27-30)
	// Edit Edges Pop Up (31-34)
	// Search/Replace Pop Up (35-45)
	// Edit Edges Pop Up (46-55)
	// Add PopUp Tree (56-63)
	// Add PopUp Element (64-71)
	// Info for PropertyTable (72-82)
	// Info SimulationMenu (83-87)
	// Exit Pop Up (88)
	// Simulation ToolTip (89-93)
	// Image Size Slider (94)
	// Menu - right click on HolonObjt (95-104)
	public static final int right_click_create_template = 104;
	// to add more: increase languageSize and add them to the arrayEN

	/**
	 * Number of different Language Array Positions
	 */
	private static int languageSize = 105;

	static String[] arrayEN = {
			// ToolBar (0-16)
			"File", "New", "Open", "Save", "Edit", "Undo", "Redo", "Find/Replace", "Edit showed Information", "Options",
			"Reset Categories", "View", "View Size", "Help", "About Us", "Edit Edges", "Language",
			// Tables (17-24)
			"Object", "Nr.", "Device", "Energy", "Quantity", "Activated", "Field", "Information",
			// Graph (25-26)
			"None ", "Reset",
			// Warning PopUps (27-30)
			"Warning", "Do you want to save your current data?", "Do you really want to delete the Category ",
			"Please select a Category or an Object in order to delete something.",
			// Edit Edges Pop Up (31-34)
			"Edit Showed Informations", "Show Total Energy of Objects", "Show Connection Properties", "Cancel",
			// Search/Replace Pop Up (35-45)
			"Search for Objects", "Find", "Replace", "Direction", "Forward", "Backward", "Scope", "All", "Single",
			"Replace All", "Close",
			// Edit Edges Pop Up (46-55)
			"Edit Capacities of Edges", "Maximum Capacity:", "Change for all existing Edges only",
			"Change for new created Edges only", "Change for all existing and new created Edges", "Cancel",
			"Please select one of the options",
			"Please enter a number greater or equal 0 in the Field for Maximum Capacity", "Edit Edge Capacities",
			"(enter \"infinite\" for infinite Capacity)",
			// Add PopUp Tree (56-63)
			"Please enter a Name for Category ", "Please select a Category first before adding ", "Add Object Menu",
			"Name:", "Browse Image", "Add Element", "Delete Element", "Cancel",
			// Add PopUp Element (64-71)
			"Add Element to Object", "Element Name:", "Provided Energy:", "Amount:",
			"Please enter numbers in the Fields amount and Energy", "No name", "Name already given", "Cancel",
			// Info for PropertyTable (72-82)
			"Name", "Total Energy", "Manual", "Active", " is connected to", " with ID: ", "Edge: ", " to ",
			"Current flow", "Max. Capacity", "Status",
			// Info SimulationMenu (83-87)
			"Simulate", "Simulation Speed:", "Algorithm:", "non selected", "success",
			// Exit Pop Up (88)
			"Are you sure you want to exit?",
			// Simulation ToolTip (89-93)
			"Play", "Reset", "Forward", "Backward", "Time Slider",
			// Image Size Slider (94)
			"Image Size",
			// Menu - right click on HolonObjt (95-104)
			"Cut", "Copy", "Paste", "Delete", "Group", "Ungroup", "Track", "Untrack","Category","Create Template"};

	static String[] arrayES = readLanguageFile("Texts/ES.lang"); 

	static String[] arrayDE = readLanguageFile("Texts/DE.lang"); 
		
	static String[] arrayCZ = readLanguageFile("Texts/CZ.lang"); 

	static String[] arrayZH = readLanguageFile("Texts/ZH.lang");
	
	public static String[] getLanguage() {
		switch (languageVar) {
		case 0:
			return arrayEN;
		case 1:
			return arrayES;
		case 2:
			return arrayDE;
		case 3:
			return arrayCZ;
		case 4:
			return arrayZH;
		default:
			return arrayEN;
		}
	}

	public static void setLanguage(int i) {
		languageVar = i;
	}
	
	/**
	 * Reads language file from jar, each line corresponds to
	 * a position of the {@value #arrayEN}.
	 * Lines starting with # are ignored as Comments.
	 * Missing translations will be filled with english text.
	 * @param path path of the file e.g. "Texts/DE.lang"
	 * @return Array of translated String as arrayEN 
	 */
	private static String[] readLanguageFile(String path){
		String[] langArr;
		try {
			//read File from Jar
			InputStreamReader reader = new InputStreamReader(Util.loadStream(/*Languages.class.getClassLoader(),*/path));
			BufferedReader br = new BufferedReader(reader);
			
			//store Lines in Array
			int lineNumber = 0;
			langArr = new String[languageSize];
			for (String line = br.readLine(); line != null && lineNumber<languageSize; line = br.readLine()) {
				//store line in Array, # initializes Comments
				if (line.isEmpty() || line.startsWith("#"))
					continue;
				langArr[lineNumber]=new String(line.getBytes(Charset.defaultCharset()),"UTF-8");
				lineNumber++;
			}
			//missing translations? -> english
			while(lineNumber<languageSize){
				langArr[lineNumber]=arrayEN[lineNumber];
				lineNumber++;
			}
			reader.close();
			return langArr;
		} catch (Exception e) {
			System.err.print("Loading Languagepack: "+path+" failed.");
			//default: English
			return arrayEN;
		}
	}
}
