package controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import model.Driver;
import model.SceneCode;
import model.Session;

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

/**
 * Created by Gondr on 1/06/2017.
 */
public class RegisterDriverController extends Controller {

    private Image photoStored;
    private int studentNumber;
    private String email;
    private String password;
    private String address;
    private String homephone;
    private String mobilephone;
    private String accountType;
    @FXML
    private ComboBox<String> licenseType;
    @FXML
    private TextField number;
    @FXML
    private DatePicker issue;
    @FXML
    private DatePicker expiry;

    public void setUserInformation(int studentNumber, String email, String password, String address, String homephone, String mobilephone, String accountType, Image photoStored){
        this.studentNumber = studentNumber;
        this.email = email;
        this.password = password;
        this.address = address;
        this.homephone = homephone;
        this.mobilephone = mobilephone;
        this.accountType = accountType;
        this.photoStored = photoStored;
    }

    public static String validateLicense(String ln){
        String failure = "";
         if (ln.length() < 6){
             failure += "Your License must be at least 6 letters long.\n";
         }
         return failure;
    }

    public static String validateIssue(LocalDate d){
        String failure = "";
        if (d == null){
            failure += "You must set a date of issue.\n";
        }else {
            if (LocalDate.now().isBefore(d)) {
                failure += "You cannot have a issue date earlier than today.\n";
            }
        }
        return failure;
    }

    public static String validateExpire(LocalDate e, LocalDate i){
        String failure = "";
        if (e == null){
            failure += "You must set a date of expiry.\n";
        } else {
            if (i != null) {
                if (e.isBefore(i)) {
                    failure += "You cannot have your expiry date before your date of issue.\n";
                }
            }
        }
        return failure;
    }

    public boolean validate(){
        String failure = "";
        failure += validateLicense(number.getText());
        failure += validateIssue(issue.getValue());
        failure += validateExpire(expiry.getValue(), issue.getValue());
        if (failure.equals("")){
            return true;
        }else{
            popUp(Alert.AlertType.WARNING, "Error!", "Your details are not valid for the following reasons.", failure);
            return false;
        }
    }

    public void createAccount(){
        if (validate()){
            Driver.LicenseType lc = Driver.LicenseType.getValueOf(licenseType.getValue());
            Driver driver = new Driver(studentNumber, email, password, address,homephone, mobilephone, photoStored, lc, number.getText(), issue.getValue(), expiry.getValue());
            if (Session.session.getDataManager().addDriver(driver)){
                popUp(Alert.AlertType.CONFIRMATION, "Success!", "Your Account has been made.", "You can now log in and will be redirected the the title screen after.");
                try {
                    changeScene(SceneCode.MAIN);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                popUp(Alert.AlertType.WARNING, "Error!", "There is already an passenger with this id.", "You will be redirected to the homescreen to log in.");
                try {
                    changeScene(SceneCode.MAIN);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<String> licenseTypes = FXCollections.observableArrayList();
        licenseTypes.addAll(Driver.LicenseType.RESTRICTED.name, Driver.LicenseType.FULL.name, Driver.LicenseType.FULL2YEARS.name);
        licenseType.setItems(licenseTypes);
        licenseType.getSelectionModel().select(0);
    }
}
