package com.github.games647.lambdaattack;

import com.github.games647.lambdaattack.bot.Bot;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.spacehq.mc.auth.exception.request.RequestException;

public class LambdaAttack {

    public static final String PROJECT_NAME = "LambdaAttack";

    private static final Logger LOGGER = Logger.getLogger(PROJECT_NAME);

    public static Logger getLogger() {
        return LOGGER;
    }
    
    private boolean running = true;
    private GameVersion gameVersion = GameVersion.VERSION_1_10;
    private final List<Bot> clients = new ArrayList<>();
    private List<Proxy> proxies;

    public void start(String host, int port, int amount, int delay, String nameFormat) throws RequestException {
        for (int i = 0; i < amount; i++) {
            UniversalProtocol account = authenticate(String.format(nameFormat, i), "");
            
            Bot bot;
            if (proxies != null) {
                Proxy proxy = proxies.get((i + proxies.size()) % 4);
                bot = new Bot(account, proxy);
            } else {
                bot = new Bot(account);
            }

            this.clients.add(bot);
        }

        for (Bot client : clients) {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

            if (!running) {
                break;
            }

            client.connect(host, port);
        }
    }

    public UniversalProtocol authenticate(String username, String password) throws RequestException {
        UniversalProtocol protocol;
        if (!password.isEmpty()) {
            throw new UnsupportedOperationException("Not implemented");
//            protocol = new MinecraftProtocol(username, password);
//            LOGGER.info("Successfully authenticated user");
        } else {
            protocol = UniversalFactory.authenticate(gameVersion, username);
        }

        return protocol;
    }

    public void setProxies(List<Proxy> proxies) {
        this.proxies = proxies;
    }

    public GameVersion getGameVersion() {
        return gameVersion;
    }

    public void setGameVersion(GameVersion gameVersion) {
        this.gameVersion = gameVersion;
    }

    public void stop() {
        this.running = false;
        clients.stream().forEach(Bot::disconnect);
    }
}

@@ -1047,12 +1047,11 @@
 get(
-(
 i 
-+
+%25
  pro
@@ -1066,13 +1066,8 @@
 e())
- %25 4)
 ;%0A  

Fix rotating proxy list