package schedule;/*
 * 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.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

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

    Stage stage;
    ScreensController mainContainer;

    @FXML
    private Button addButton;
    @FXML
    private Button backButton;
    @FXML
    private Button removeButton;
    @FXML
    private Button editButton;
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn columnName;
    @FXML
    private TableColumn columnQuarter;
    @FXML
    private TableColumn columnYear;
    @FXML
    private TableColumn columnLastEdited;
    @FXML
    private TableColumn columnPublished;

    public class Record {

        private SimpleStringProperty fieldName;
        private SimpleStringProperty fieldQuarter;
        private SimpleStringProperty fieldYear;
        private SimpleStringProperty fieldLastEdited;
        private SimpleStringProperty fieldPublished;

        Record(String fieldName, String fieldQuarter, String fieldYear, String fieldLastEdited, String fieldPublished) {
            this.fieldName = new SimpleStringProperty(fieldName);
            this.fieldQuarter = new SimpleStringProperty(fieldQuarter);
            this.fieldYear = new SimpleStringProperty(fieldYear);
            this.fieldLastEdited = new SimpleStringProperty(fieldLastEdited);
            this.fieldPublished = new SimpleStringProperty(fieldPublished);
        }

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

        /**
         * @return the fieldQuarter
         */
        public String getFieldQuarter() {
            return fieldQuarter.get();
        }

        /**
         * @return the fieldYear
         */
        public String getFieldYear() {
            return fieldYear.get();
        }

        /**
         * @return the fieldLastEdited
         */
        public String getFieldLastEdited() {
            return fieldLastEdited.get();
        }

        /**
         * @return the fieldPublished
         */
        public String getFieldPublished() {
            return fieldPublished.get();
        }

    }

    private ObservableList<Record> dataList
            = FXCollections.observableArrayList(
                    new Record("Untitled Schedule", "Spring", "2014", "2013-12-01", "Unpublished"),
                    new Record("Winter 2014", "Winter", "2014", "2013-12-01", "Published"),
                    new Record("Fall 2013", "Fall", "2013", "2012-12-01", "Published"),
                    new Record("Spring 2013", "Spring", "2013", "2011-11-01", "Published"),
                    new Record("Winter 2013", "Winter", "2013", "2011-11-01", "Published"),
                    new Record("Fall 2012", "Fall", "2012", "2011-01-01", "Published"));


    public void setStage(Stage stage) {
        this.stage = stage;
    }

    /**
     * Initializes the controller class.
     */
    @FXML
    protected void editButtonClicked(ActionEvent event) {
        ScreensController mainContainer = new ScreensController();
        mainContainer.loadScreen(Framework.EDIT_SCHEDULE, Framework.EDIT_SCHEDULE_FXML);
        mainContainer.loadScreen(Framework.COURSE_OVERLAP, Framework.COURSE_OVERLAP_FXML);
        mainContainer.loadScreen(Framework.COURSE_OVERLAP_RESTRICTIONS, Framework.COURSE_OVERLAP_RESTRICTIONS_FXML);
        mainContainer.loadScreen(Framework.TIME_RESTRICTIONS, Framework.TIME_RESTRICTIONS_FXML);
        mainContainer.loadScreen(Framework.COURSE_TIME_PROXIMITY, Framework.COURSE_TIME_PROXIMITY_FXML);
        mainContainer.loadScreen(Framework.COURSE_DISTANCE_PROXIMITY, Framework.COURSE_DISTANCE_PROXIMITY_FXML);
        mainContainer.loadScreen(Framework.SCHEDULING_TIME_PATTERNS, Framework.SCHEDULING_TIME_PATTERNS_FXML);

        mainContainer.setScreen(Framework.EDIT_SCHEDULE);
        Group root = new Group();
        root.getChildren().addAll(mainContainer);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

    @FXML
    protected void removeButtonClicked(ActionEvent event) {

    }

    @FXML
    protected void backButtonClicked(ActionEvent event) {

    }

    @FXML
    protected void addButtonClicked(ActionEvent event) {
        try {
            final FXMLLoader loader = new FXMLLoader(getClass().getResource("newSched.fxml"));
            final Parent root = (Parent) loader.load();

            final NewSchedController controller = loader.<NewSchedController>getController();
            Scene scene = new Scene(root);

            controller.setStage(stage);

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

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

    }

    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        mainContainer = new ScreensController();

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

        columnQuarter.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldQuarter"));

        columnYear.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldYear"));

        columnLastEdited.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldLastEdited"));

        columnPublished.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldPublished"));

        tableView.setItems(dataList);
    }



}