package controllers;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import model.Ride;
import model.SceneCode;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by Gondr on 4/04/2017.
 */
public class AddRideController extends Controller{
    @FXML
    TextField model;
    @FXML
    TextField colour;
    @FXML
    TextField license;
    @FXML
    TextField year;
    @FXML
    TextField numSeats;

    public void addRide() throws Exception {
        //check for invalid data
        String failure = "";
        if (model.getText().length() == 0){
            failure += "Mandatory Field model is not entered.\n";
        }
        if (colour.getText().length() == 0){
            failure += "Mandatory Field colour is not entered.\n";
        }
        if (license.getText().length() == 0){
            failure += "Mandatory Field license is not entered.\n";
        }
        if (year.getText().length() == 0){
            failure += "Mandatory Field year is not entered.\n";
        }
        int numYear = 0;
        try{
            numYear = Integer.parseInt(year.getText());
            //TODO other checks here like the year cannot be too old, cannot be more than this year etc
        }catch (Exception e){
            failure += "Year must be a Integer.\n";
        }
        if (numSeats.getText().length() == 0){
            failure += "Mandatory Field number of seats is not entered.\n";
        }
        int seats = 0;
        try{
            seats = Integer.parseInt(numSeats.getText());
            //TODO other checks here like the year cannot be too old, cannot be more than this year etc
        }catch (Exception e){
            failure += "Seats must be a Integer.\n";
        }
        if (failure == ""){
            //pass
            Ride ride = new Ride(model.getText(), colour.getText(), license.getText(), numYear, seats);
            parent.getSession().getDataManager().addRides(ride);
            changeScene(SceneCode.MY_RIDES);
        }else{
            popUp(Alert.AlertType.WARNING, "Unable to Add Ride!", "Invalid Data", failure);
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
