package net.mc_pandacraft.java.plugin.pandacraftutils.config.elements;

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

import org.bukkit.ChatColor;

public class MultiCommandConfigEntry {
	private final String name;
	private final List<MucoSubCommand> subCommands;
	
	
	public MultiCommandConfigEntry(String n, List<String> subCommandsString) throws Exception {
		
		if (n == null || n.trim().equals("") || subCommandsString == null || subCommandsString.size() == 0)
			throw new IllegalArgumentException("Un des paramètres du constructeur est invalide");
		
		name = n;
		
		subCommands = new ArrayList<MucoSubCommand>();
		
		for (String subCmd : subCommandsString) {
			
			String[] args = subCmd.split(" ", 4);
			
			
			if (args.length != 4)
				throw new Exception("/muco : bad format for "+ChatColor.ITALIC+subCmd);
			
			long time;
			
			try {
				time = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				throw new Exception("/muco : bad format for "+ChatColor.ITALIC+subCmd);
			}

			boolean console = args[1].equalsIgnoreCase("c");
			boolean async = args[2].equalsIgnoreCase("a");
			
			if (!args[3].startsWith("/"))
				throw new Exception("/muco : bad format for "+ChatColor.ITALIC+subCmd);
			
			String cmd = args[3].substring(1);
			
			subCommands.add(new MucoSubCommand(time, console, async, cmd));
			
		}
		
		
	}
	
	
	public String getName() { return name; }
	
	public List<MucoSubCommand> getSubCommands() {
		return Collections.unmodifiableList(subCommands);
	}
	
	
	
	
	
	
	public class MucoSubCommand {
		
		public final long tick;
		public final boolean console;
		public final boolean async;
		public final String command;
		
		
		private MucoSubCommand(long t, boolean c, boolean a, String cmd) {
			if (t < 0 || cmd == null)
				throw new IllegalArgumentException("Un des paramètres du constructeur est invalide");
			
			tick = t;
			console = c;
			async = a;
			command = cmd;
		}
	}
	
}
