package com.gitrekt.resort.controller;

import com.gitrekt.resort.model.RoomSearchResult;
import com.gitrekt.resort.model.services.BookingService;
import com.gitrekt.resort.view.BrowseRoomsListItem;
import com.gitrekt.resort.view.DeletableListItem;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.chrono.ChronoLocalDate;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.util.Callback;

/**
 * FXML Controller class for the browse rooms screen.
 */
public class BrowseRoomsScreenController implements Initializable,
        DeletableListItemDeletionListener {
    
    @FXML
    private ListView<RoomSearchResult> roomsListView;
    
    @FXML
    private ListView<RoomSearchResult> selectedRoomsListView;
    
    @FXML
    private DatePicker checkInDatePicker;
    
    @FXML
    private DatePicker checkOutDatePicker;
    
    private final ObservableList<RoomSearchResult> roomSearchResults;
    
    private final ObservableList<RoomSearchResult> selectedRooms;
    
    public BrowseRoomsScreenController() {
        roomSearchResults = FXCollections.observableArrayList();
        selectedRooms = FXCollections.observableArrayList();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        roomsListView.setItems(roomSearchResults);
        roomsListView.setCellFactory(param -> new BrowseRoomsListItem() {
            {
                // Don't touch. Magic.
                prefWidthProperty().bind(roomsListView.widthProperty());
            }
        });
        roomsListView.setPlaceholder(new Label("No results."));
        selectedRoomsListView.setCellFactory(
            param -> new DeletableListItem(this)
        );
        selectedRoomsListView.setItems(selectedRooms);
        selectedRoomsListView.setPlaceholder(new Label("No rooms selected"));
        initializeDatePickers();
    }
    
    private void initializeDatePickers() {
        checkInDatePicker.setDayCellFactory(
            (final DatePicker datePicker) -> new DateCell() {
                @Override
                public void updateItem(LocalDate item, boolean empty) {
                    super.updateItem(item, empty);
                
                    if(item.isAfter(LocalDate.now().plusYears(1))) {
                        setDisable(true);
                    }
                    
                    if(item.isBefore(ChronoLocalDate.from(LocalDate.now()))) {
                        setDisable(true);
                    }
                }
            }
        );
    }
    
    @FXML
    private void onCheckInDateSelected() {
        // TODO
    }
    
    @FXML
    private void onCheckOutDateSelected() {
        // TODO
    }
    
    private void sortResultsByPrice() {
        roomSearchResults.sort(
            (RoomSearchResult r1, RoomSearchResult r2) -> 
                r1.getRoomPrice().compareTo(r2.getRoomPrice())
        );
    }
    
    @FXML
    private void onBackButtonClicked() {
        ScreenManager.getInstance().switchToScreen(
            "/fxml/GuestHomeScreen.fxml"
        );
    }
    
    @FXML
    private void onFindAvailableRoomsButtonClicked() {
        // The new date api is great. Converting back and forth, not so much.
        LocalDate checkInDateTemp = checkInDatePicker.getValue();
        LocalDate checkOutDateTemp = checkOutDatePicker.getValue();
        Instant temp1 = Instant.from(
            checkInDateTemp.atStartOfDay(ZoneId.systemDefault())
        );
        Instant temp2 = Instant.from(
            checkOutDateTemp.atStartOfDay(ZoneId.systemDefault())
        );
        Date checkInDate = Date.from(temp1);
        Date checkOutDate = Date.from(temp2);
        
        // Clear any existing results
        roomSearchResults.clear();
        
        // Get the new results
        BookingService bookingService = new BookingService();
        roomSearchResults.addAll(
            bookingService.getRoomTypesAvailable(checkInDate, checkOutDate)
        );
    }
    
    @FXML
    private void onNextButtonClicked() {
        // TODO replace with the packages screen first - this is temporary
        
        if(selectedRooms.size() > 0) {
            ScreenManager.getInstance().switchToScreen(
                "/fxml/PlaceBookingScreen.fxml"
            );
        }
    }
    
    // TODO: Determine if really necessary
    /**
     * Handles item deletion events for deletable list items.
     */
    @Override
    public void onItemDeleted(DeletableListItem item) {
        // TODO implement
    }
    
}

@@ -2269,31 +2269,33 @@
 ) %7B%0A        
-checkIn
+Callback%3C
 DatePicker.s
@@ -2296,13 +2296,21 @@
 cker
-.setD
+, DateCell%3E d
 ayCe
@@ -2310,33 +2310,35 @@
 %3E dayCellFactory
-(
+ = 
 %0A            (fi
@@ -2687,29 +2687,8 @@
   %7D%0A
-                    %0A
     
@@ -2849,32 +2849,33 @@
  %7D%0A            %7D
+;
 %0A        );%0A    
@@ -2859,32 +2859,215 @@
      %7D;%0A        
+%0A        // Disable selecting invalid check-in/check-out dates%0A        checkInDatePicker.setDayCellFactory(dayCellFactory);%0A        checkOutDatePicker.setDayCellFactory(dayCellFactory
 );%0A    %7D%0A    %0A  

Disable invalid date selection