package scheduler.view;

import java.io.IOException;

import scheduler.Main;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class PrefInstructorsController 
{
    @FXML
    private ListView<String> instructors;
    @FXML
    private Button editTimePrefs;
    @FXML
    private ListView<String> instructorCourses;
    
    private Main mainApp;
    private ObservableList<String> courses;
    private static final String defaultMsg = "double-click to select a course";
    

    /**
     * The constructor.
     * The constructor is called before the initialize() method.
     */
    public PrefInstructorsController() {
    }
    
    /**
     * 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
       this.courses = this.mainApp.getCourseData();
       instructorCourses.setCellFactory(ComboBoxListCell.forListView(this.courses));
       instructors.setItems(mainApp.getInstructorData());
    }

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {        
        instructorCourses.setEditable(true);
        instructorCourses.setItems(FXCollections.observableArrayList(defaultMsg));
    }
    
    /**
     * Adds another course box.
     */
    @FXML
    private void addCourse() {
        instructorCourses.getItems().add(defaultMsg);
    }
    
    /**
     * Deletes a course.
     */
    @FXML
    private void removeCourse() {
        if (!instructorCourses.getSelectionModel().isEmpty()) {
            instructorCourses.getItems().remove(instructorCourses.getSelectionModel().getSelectedIndex());
        }
    }    
    
    /**
     * 
     */
    @FXML
    private void openTeachingPrefs() {
        try {
            // Load the fxml file and create a new stage for the popup dialog.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/TeachingTimesPrefs.fxml"));
            VBox page = (VBox) loader.load();

            // Create the dialog Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Edit Time Preferences");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            

            // Show the dialog and wait until the user closes it
            dialogStage.showAndWait();
            
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}