package us.myles.ViaVersion;

import com.google.gson.JsonObject;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaAPI;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.bungee.commands.BungeeCommand;
import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler;
import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
import us.myles.ViaVersion.bungee.platform.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class Bungee extends Plugin implements ViaPlatform {

    private BungeeViaAPI api;
    private BungeeConfigAPI config;
    private BungeeCommandHandler commandHandler;

    @Override
    public void onLoad() {
        api = new BungeeViaAPI();
        config = new BungeeConfigAPI(getDataFolder());
        commandHandler = new BungeeCommandHandler();
        ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler));
        // Init platform
        Via.init(ViaManager.builder()
                .platform(this)
                .injector(new BungeeViaInjector())
                .loader(new BungeeViaLoader())
                .commandHandler(commandHandler)
                .build());
    }

    @Override
    public void onEnable() {
        // Inject
        Via.getManager().init();
    }

    @Override
    public String getPlatformName() {
        return "BungeeCord";
    }

    @Override
    public String getPluginVersion() {
        return getDescription().getVersion();
    }

    @Override
    public TaskId runAsync(Runnable runnable) {
        return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable).getId());
    }

    @Override
    public TaskId runSync(Runnable runnable) {
        return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable).getId());
    }

    @Override
    public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
        return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS).getId());
    }

    @Override
    public void cancelTask(TaskId taskId) {
        if (taskId == null) return;
        if (taskId.getObject() == null) return;
        if (taskId instanceof BungeeTaskId) {
            getProxy().getScheduler().cancel((Integer) taskId.getObject());
        }
    }

    @Override
    public ViaCommandSender[] getOnlinePlayers() {
        ViaCommandSender[] array = new ViaCommandSender[getProxy().getPlayers().size()];
        int i = 0;
        for (ProxiedPlayer player : getProxy().getPlayers()) {
            array[i++] = new BungeeCommandSender(player);
        }
        return array;
    }

    @Override
    public void sendMessage(UUID uuid, String message) {
        getProxy().getPlayer(uuid).sendMessage(new TextComponent(message));
    }

    @Override
    public boolean kickPlayer(UUID uuid, String message) {
        if (getProxy().getPlayer(uuid) != null) {
            getProxy().getPlayer(uuid).disconnect(new TextComponent(message));
            return true;
        }
        return false;
    }

    @Override
    public boolean isPluginEnabled() {
        return true;
    }

    @Override
    public ViaAPI getApi() {
        return api;
    }

    @Override
    public ViaVersionConfig getConf() {
        return config;
    }

    @Override
    public ConfigurationProvider getConfigurationProvider() {
        return config;
    }

    @Override
    public void onReload() {
        // Injector prints a message <3
    }

    @Override
    public JsonObject getDump() {
        return null;
    }

}

@@ -229,21 +229,164 @@
 api.
-plugin.Plugin
+event.PlayerDisconnectEvent;%0Aimport net.md_5.bungee.api.plugin.Listener;%0Aimport net.md_5.bungee.api.plugin.Plugin;%0Aimport net.md_5.bungee.event.EventHandler
 ;%0Aim
@@ -1086,16 +1086,26 @@
 Platform
+, Listener
  %7B%0A%0A    
@@ -1751,16 +1751,16 @@
 andler)%0A
-
         
@@ -1773,24 +1773,93 @@
   .build());
+%0A%0A        getProxy().getPluginManager().registerListener(this, this);
 %0A    %7D%0A%0A    
@@ -4221,19 +4221,180 @@
 return n
-ull
+ew JsonObject();%0A    %7D%0A%0A    @EventHandler%0A    public void onQuit(PlayerDisconnectEvent e) %7B%0A        Via.getManager().removePortedClient(e.getPlayer().getUniqueId())
 ;%0A    %7D%0A

Implement removePortedClient on Bungee