package com.gruppe27.fellesprosjekt.client.components;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.gruppe27.fellesprosjekt.client.CalendarClient;
import com.gruppe27.fellesprosjekt.client.controllers.CalendarController;
import com.gruppe27.fellesprosjekt.common.User;
import com.gruppe27.fellesprosjekt.common.messages.UserMessage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import javafx.util.StringConverter;

import java.util.ArrayList;
import java.util.stream.Collectors;

public class ChooseCalendarComboBox extends ComboBox<User> {
    private ArrayList<User> allUsers;
    private ObservableList<User> users;
    private CalendarController controller;

    public ChooseCalendarComboBox() {
        super();
        allUsers = new ArrayList<>();

        this.setConverter(new StringConverter<User>() {
            @Override
            public String toString(User user) {
                if (user == null) return null;
                return user.getName();
            }

            @Override
            public User fromString(String name) {
                for (User user : users) {
                    if (user.getName().equals(name)) return user;
                }

                return null;
            }
        });

        this.setEditable(true);
        users = FXCollections.observableArrayList();
        this.valueProperty().addListener((observable, oldValue, newValue) -> {
            this.controller.setCurrentUser(newValue);
        });

        this.getEditor().textProperty().addListener((observable, oldValue, newValue) ->
            filterUsers(newValue)
        );

        this.setItems(users);
        findUsers();
    }

    private void filterUsers(String name) {
        users.setAll(allUsers
                .stream()
                .filter(user -> user.getName().toLowerCase().startsWith(name.toLowerCase()))
                .collect(Collectors.toList()));
        this.show();
    }

    public void setController(CalendarController controller) {
        this.controller = controller;
    }

    public void findUsers() {
        UserMessage userMessage = new UserMessage(UserMessage.Command.SEND_ALL);
        CalendarClient client = CalendarClient.getInstance();

        Listener listener = new Listener() {
            public void received(Connection connection, Object object) {
                if (object instanceof UserMessage) {
                    UserMessage message = (UserMessage) object;
                    if (message.getCommand() == UserMessage.Command.RECEIVE_ALL) {
                        users.addAll(message.getUsers());
                        allUsers.addAll(message.getUsers());
                        client.removeListener(this);
                    }
                }
            }
        };

        client.addListener(listener);
        client.sendMessage(userMessage);
    }

    public void resetUser() {
        this.setValue(null);
        this.hide();
    }

    public User getUser() {
        return this.getValue();
    }
}

@@ -576,44 +576,8 @@
 ist;
-%0Aimport java.util.stream.Collectors;
 %0A%0Apu
@@ -1581,445 +1581,42 @@
 his.
-getEditor().textProperty().addListener((observable, oldValue, newValue) -%3E%0A            filterUsers(newValue)%0A        );%0A%0A        this.setItems(users);%0A        findUsers();%0A    %7D%0A%0A    private void filterUsers(String name) %7B%0A        users.setAll(allUsers%0A                .stream()%0A                .filter(user -%3E user.getName().toLowerCase().startsWith(name.toLowerCase()))%0A                .collect(Collectors.toList()));%0A        this.show
+setItems(users);%0A        findUsers
 ();%0A

Remove autocomplete from choose calendar list