package objects;

import java.util.List;

import com.google.common.collect.Lists;

import controllers.DatabaseController;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;

public class UpdateRaceRow {

	int raceId;
	String raceDate;
	List<String> raceTypes = Lists.newArrayList("DD", "LD", "V75", "V65");
	String raceType;
	boolean isAutostartBool;
	int raceNumber;
	String trackName;
	int trackId;

	Button applyButton = new Button("Apply changes");
	private TextField raceIdTextField;
	private TextField raceDateTextField;
	private TextField raceTypeTextField;
	private CheckBox isAutostart;
	private TextField raceNumberTextField;
	private TextField trackNameTextField;

	private TextField autoStartText;
	private TextField buttonTextField;

	public UpdateRaceRow(int raceId, String raceDate, String raceType, boolean isAutostart, int raceNumber, String trackName, int trackId) {
		this.raceDate = raceDate;
		this.raceId = raceId;
		this.isAutostartBool = isAutostart;
		this.raceType = raceType;
		this.raceNumber = raceNumber;
		this.trackName = trackName;
		this.trackId = trackId;

		applyButton.setOnAction(new ApplyAction());
	}

	public FlowPane getGui(boolean isHeader) {
		final FlowPane returnValue = new FlowPane(Orientation.HORIZONTAL);
		returnValue.setDisable(false);
		returnValue.setVisible(true);
		returnValue.setPrefWrapLength(4000);

		raceIdTextField = new TextField(String.valueOf(raceId));
		raceDateTextField = new TextField(raceDate);
		raceTypeTextField = new TextField(raceType);
		isAutostart = new CheckBox();
		raceNumberTextField = new TextField(String.valueOf(raceNumber));
		trackNameTextField = new TextField(trackName);

		autoStartText = new TextField("AutoStart?");
		buttonTextField = new TextField("");

		autoStartText.setDisable(true);
		buttonTextField.setDisable(true);

		if (isHeader) {
			raceIdTextField.setText("Race Id");
			raceNumberTextField.setText("Race Number");
			raceIdTextField.setDisable(true);
			raceDateTextField.setDisable(true);
			raceTypeTextField.setDisable(true);
			isAutostart.setDisable(true);
			raceNumberTextField.setDisable(true);
			trackNameTextField.setDisable(true);
			applyButton.setDisable(true);
		}
		trackNameTextField.setEditable(false);
		raceDateTextField.setEditable(false);
		raceNumberTextField.setEditable(false);

		isAutostart.setSelected(isAutostartBool);

		returnValue.getChildren().add(trackNameTextField);
		returnValue.getChildren().add(raceIdTextField);
		returnValue.getChildren().add(raceDateTextField);
		returnValue.getChildren().add(raceNumberTextField);
		returnValue.getChildren().add(raceTypeTextField);

		if (isHeader) {
			returnValue.getChildren().add(autoStartText);
			returnValue.getChildren().add(buttonTextField);
		} else {
			returnValue.getChildren().add(isAutostart);
			returnValue.getChildren().add(applyButton);
			isAutostart.setPrefWidth(autoStartText.getPrefWidth());
		}

		return returnValue;
	}

	private class ApplyAction implements EventHandler<javafx.event.ActionEvent> {

		@Override
		public void handle(javafx.event.ActionEvent arg0) {
			try {
				final int raceIdInt = Integer.parseInt(raceIdTextField.getText());
				final int raceNumberInt = Integer.parseInt(raceNumberTextField.getText());

				DatabaseController.getInstance().updateRaceInfo(raceIdInt, isAutostart.isSelected(), raceTypeTextField.getText(), raceNumberInt, raceDate, trackId);

				raceType = raceTypeTextField.getText();
			} catch (final NumberFormatException e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}
}
