package com.gitrekt.resort.controller;

import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

/**
 * FXML Controller class for the management bookings report screen.
 */
public class BookingsReportScreenController implements Initializable {

    @FXML
    private StackedBarChart barChart;
    
    @FXML
    private Label monthYearLabel;
    
    @FXML
    private Button nextMonthButton;
    
    @FXML
    private Button previousMonthButton;
    
    @FXML
    private Button pickMonthButton;
    
    @FXML
    private Button backButton;

    private XYChart.Series<String, Number> roomCategoriesDataSeries;
    
    private ObservableList<Data<String, Number>> roomCategoriesList;
    
    private LocalDateTime selectedMonth;
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // Default month is the current month.
        selectedMonth = LocalDateTime.now().withDayOfMonth(1);
        // TODO: Test using test data, then implement with real logic.
        Data<String,Number> testData = new Data<String,Number>();
        roomCategoriesList = FXCollections.observableArrayList();
        roomCategoriesDataSeries = new XYChart.Series<>();
        roomCategoriesDataSeries.setData(roomCategoriesList);
    }    
    
    @FXML
    private void onPickMonthButtonClicked() {
        // TODO
    }
    
    @FXML
    private void onNextMonthButtonClicked() {
        selectedMonth = selectedMonth.with(
            TemporalAdjusters.firstDayOfNextMonth()
        );
        monthYearLabel.setText(getCurrentMonthYearString());
    }
    
    @FXML
    private void onPreviousMonthButtonClicked() {
        selectedMonth = selectedMonth.minusMonths(1);
        selectedMonth = selectedMonth.withDayOfMonth(1);
        monthYearLabel.setText(getCurrentMonthYearString());
    }
    
    @FXML
    private void onBackButtonClicked() {
        ScreenManager.getInstance().switchToScreen(
            "/fxml/ReportsHomeScreen.fxml"
        );
    }
    
    public void onSelectMonthButtonClicked() {
        // TODO
        monthYearLabel.setText(getCurrentMonthYearString());
    }
    
    private void populateData() {
        // TODO
    }
    
    private String getCurrentMonthYearString() {
        return selectedMonth.getMonth()
                .getDisplayName(TextStyle.FULL, Locale.US)
                + " "
                + selectedMonth.getYear();
    }
    
}

@@ -86,16 +86,39 @@
 teTime;%0A
+import java.time.Year;%0A
 import j
@@ -385,24 +385,102 @@
 tializable;%0A
+import javafx.scene.chart.CategoryAxis;%0Aimport javafx.scene.chart.NumberAxis;%0A
 import javaf
@@ -549,48 +549,8 @@
 rt;%0A
-import javafx.scene.chart.XYChart.Data;%0A
 impo
@@ -802,16 +802,32 @@
 BarChart
+%3CString, Number%3E
  barChar
@@ -1109,22 +1109,22 @@
 .Series%3C
-String
+Number
 , Number
@@ -1152,24 +1152,96 @@
 eries;%0A    %0A
+    private CategoryAxis xAxis;%0A    %0A    private NumberAxis yAxis;%0A    %0A
     private 
@@ -1259,48 +1259,34 @@
 ist%3C
-Data%3C
 String
-, Number%3E%3E roomCategoriesList
+%3E daysInCurrentMonth
 ;%0A  
@@ -1584,321 +1584,87 @@
     
-// TODO: Test using test data, then implement with real logic.%0A        Data%3CString,Number%3E testData = new Data%3CString,Number%3E();%0A        roomCategoriesList = FXCollections.observableArrayList();%0A        roomCategoriesDataSeries = new XYChart.Series%3C%3E();%0A        roomCategoriesDataSeries.setData(roomCategoriesList
+daysInCurrentMonth = FXCollections.observableArrayList();%0A        prepareGraph(
 );%0A 
@@ -1976,32 +1976,56 @@
 hYearString());%0A
+        prepareGraph();%0A
     %7D%0A    %0A    @
@@ -2243,32 +2243,56 @@
 hYearString());%0A
+        prepareGraph();%0A
     %7D%0A    %0A    @
@@ -2574,24 +2574,48 @@
 rString());%0A
+        prepareGraph();%0A
     %7D%0A    %0A 
@@ -2635,39 +2635,544 @@
 id p
-opulateData() %7B%0A        // TODO
+repareGraph() %7B%0A        // Prepare the x-axis, the days in the month%0A        daysInCurrentMonth.clear();%0A        int selectedYear = selectedMonth.getYear();%0A        int numDaysInMonth = selectedMonth.getMonth()%0A                .length(Year.isLeap(selectedYear));%0A        for(int i = 1; i %3C= numDaysInMonth; i++) %7B%0A            daysInCurrentMonth.add(String.valueOf(i));%0A        %7D%0A        barChart.getXAxis().setAutoRanging(false);%0A        CategoryAxis x  = (CategoryAxis) barChart.getXAxis();%0A        x.setCategories(daysInCurrentMonth);
 %0A   

Configure axes for report