package objects;

import java.util.List;

import com.google.common.collect.Lists;

import controllers.DatabaseContoller;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.control.Button;
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;
	String startType;
	int raceNumber;
	String trackName;

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

	public UpdateRaceRow(int raceId, String raceDate, String raceType, String startType, int raceNumber, String trackName) {
		this.raceDate = raceDate;
		this.raceId = raceId;
		this.startType = startType;
		this.raceType = raceType;
		this.raceNumber = raceNumber;
		this.trackName = trackName;

		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);
		startTypeTextField = new TextField(startType);
		raceNumberTextField = new TextField(String.valueOf(raceNumber));
		trackNameTextField = new TextField(trackName);

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

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

		return returnValue;
	}

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

		@Override
		public void handle(javafx.event.ActionEvent arg0) {
			if (!startTypeTextField.getText().equals(startType) ||
					!raceTypeTextField.getText().equals(raceType)) {
				try {
					final int raceIdInt = Integer.parseInt(raceIdTextField.getText());
					final int raceNumberInt = Integer.parseInt(raceNumberTextField.getText());

					DatabaseContoller.getInstance().updateRaceInfo(raceIdInt, startTypeTextField.getText(), raceTypeTextField.getText(), raceNumberInt);

					startType = startTypeTextField.getText();
					raceType = raceTypeTextField.getText();
				} catch (final NumberFormatException e) {
					System.err.print("Cant parse " + raceIdTextField.getText() + " as integer");
				}
			}
		}
	}
}
