/*
 * Copyright (c) 2015, Mazen Kotb <email@mazenmc.io>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package io.minecloud.bungee;

import io.minecloud.models.server.Server;
import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReconnectHandler extends AbstractReconnectHandler {
    private MineCloudPlugin plugin;

    ReconnectHandler(MineCloudPlugin plugin) {
        this.plugin = plugin;
    }

    @Override
    protected ServerInfo getStoredServer(ProxiedPlayer proxiedPlayer) {
        ServerInfo info = ReconnectHandler.getForcedHost(proxiedPlayer.getPendingConnection());

        if (info == null) {
            List<Server> servers = new ArrayList<>(plugin.mongo.repositoryBy(Server.class)
                    .findAll((server) -> server.type().defaultServer() &&
                            server.network().equals(plugin.bungee().network())));

            Collections.sort(servers, (a, b) -> b.onlinePlayers().size() - a.onlinePlayers().size());

            info = plugin.getProxy().getServerInfo(servers.get(0).name());
        }

        if (info == null) {
            proxiedPlayer.disconnect(new TextComponent("Could not find a server to connect to, please report."));
        }

        return info;
    }

    @Override
    public void setServer(ProxiedPlayer proxiedPlayer) {

    }

    @Override
    public void save() {
    }

    @Override
    public void close() {
    }
}

@@ -853,24 +853,76 @@
 ver.Server;%0A
+import io.minecloud.models.server.ServerRepository;%0A
 import net.m
@@ -958,24 +958,24 @@
 ectHandler;%0A
-
 import net.m
@@ -1113,36 +1113,8 @@
 r;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1576,47 +1576,38 @@
     
-List%3C
 Server
-%3E servers = new ArrayList%3C%3E(
+Repository repository = 
 plug
@@ -1641,24 +1641,18 @@
 r.class)
+;
 %0A
-       
         
@@ -1659,36 +1659,27 @@
     
- .findAll((s
+List%3CS
 erver
-) -
 %3E server
 .typ
@@ -1678,34 +1678,108 @@
 rver
-.type().defaultServer() &&
+s = repository.find(repository.createQuery()%0A                    .field(%22defaultServer%22).equal(true)
 %0A   
@@ -1799,39 +1799,31 @@
     
-        server.
+.field(%22
 network
-(
+%22
 ).equal
-s
 (plu
@@ -1846,16 +1846,45 @@
 work()))
+%0A                    .asList(
 );%0A%0A    

Fix bungee bug