package scheduler.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
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 ResourcesCoursesController {
   // Reference to the main application.
   protected Main mainApp;
   
   @FXML
   private TextField courseNumberTextField;
   
   @FXML
   private TextField capacityTextField;
   
   @FXML
   private TextField unitsTextField;
   
   @FXML
   private TextField notesTextField;
   
   @FXML
   private TextField wtuTextField;
   
   @FXML
   private TextField subjectTextField;
   
   @FXML
   private TextField courseTitleTextField;
   
   @FXML
   private Button increaseWTUButton;
   
   @FXML
   private Button addButton;
   
   @FXML
   private Button finishButton;
   
   @FXML
   private Button increaseUnitsButton;
   
   @FXML
   private Button cancelButton;
   
   @FXML
   private Button decreaseWTUButton;
   
   @FXML
   private Button removeButton;
   
   @FXML
   private Button decreaseUnitsButton;
   
   @FXML
   private CheckBox labCheckBox;
   
   @FXML
   private CheckBox compLabCheckBox;
   
   @FXML
   private CheckBox lectureCheckBox;
   
   @FXML
   private CheckBox seminarCheckBox;
   
   @FXML
   private CheckBox activityCheckBox;
   
   @FXML
   private ComboBox<String> corequisiteMenu;
   private ObservableList<String> corequisiteMenuData = FXCollections.observableArrayList();
   
   @FXML
   private ListView<String> courseListView;
   
   @FXML
   private ComboBox<String> equipmentMenu;
   private ObservableList<String> equipmentMenuData = FXCollections.observableArrayList();
   
   public ResourcesCoursesController() {
      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 Subject TextField text changes.
      subjectTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Subject Changed (newValue: " + newValue + ")");
      });
      
      // Handle Course Number TextField text changes.
      courseNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Course Number Changed (newValue: " + newValue + ")");
      });
      
      // Handle Course Title TextField text changes.
      courseTitleTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Course Title Changed (newValue: " + newValue + ")");
      });
      
      // Handle Units TextField text changes.
      unitsTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("Units Changed (newValue: " + newValue + ")");
      });
      
      // Handle Units Increase Button event.
      increaseUnitsButton
            .setOnAction((event) -> {
               System.out.println("Increase Units Button pressed");
               if (unitsTextField.getText().equals("")) {
                  unitsTextField.setText("1");
               }
               else {
                  unitsTextField.setText(Integer.toString(Integer.parseInt(unitsTextField.getText()) + 1));
               }
            });
      
      // Handle Units Decrease Button event.
      decreaseUnitsButton
            .setOnAction((event) -> {
               System.out.println("Decrease Units Button pressed");
               if (unitsTextField.getText().equals("") || unitsTextField.getText().equals("0")) {
                  unitsTextField.setText("0");
               }
               else {
                  unitsTextField.setText(Integer.toString(Integer.parseInt(unitsTextField.getText()) - 1));
               }
            });
      
      // Handle WTU TextField text changes.
      wtuTextField.textProperty().addListener((observable, oldValue, newValue) -> {
         System.out.println("WTU Changed (newValue: " + newValue + ")");
      });
      
      // Handle WTU Increase Button event.
      increaseWTUButton.setOnAction((event) -> {
         System.out.println("Increase WTU Button pressed");
         if (wtuTextField.getText().equals("")) {
            wtuTextField.setText("1");
         }
         else {
            wtuTextField.setText(Integer.toString(Integer.parseInt(wtuTextField.getText()) + 1));
         }
      });
      
      // Handle WTU Decrease Button event.
      decreaseWTUButton.setOnAction((event) -> {
         System.out.println("Decrease WTU Button pressed");
         if (wtuTextField.getText().equals("") || wtuTextField.getText().equals("0")) {
            wtuTextField.setText("0");
         }
         else {
            wtuTextField.setText(Integer.toString(Integer.parseInt(wtuTextField.getText()) - 1));
         }
      });
      
      // 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 + ")");
      });
      
      // Activity Class Type CheckBox event.
      activityCheckBox.setOnAction((event) -> {
         boolean selected = activityCheckBox.isSelected();
         System.out.println("Activity Class Type Action (selected: " + selected + ")");
      });
      
      // Seminar Class Type CheckBox event.
      seminarCheckBox.setOnAction((event) -> {
         boolean selected = seminarCheckBox.isSelected();
         System.out.println("Seminar 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 + ")");
      });
      
      // Init equipment menu
      
      // Define rendering of the list of values in ComboBox drop down.
      corequisiteMenu.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.
      corequisiteMenu.setOnAction((event) -> {
         String coreq = corequisiteMenu.getSelectionModel().getSelectedItem();
         System.out.println("Corequisite Action (selected: " + coreq + ")");
      });
      
      // 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");
      });
   }
   
   @FXML
   private void subjectChanged(ActionEvent event) {
      // Button was clicked, do something...
      System.out.println("Button Action\n");
   }
   
   /**
    * 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
      courseListView.setItems(mainApp.getCourseData());
      corequisiteMenu.setItems(mainApp.getCourseData());
   }
}