package com.gruppe27.fellesprosjekt.client.controllers;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.gruppe27.fellesprosjekt.client.CalendarApplication;
import com.gruppe27.fellesprosjekt.client.CalendarClient;
import com.gruppe27.fellesprosjekt.common.Event;
import com.gruppe27.fellesprosjekt.common.Room;
import com.gruppe27.fellesprosjekt.common.messages.EventMessage;
import com.gruppe27.fellesprosjekt.common.messages.RoomMessage;
import com.gruppe27.fellesprosjekt.common.messages.RoomRequestMessage;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import java.net.URL;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.ResourceBundle;

public class CreateEventController implements Initializable {
    @FXML
    TextField emne;

    @FXML
    DatePicker datePicker;

    @FXML
    TextField fromTimeField;

    @FXML
    TextField toTimeField;

    @FXML
    ListView deltakere;

    @FXML
    ChoiceBox<String> roomChoiceBox;

    @FXML
    TextField capacityField;

    @FXML
    Button createEventButton;

    @FXML
    Button cancelButton;



    private CalendarApplication application;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void setApp(CalendarApplication application) {
        this.application = application;
    }

    @FXML
    private void handleChoiceboxClicked() {
        System.out.println("Cbox clicked.");
        LocalDate date = datePicker.getValue();
        LocalTime start = LocalTime.parse(fromTimeField.getText());
        LocalTime end = LocalTime.parse(toTimeField.getText());
        int capacity = Integer.parseInt(capacityField.getText());
        this.updateCurrentRooms(date,start,end,capacity);
        //TODO needs time to update rooms before I can do something.

    }

    @FXML
    private void updateCurrentRooms(LocalDate date, LocalTime start, LocalTime end, int capacity) {
        RoomRequestMessage message = new RoomRequestMessage(RoomRequestMessage.Command.ROOM_REQUEST, date,start,end,capacity);

        CalendarClient client = CalendarClient.getInstance();

        Listener roomListener = new Listener() {
            public void received(Connection connection, Object object) {
                if (object instanceof RoomMessage) {
                    RoomMessage message = (RoomMessage) object;

                    switch (message.getCommand()) {
                        case RECEIVE_ROOMS:
                            updateChoiceBox(message.getRooms());
                            break;
                    }
                    client.removeListener(this);
                }
            }
        };
        client.addListener(roomListener);
        client.sendMessage(message);

    }

    private void updateChoiceBox(HashSet<Room> rooms) {
        ArrayList<Room> roomsArray = new ArrayList<>();
        roomsArray.addAll(rooms);
        ArrayList<String> stringArrayList = new ArrayList<>();
        for(Room room: roomsArray) {
            String roomString = room.toString();
            stringArrayList.add(roomString);
        }

        ObservableList<String> observableList = FXCollections.observableArrayList(stringArrayList);

        Platform.runLater(() -> {
            roomChoiceBox.setItems(observableList);
            roomChoiceBox.show();
        });

    }

    @FXML
    private void handleCreateEventAction() {
        Event event = new Event();
        event.setName(emne.getText());

        event.setDate(datePicker.getValue());

        LocalTime startTime = LocalTime.parse(fromTimeField.getText());
        LocalTime endTime = LocalTime.parse(toTimeField.getText());
        event.setStartTime(startTime);
        event.setEndTime(endTime);

        EventMessage message = new EventMessage(EventMessage.Command.CREATE_EVENT, event);
        CalendarClient.getInstance().sendMessage(message);
    }

    @FXML
    private void handleCancelAction() {
    }
}

@@ -598,16 +598,101 @@
 atform;%0A
+import javafx.beans.value.ChangeListener;%0Aimport javafx.beans.value.ObservableValue;%0A
 import j
@@ -724,16 +724,16 @@
 ctions;%0A
-
 import j
@@ -1667,16 +1667,78 @@
 ication;
+%0A    private ArrayList%3CRoom%3E roomsArray;%0A    Room currentRoom;
 %0A%0A    @O
@@ -1813,16 +1813,409 @@
 urces) %7B
+%0A        roomsArray = null;%0A        currentRoom = null;%0A        roomChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener%3CNumber%3E() %7B%0A            @Override%0A            public void changed(ObservableValue%3C? extends Number%3E observable, Number oldValue, Number newValue) %7B%0A                currentRoom = roomsArray.get(newValue.intValue());%0A            %7D%0A        %7D);
 %0A%0A    %7D%0A
@@ -3850,32 +3850,37 @@
 st%3C%3E();%0A        
+this.
 roomsArray.addAl
@@ -4687,24 +4687,24 @@
 startTime);%0A
-
         even
@@ -4725,16 +4725,87 @@
 ndTime);
+%0A        event.setRoom(currentRoom);%0A        System.out.println(event);
 %0A%0A      

add roomchange listener, and setRoom.