package scheduler.view;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import scheduler.Main;

public class ResourcesInstructorsController {
   // Reference to the main application.
   protected Main mainApp;
   
   @FXML
   private AnchorPane rootPane; // this is my root element in FXML
   
   @FXML
   private TextField officeTextField;
   
   @FXML
   private TextField phoneTextField;
   
   @FXML
   private TextField lastNameTextField;
   
   @FXML
   private TextField firstNameTextField;
   
   @FXML
   private TextField emailTextField;
   
   @FXML
   private TextField emplTextField;
   
   @FXML
   private Button addButton;
   
   @FXML
   private Button removeButton;
   
   @FXML
   private Button cancelButton;
   
   @FXML
   private Button finishButton;
   
   @FXML
   private Button uploadButton;
   
   @FXML
   private ImageView photoImageView;
   
   @FXML
   private ListView<String> instructorListView;
   
   /**
    * Initializes the controller class. This method is automatically called
    * after the fxml file has been loaded.
    */
   @FXML
   private void initialize() {
      // Handle First Name TextField text changes.
      firstNameTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("First Name Changed (newValue: " + newValue + ")");
      });
      
      // Handle Last Name TextField text changes.
      lastNameTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Last Name Changed (newValue: " + newValue + ")");
      });
      
      // Handle EmplID TextField text changes.
      emplTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Employee ID Changed (newValue: " + newValue + ")");
      });
      
      // Handle Office TextField text changes.
      officeTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Office Changed (newValue: " + newValue + ")");
      });
      
      // Handle Phone TextField text changes.
      phoneTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Phone Number Changed (newValue: " + newValue + ")");
      });
      
      // Handle Email TextField text changes.
      emailTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Email Changed (newValue: " + newValue + ")");
      });
      
      FileChooser fileChooser = new FileChooser();
      uploadButton.setOnAction(new EventHandler<ActionEvent>() {
         @Override
         public void handle(final ActionEvent e) {
            fileChooser.showOpenDialog(rootPane.getScene().getWindow());
            System.out.println("Upload image dialog opened");
         }
      });
      
      // Handle Finish Button event.
      finishButton.setOnAction((event) -> {
         System.out.println("Finish Button pressed");
      });
      
      // Handle Cancel Button event.
      cancelButton.setOnAction((event) -> {
         System.out.println("Cancel Button pressed");
      });
      
      // Handle Add Button event.
      addButton.setOnAction((event) -> {
         System.out.println("Add Button pressed");
      });
      
      // Handle Remove Button event.
      removeButton.setOnAction((event) -> {
         System.out.println("Remove Button pressed");
      });
   }
   
   /**
    * Is called by the main application to give a reference back to itself.
    * 
    * @param mainApp
    */
   public void setMainApp(Main mainApp) {
      this.mainApp = mainApp;
      
      // Add observable list data to the table
      instructorListView.setItems(mainApp.getInstructorData());
   }
}