package net.avicus.icarus.module.teams;

import net.avicus.icarus.match.Match;
import net.avicus.icarus.module.Parser;
import net.avicus.icarus.module.ParserException;
import net.avicus.icarus.utils.ParserUtils;
import net.avicus.icarus.utils.config.ConfigSection;

import java.util.HashMap;
import java.util.List;

public class TeamsParser implements Parser<TeamsModule> {

    @Override
    public TeamsModule build(Match match) throws ParserException {
        List<ConfigSection> configs = match.getConfig().getSectionList("teams");

        HashMap<String, Team> teams = new HashMap<String, Team>();

        for (ConfigSection cs : configs) {
            try {
                teams.put(createIdIfAbsent(cs), parseTeam(cs));
            } catch (Exception e) {
                throw new ParserException(this, "Failed to parse team '" + cs.getString("id") + "'.", e);
            }
        }

        return new TeamsModule(teams);
    }

    public String createIdIfAbsent(ConfigSection cs) throws ParserException {
        if (cs.contains("id")) return cs.getAsString("id");
        if (cs.contains("name")) return cs.getAsString("name").toLowerCase().split(" ")[0]; // "Red Team" => "red"
        else throw new ParserException(this, "Team name must be specified."); // The team doesn't even have a name, so an error about an id is irrelevant.
    }

    public Team parseTeam(ConfigSection cs) throws ParserException {
        if (cs.empty("name"))
            throw new IllegalArgumentException("Team name must be specified.");
        if (cs.empty("color"))
            throw new IllegalArgumentException("Team color must be specified.");
        if (cs.empty("max"))
            throw new IllegalArgumentException("Team max size must be specified.");
        return new Team(cs.getString("name"), ParserUtils.parseChatColor(cs.getString("color")), cs.getInt("max"));
    }

}

@@ -368,16 +368,80 @@
 ule%3E %7B%0A%0A
+    HashMap%3CString, Team%3E teams = new HashMap%3CString, Team%3E();%0A%0A
     @Ove
@@ -446,16 +446,16 @@
 verride%0A
-
     publ
@@ -599,76 +599,8 @@
 );%0A%0A
-        HashMap%3CString, Team%3E teams = new HashMap%3CString, Team%3E();%0A%0A
     
@@ -1040,16 +1040,66 @@
 ns(%22id%22)
+  && !this.teams.containsKey(cs.getAsString(%22id%22))
 ) return
@@ -1152,16 +1152,67 @@
 (%22name%22)
+ && !this.teams.containsKey(cs.getAsString(%22name%22))
 ) return
@@ -1361,16 +1361,75 @@
 ecified.
+ This can also happen if a team is declared more than once.
 %22); // T

Check if a team has already been declared.