package authoring_environment.room.configure_popup;

import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Consumer;

import authoring_environment.room.InvalidConfigureException;
import authoring_environment.room.object_instance.DraggableImage;
import javafx.scene.control.Alert;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import structures.data.DataInstance;

public class ConfigureController {
	private ConfigureView configure;
	private ConfigureModel model;
	private DataInstance myDataInstance;
	private DraggableImage myDragImage;
	private Consumer<Void> myDrawFunction;
	private final String SCALE_ERROR = "ScaleError";
	private final String TRANSPARENCY_ERROR = "TransparencyError";
	private final ResourceBundle myResources;
	//TODO think about using interface to isolate set and get methods for DataInstance
	public ConfigureController(ResourceBundle resources, DataInstance dataInstance, DraggableImage dragImage, Consumer<Void> redrawFunc) {
		configure = new ConfigureView(resources);
		model = new ConfigureModel(dataInstance);
		myDataInstance = dataInstance;
		myDragImage = dragImage;
		myDrawFunction = redrawFunc;
		myResources = resources;
	}
	
	public void initialize() {
		populatePopUp();
		configure.getSaveButton().setOnAction(e -> {
		try {
			onSave(); 
		}
		catch (InvalidConfigureException exception) {
			Alert myAlert = new Alert(AlertType.ERROR);
			myAlert.setContentText(exception.getBadInput());
			myAlert.showAndWait();
		}
		});
	}
	
	private void populatePopUp() {
		List<TextField> fieldList = configure.getFieldList();
		setInput(0, fieldList, myDataInstance.getVelocity().x);
		setInput(1, fieldList, myDataInstance.getVelocity().y);
		setInput(2, fieldList, myDataInstance.getAngularVelocity());
		setInput(3, fieldList, myDataInstance.getScaleX());
		setInput(4, fieldList, myDataInstance.getScaleY());
		setInput(5, fieldList, myDataInstance.getAngle());
		setInput(6, fieldList, myDataInstance.getAlpha());
		RadioButton visiblityButton = configure.getVisiblity();
		visiblityButton.setSelected(myDataInstance.isVisible());
		myDragImage.setAngle(myDataInstance.getAngle());
		myDragImage.setVisibility(myDataInstance.isVisible());
		myDragImage.setScale(myDataInstance.getScaleX(), myDataInstance.getScaleY());
		myDragImage.setAlpha(myDataInstance.getAlpha());
	}
	
	private void onSave() throws InvalidConfigureException {
		List<TextField> fieldList = configure.getFieldList();
		double velocityFieldX = getInput(0, fieldList);
		double velocityFieldY = getInput(1, fieldList);
		model.setVelocity(velocityFieldX, velocityFieldY);
		double angularVelocity = getInput(2, fieldList);
		model.setAngularVelocity(angularVelocity);
		double scaleX = getInput(3, fieldList);
		double scaleY = getInput(4, fieldList);
		if (scaleX < 0 || scaleY < 0)
			throw new InvalidConfigureException(myResources.getString(SCALE_ERROR)); 
		else {
			model.setScale(scaleX, scaleY);
			myDragImage.setScale(scaleX, scaleY);
		}
		double angle = getInput(5, fieldList);
		model.setAngle(angle);
		myDragImage.setAngle(angle);
		double transparency = getInput(6, fieldList);
		if (transparency < 0 || transparency > 1) {
			throw new InvalidConfigureException(myResources.getString(TRANSPARENCY_ERROR));
		}
		else {
			model.setAlpha(transparency);
			myDragImage.setAlpha(transparency);
		}
		RadioButton visibilityButton = configure.getVisiblity();
		model.setVisibility(visibilityButton.isSelected());
		myDragImage.setVisibility(visibilityButton.isSelected());
		configure.close();
		myDrawFunction.accept(null);
	}

	
	//TODO fix this call, used to redraw canvas and pass to room controller upon deleting instance
	public ConfigureView getConfigureView() {
		return configure;
	}
	
	private double getInput(int n, List<TextField> fieldList) {
		return Double.parseDouble(fieldList.get(n).getText());
	}
	
	private void setInput(int n, List<TextField> fieldList, double doubleToSet) {
		fieldList.get(n).setText(Double.toString(doubleToSet));
	}
}

@@ -1669,31 +1669,33 @@
 ;%0A%09%09
-setInput(0, fieldList, 
+Double%5B%5D doublesToSet = %7B
 myDa
@@ -1724,34 +1724,8 @@
 ().x
-);%0A%09%09setInput(1, fieldList
 , my
@@ -1756,34 +1756,8 @@
 ().y
-);%0A%09%09setInput(2, fieldList
 , my
@@ -1793,34 +1793,8 @@
 ty()
-);%0A%09%09setInput(3, fieldList
 , my
@@ -1821,34 +1821,8 @@
 eX()
-);%0A%09%09setInput(4, fieldList
 , my
@@ -1849,34 +1849,35 @@
 eY()
-);%0A%09%09setInput(5, fieldList
+, myDataInstance.getAngle()
 , my
@@ -1889,31 +1889,31 @@
 nstance.getA
-ngle())
+lpha()%7D
 ;%0A%09%09setInput
@@ -1905,33 +1905,33 @@
 ()%7D;%0A%09%09setInput(
-6
+7
 , fieldList, myD
@@ -1931,33 +1931,20 @@
 st, 
-myDataInstance.getAlpha()
+doublesToSet
 );%0A%09
@@ -3890,21 +3890,24 @@
 st, 
-d
+D
 ouble
+%5B%5D
  double
+s
 ToSe
@@ -3911,16 +3911,48 @@
 oSet) %7B%0A
+%09%09for (int i = 0; i%3C n; i++) %7B%0A%09
 %09%09fieldL
@@ -3959,17 +3959,17 @@
 ist.get(
-n
+i
 ).setTex
@@ -3996,16 +3996,24 @@
 uble
+s
 ToSet
-));
+%5Bi%5D));%0A%09%09%7D
 %0A%09%7D%0A

Refactor configure controller