package slogo_front;

import java.util.List;

import slogo_back.Model;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;

public class Manager {

	/**
	 * TODO: Please have all methods that back-end calls have "Number" as the
	 * return type. If there's nothing to return, simply return null.
	 */
	View view;
	// canvas
	Display display;
	private static final int xCanvas = 1000;
	private static final int yCanvas = 600;
	
	Turtle turtle = new Turtle(xCanvas/2, yCanvas/2, 0, 0, Color.BLACK, 1,
			"/images/turtle_small.png", true, true);
	Model model;

	Manager(View defaultView) {
		view = defaultView;
		display = view.getDisplay();
		model = new Model("resources/languages/English.properties");
		model.setManager(this);
		initialize();
		// turtle = new Turtle(0,0,0, )
	}

	/*
	 * BACKEND AND FRONTEND API INTEGRATION
	 */
	 public double home(double[] var){
		 return display.home(turtle);
	 }
	
	 public double hideTurtle(double[] var){
		 return display.hide(turtle);
	 }
	
	 public double showTurtle(double[] var){
		 return display.show(turtle);
	 }
	
	 public double penUp(double[] var){
		 return display.penUp(turtle);
	 }
	
	 public double penDown(double[] var){
		 return display.penDown(turtle);
	 }
	
	 public double setXY(double[] pos){
		 return display.setXY(turtle, pos[0], pos[1]);
	 }
	
	 public double towards(double[] pos){
		 return display.towards(turtle, pos[0], pos[1]);
	 }
	
	 public double setHeading(double[] degrees){
		 return display.setHeading(turtle, degrees[0]);
	 }
	
	 public double right(double[] degrees){
		 return display.right(turtle, degrees[0]);
	 }
	
	 public double left(double[] degrees){
		 return display.left(turtle, degrees[0]);
	 }
	
	 public double back(double[] pixels){
		 return display.back(turtle, pixels[0]);
	 }
	
	 public double forward(double[] pixels){
		 return display.forward(turtle, pixels[0]);
	 }
	
	 public double xCor(double[] var){
		 return display.xCor(turtle);
		 
	 }
	
	 public double yCor(double[] var){
		 return display.yCor(turtle);
	 }
	
	 public double isPenDown(double[] var){
		 return display.isPenDown(turtle);
	 }
	
	 public double isShowing(double[] var){
		 return display.showing(turtle);
	 }

	private void initialize() {
		// setting handlers
		view.setCommandLine(parse);
		view.setMoveForward(forwardEvent);
	}

	// private EventHandler parse = new EventHandler<KeyEvent>() {
	// public void handle(KeyEvent event) {
	//
	// view.
	// }
	//
	// };

	private EventHandler<KeyEvent> parse = new EventHandler<KeyEvent>() {
		public void handle(KeyEvent event) {
			String parse;
			KeyCode keyCode = event.getCode();
			if (keyCode == KeyCode.ENTER) {
				parse = view.commandLineText();
				if (parse.toLowerCase().equals("clear")) {
					view.clearHistoryText();
				}
				view.addHistoryText(parse);
				System.out.println(parse);

				// TODO throw error here
//				try{
					List<Double> results = model.toBack(parse + "\n");
					for (Double value : results) {
						System.out.println(value);
					}
//				}catch(InvalidParameterException e){
					
//				}
			}

		}
	};

	private EventHandler<MouseEvent> forwardEvent = new EventHandler<MouseEvent>() {
		public void handle(MouseEvent event) {
			// TODO turtle within display or in view? think about allowances for
			// multiple turtles
			display.forward(turtle, 100);
		}

	};

}

@@ -3071,16 +3071,30 @@
 ck(parse
+.toLowerCase()
  + %22%5Cn%22)

setting input to lowercase