package database;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author jroll
 */
public class InstructorsListController implements Initializable {

    Stage stage;

    @FXML
    TableView tableView;
    @FXML
    TableColumn columnName;
    @FXML
    TableColumn columnTitle;
    @FXML
    TableColumn columnAlias;
    @FXML
    TableColumn columnOffice;
    @FXML
    TableColumn columnPhone;
    @FXML
    TableColumn columnWtus;
    @FXML
    TableColumn columnEmplID;
    @FXML
    TableColumn columnNotes;

    @FXML
    private Button backButton;
    @FXML
    private Button addButton;
    @FXML
    private Button removeButton;
    @FXML
    private Button editButton;

    public class Record {

        private SimpleStringProperty fieldName;
        private SimpleStringProperty fieldTitle;
        private SimpleStringProperty fieldAlias;
        private SimpleStringProperty fieldOffice;
        private SimpleStringProperty fieldPhone;
        private SimpleStringProperty fieldWtus;
        private SimpleStringProperty fieldEmplID;
        private SimpleStringProperty fieldNotes;

        Record(String fieldName, String fieldTitle, String fieldAlias, String fieldOffice, String fieldPhone, String fieldWtus, String fieldEmplID, String fieldNotes) {
            this.fieldName = new SimpleStringProperty(fieldName);
            this.fieldTitle = new SimpleStringProperty(fieldTitle);
            this.fieldAlias = new SimpleStringProperty(fieldAlias);
            this.fieldOffice = new SimpleStringProperty(fieldOffice);
            this.fieldPhone = new SimpleStringProperty(fieldPhone);
            this.fieldWtus = new SimpleStringProperty(fieldWtus);
            this.fieldEmplID = new SimpleStringProperty(fieldEmplID);
            this.fieldNotes = new SimpleStringProperty(fieldNotes);

        }

        /**
         * @return the fieldName
         */
        public String getFieldName() {
            return fieldName.get();
        }

        /**
         * @return the fieldTitle
         */
        public String getFieldTitle() {
            return fieldTitle.get();
        }

        /**
         * @return the fieldAlias
         */
        public String getFieldAlias() {
            return fieldAlias.get();
        }

        /**
         * @return the fieldOffice
         */
        public String getFieldOffice() {
            return fieldOffice.get();
        }

        /**
         * @return the fieldPhone
         */
        public String getFieldPhone() {
            return fieldPhone.get();
        }

        /**
         * @return the fieldWtus
         */
        public String getFieldWtus() {
            return fieldWtus.get();
        }

        /**
         * @return the fieldEmplID
         */
        public String getFieldEmplID() {
            return fieldEmplID.get();
        }

        /**
         * @return the fieldNotes
         */
        public String getFieldNotes() {
            return fieldNotes.get();
        }

    }

    private ObservableList<Record> dataList
            = FXCollections.observableArrayList(
                    new Record("Armstrong, Brett Taylor", "Professor", "btarmst", "014-0240", "805-756-2824", "0", "006543255", "On sabbatical"),
                    new Record("Dalbey, John", "Lecturer", "jdalbey", "014-0203", "805-756-2921", "12", "006543265", "On sabbatical"),
                    new Record("Fisher, Gene L", "Professor", "gfisher", "014-0210", "805-756-2416", "10", "006543276", ""),
                    new Record("Janzen, David S", "Professor", "djanzen", "014-0212", "805-756-2929", "10", "003465255", ""),
                    new Record("Mammen, Kurt", "Lecturer", "kmammen", "014-0206", "805-756-1332", "8", "005962849", ""),
                    new Record("Seng, John S", "Professor", "jseng", "014-0231", "805-756-5532", "10", "005698442", ""),
                    new Record("Armstrong, Brett Taylor", "Professor", "btarmst", "014-0240", "805-756-2824", "0", "006543255", ""),
                    new Record("Staley, Clinton A", "Professor", "cstaley", "014-0204", "805-756-2158", "0", "005783466", ""));

    /**
     * Initializes the controller class.
     */
    public void setStage(Stage stage) {
        this.stage = stage;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

        columnName.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldName"));

        columnTitle.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldTitle"));

        columnAlias.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldAlias"));

        columnOffice.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldOffice"));

        columnPhone.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldPhone"));

        columnEmplID.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldEmplID"));

        columnNotes.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldNotes"));

        tableView.setItems(dataList);
    }

    @FXML
    private void backButtonClicked(ActionEvent event) {
        stage.close();
    }

    @FXML
    private void addButtonClicked(ActionEvent event
    ) {
        try {
            final FXMLLoader loader = new FXMLLoader(getClass().getResource("add-instructor-view.fxml"));
            final Parent root = (Parent) loader.load();

            final AddInstructorController controller = loader.<AddInstructorController>getController();
            Scene scene = new Scene(root);
            Stage stage = new Stage();
            controller.setStage(stage);

            stage.setScene(scene);
            stage.show();

            // Hide the current screen
            //((Node)(event.getSource())).getScene().getWindow().hide();
        } catch (IOException ex) {
            Logger.getLogger(ChooseBuildingController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    @FXML
    public void addInstructor(String firstName, String lastName,String fieldTitle, String fieldAlias, 
            String fieldOffice, String fieldPhone, String fieldWtus, String fieldEmplID, 
            String fieldNotes) {
        String fieldName = lastName + ", " + firstName;
        this.dataList.add(new Record(fieldName, fieldTitle, fieldAlias, fieldOffice,
         fieldPhone, fieldWtus, fieldEmplID, fieldNotes));

        tableView.getItems().clear();
        tableView.setItems(this.dataList);
    }
    
    @FXML
    private void removeButtonClicked(ActionEvent event
    ) {
        try {
            final FXMLLoader loader = new FXMLLoader(getClass().getResource("remove-instructor-dialog.fxml"));
            final Parent root = (Parent) loader.load();

            final RemoveInstructorController controller = loader.<RemoveInstructorController>getController();
            Scene scene = new Scene(root);
            Stage stage = new Stage();
            controller.setStage(stage);

            stage.setScene(scene);
            stage.show();

            // Hide the current screen
            //((Node)(event.getSource())).getScene().getWindow().hide();
        } catch (IOException ex) {
            Logger.getLogger(ChooseBuildingController.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    @FXML
    private void editButtonClicked(ActionEvent event) {
        try {
             TableView.TableViewSelectionModel selectionModel = tableView.getSelectionModel();
             ObservableList selectedCells = selectionModel.getSelectedCells();
                if (selectedCells.size() > 0) {
                    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
                    int row = tablePosition.getRow();
                    ObservableList<InstructorsListController.Record> records = tableView.getItems();
                    InstructorsListController.Record record = records.get(row);
                    System.out.println(record);
                    
                    final FXMLLoader loader = new FXMLLoader(getClass().getResource("edit-instructor-popup.fxml"));
                    final Parent root = (Parent) loader.load();

                    final EditInstructorController controller = loader.<EditInstructorController>getController();

            
                    Scene scene = new Scene(root);
                    System.out.println("in InstructorsListController, " + record);
                    controller.setInstructor(record);
                    Stage stage = new Stage();
                    controller.setStage(stage);

                    stage.setScene(scene);
                    stage.show();
                }
            
            
            

            // Hide the current screen
            //((Node)(event.getSource())).getScene().getWindow().hide();
        } catch (IOException ex) {
            Logger.getLogger(InstructorsListController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}