package language;

import java.util.ArrayList;

import language.code.CTypeCode;
import language.code.Pascal;

import background.CommandManager;
import background.commands.Command;

public class CodeLanguage extends Language {
	
	public CodeLanguage(CommandManager cm) { super(cm); }

	@Override
	public Command getCommandByName(String name) {
		switch (name) {			
			case "premenna": return commandManager.getCommand("Variable", false);
			case "pole": return commandManager.getCommand("Array", false);
			case "for": return commandManager.getCommand("For", false);
			case "repeat": return commandManager.getCommand("Repeat", false);
			case "while": return commandManager.getCommand("While", false);
			case "if": return commandManager.getCommand("If", false);
			case "operacia": return commandManager.getCommand("Operation", false);
			case "vstup": return commandManager.getCommand("Read", false);
			case "vystup": return commandManager.getCommand("Write", false);
		}
		return null;		
	}
	
	public static CodeLanguage getLangByName(String name, CommandManager cm) {
		switch (name) {
			case "C typ": return new CTypeCode(cm);
			case "Pascal": return new Pascal(cm);
			default: return new CTypeCode(cm);
		}
	}
	

	public String changeFormat(String s) {
		/* funkcia ktora zmeni format z klasickeho kodu na hviezdickovy, ktory sa moze potom dalej spracovat */
		return s;
	}
	
	public String formatProcess(String s, String begin, String end) {
		ArrayList<String> lines = new ArrayList<String>();
		String returnS = "";
		
		String xLines[] = s.split("[\\r\\n]+");				//  String xLines[] = input.split(" *;+ *"); 		
		for (int i=0; i<xLines.length; i++) { lines.add(xLines[i].trim());}		/* Nasekane na riadky a jednotlive prikazy, kedze tie su oddelene bodkociarkou */

		int prevstars = 0;
		int stars = 0;
		
		for (String line: lines) {
			prevstars = stars;
			if (line.contains(begin)) { prevstars++; line = line.replace(begin, ""); }
			if (line.contains(end)) { prevstars--; line = line.replace(end, ""); }
			if (!line.trim().equals("")) { 
				for (int i=1; i<=stars; i++) returnS += "* ";
				returnS +=line.trim() + System.getProperty("line.separator");
			}			
			stars = prevstars;
		}

		return returnS;
	}
}
