package scheduler.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import scheduler.Main;

public class ResourcesRoomsController {
   // Reference to the main application.
   protected Main mainApp;
   
   @FXML
   private TextField buildingNumberTextField;
   
   @FXML
   private TextField capacityTextField;
   
   @FXML
   private TextField buildingNameTextField;
   
   @FXML
   private TextField roomNumberTextField;
   
   @FXML
   private TextField notesTextField;
   
   @FXML
   private CheckBox labCheckBox;
   
   @FXML
   private CheckBox compLabCheckBox;
   
   @FXML
   private Button addButton;
   
   @FXML
   private CheckBox lectureCheckBox;
   
   @FXML
   private Button finishButton;
   
   @FXML
   private Button cancelButton;
   
   @FXML
   private Button removeButton;
   
   @FXML
   private ComboBox<String> equipmentMenu;
   private ObservableList<String> equipmentMenuData = FXCollections.observableArrayList();
   
   @FXML
   private ListView<String> roomListView;
   
   public ResourcesRoomsController() {
      equipmentMenuData.add("Smartroom");
      equipmentMenuData.add("Overhead Projector");
      equipmentMenuData.add("Laptop Projection");
   }
   
   /**
    * Initializes the controller class. This method is automatically called
    * after the fxml file has been loaded.
    */
   @FXML
   private void initialize() {
      // Handle Building Name TextField text changes.
      buildingNameTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Building Name Changed (newValue: " + newValue + ")");
      });
      
      // Handle Building Number TextField text changes.
      buildingNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Building Number Changed (newValue: " + newValue + ")");
      });
      
      // Handle Room Number TextField text changes.
      roomNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Course Title Changed (newValue: " + newValue + ")");
      });
      
      // Handle Capacity text changes.
      capacityTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Room Capacity Changed (newValue: " + newValue + ")");
      });
      
      // Handle Lecture Class Type CheckBox event.
      lectureCheckBox.setOnAction((event) -> {
         boolean selected = lectureCheckBox.isSelected();
         System.out.println("Lecture Class Type Action (selected: " + selected + ")");
      });
      
      // Lab Class Type CheckBox event.
      labCheckBox.setOnAction((event) -> {
         boolean selected = labCheckBox.isSelected();
         System.out.println("Lab Class Type Action (selected: " + selected + ")");
      });
      
      // Init equipment menu
      equipmentMenu.setItems(equipmentMenuData);
      
      // Define rendering of the list of values in ComboBox drop down.
      equipmentMenu.setCellFactory((comboBox) -> {
         return new ListCell<String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
               super.updateItem(item, empty);
               
               if (item == null || empty) {
                  setText(null);
               }
               else {
                  setText(item);
               }
            }
         };
      });
      
      // Handle ComboBox event.
      equipmentMenu.setOnAction((event) -> {
         String selectedEquipment = equipmentMenu.getSelectionModel().getSelectedItem();
         System.out.println("Equipment Menu Action (selected: " + selectedEquipment + ")");
      });
      
      // Handle Comp Lab CheckBox event.
      compLabCheckBox.setOnAction((event) -> {
         boolean selected = compLabCheckBox.isSelected();
         System.out.println("Computer Lab Required Action (selected: " + selected + ")");
      });
      
      // Handle Capacity TextField text changes.
      capacityTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Class Capacity Changed (newValue: " + newValue + ")");
      });
      
      // Handle Notes TextField text changes.
      notesTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Notes Changed (newValue: " + newValue + ")");
      });
      
      // 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
      roomListView.setItems(mainApp.getRoomData());
   }
}