package org.treytomes.contacts.controller;

import org.treytomes.contacts.model.Person;
import org.treytomes.contacts.util.CalendarUtil;

import javafx.fxml.FXML;
import javafx.scene.control.Dialogs;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * Dialog to edit details of a person.
 * 
 * @author ttomes
 *
 */
public class PersonEditController {
	
	@FXML
	private TextField _firstNameField;
	
	@FXML
	private TextField _lastNameField;
	
	@FXML
	private TextField _streetField;
	
	@FXML
	private TextField _postalCodeField;
	
	@FXML
	private TextField _cityField;
	
	@FXML
	private TextField _birthdayField;
	
	private Stage _dialogStage;
	private Person _person;
	private boolean _okClicked;
	
	public PersonEditController() {
		_okClicked = false;
	}
	
	/**
	 * Initializes the controller class.
	 * This method is automatically called after the fxml file has been loaded.
	 */
	@FXML
	private void initialize() {
	}
	
	/**
	 * Sets the stage of this dialog.
	 * 
	 * @param dialogStage The stage of this dialog.
	 */
	public void setDialogStage(Stage dialogStage) {
		_dialogStage = dialogStage;
	}
	
	/**
	 * Sets the person to be edited in the dialog.
	 * 
	 * @param person The person to be edited in the dialog.
	 */
	public void setPerson(Person person) {
		_person = person;
		
		_firstNameField.setText(_person.getFirstName());
		_lastNameField.setText(_person.getLastName());
		_streetField.setText(_person.getStreet());
		_postalCodeField.setText(Integer.toString(_person.getPostalCode()));
		_cityField.setText(_person.getCity());
		_birthdayField.setText(CalendarUtil.format(_person.getBirthday()));
		_birthdayField.setPromptText("yyyy-mm-dd");
	}
	
	/**
	 * Returns true if the user clicked ok; false otherwise.
	 * 
	 * @return True if the user clicked ok; false otherwise.
	 */
	public boolean isOKClicked() {
		return _okClicked;
	}
	
	/**
	 * Called when the user clicks the OK button.
	 */
	@FXML
	private void handleOk() {
		if (isInputValid()) {
			_person.setFirstName(_firstNameField.getText());
			_person.setLastName(_lastNameField.getText());
			_person.setStreet(_streetField.getText());
			_person.setPostalCode(Integer.parseInt(_postalCodeField.getText()));
			_person.setCity(_cityField.getText());
			_person.setBirthday(CalendarUtil.parse(_birthdayField.getText()));
			
			_okClicked = true;
			_dialogStage.close();
		}
	}
	
	/**
	 * Called when the user clicks the Cancel button.
	 */
	@FXML
	private void handleCancel() {
		_dialogStage.close();
	}
	
	/**
	 * VAlidates the user input in the text fields.
	 * 
	 * @return True if the input is valid; false otherwise.
	 */
	private boolean isInputValid() {
		String errorMessage = "";
		
		if ((_firstNameField.getText() == null) || (_firstNameField.getText().length() == 0)) {
			errorMessage += "No valid first name!\n";
		}
		
		if ((_lastNameField.getText() == null) || (_lastNameField.getText().length() == 0)) {
			errorMessage += "No valid last name!\n";
		}
		
		if ((_streetField.getText() == null) || (_streetField.getText().length() == 0)) {
			errorMessage += "No valid street!\n";
		}
		
		if ((_postalCodeField.getText() == null) || (_postalCodeField.getText().length() == 0)) {
			errorMessage += "No valid postal code!\n";
		} else {
			// Try to parse the postal code into an integer.
			try {
				Integer.parseInt(_postalCodeField.getText());
			} catch (NumberFormatException e) {
				errorMessage += "No valid postal code (must be an integer)!\n";
			}
		}
		
		if ((_cityField.getText() == null) || (_cityField.getText().length() == 0)) {
			errorMessage += "No valid city!\n";
		}
		
		if ((_birthdayField.getText() == null) || (_birthdayField.getText().length() == 0)) {
			errorMessage += "No valid birthday!\n";
		} else {
			if (!CalendarUtil.validString(_birthdayField.getText())) {
				errorMessage += "No valid birthday.  Use the format yyyy-mm-dd!\n";
			}
		}
		
		if (errorMessage.length() == 0) {
			return true;
		} else {
			// Show the error message.
			Dialogs.showErrorDialog(_dialogStage, errorMessage, "Please correct the invalid fields.", "Invalid Fields");
			return false;
		}
	}
}