package net.craftstars.general.command.inven;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.craftstars.general.General;
import net.craftstars.general.command.CommandBase;
import net.craftstars.general.text.LanguageText;
import net.craftstars.general.text.Messaging;
import net.craftstars.general.util.Toolbox;

public class modeCommand extends CommandBase {
	public modeCommand(General instance) {
		super(instance);
	}
	
	@Override
	public Map<String,Object> parse(CommandSender sender, Command command, String label, String[] args, boolean isPlayer) {
		HashMap<String,Object> params = new HashMap<String,Object>();
		GameMode mode;
		switch(args.length) {
		case 0: // /mode
			if(!isPlayer) return null;
			setCommand("view");
			params.put("who", sender);
			break;
		case 1: // /mode <mode> OR /mode <player>
			try {
				mode = GameMode.getByValue(Integer.parseInt(args[0]));
				if(mode == null) mode = GameMode.valueOf(args[0].toUpperCase());
				if(!isPlayer) return null;
				setCommand("set");
				params.put("who", sender);
				params.put("mode", mode);
			} catch(RuntimeException e) {
				if(!(e instanceof IllegalArgumentException || e instanceof NumberFormatException)) throw e;
				Player player = Toolbox.matchPlayer(args[0]);
				if(player == null) {
					Messaging.send(sender, LanguageText.MISC_BAD_MODE);
					Messaging.invalidPlayer(sender, args[0]);
					return null;
				}
				setCommand("view");
				params.put("who", player);
			}
			break;
		case 2: // /mode <player> <mode>
			try {
				mode = GameMode.getByValue(Integer.parseInt(args[0]));
				if(mode == null) mode = GameMode.valueOf(args[1].toUpperCase());
				Player player = Toolbox.matchPlayer(args[0]);
				if(player == null) {
					Messaging.invalidPlayer(sender, args[0]);
					return null;
				}
				setCommand("set");
				params.put("who", player);
				params.put("mode", mode);
			} catch(RuntimeException e) {
				if(!(e instanceof IllegalArgumentException || e instanceof NumberFormatException)) throw e;
				Messaging.send(sender, LanguageText.MISC_BAD_MODE);
				return null;
			}
			break;
		default:
			return null;
		}
		return params;
	}
	
	@Override
	public boolean execute(CommandSender sender, String command, Map<String,Object> args) {
		Player who = (Player)args.get("who");
		if(command.equals("view")) {
			if(!sender.hasPermission("general.gamemode.view"))
				return Messaging.lacksPermission(sender, "general.gamemode.view");
			String modeName = Toolbox.formatItemName(who.getGameMode().toString());
			Messaging.send(sender, LanguageText.MISC_IN_MODE.value("player", who.getDisplayName(), "mode", modeName));
		} else if(command.equals("set")) {
			if(who.equals(sender)) {
				if(!sender.hasPermission("general.gamemode.set"))
					return Messaging.lacksPermission(sender, "general.gamemode.set");
			} else if(!sender.hasPermission("general.gamemode.set.other"))
				return Messaging.lacksPermission(sender, "general.gamemode.set.other");
			GameMode mode = (GameMode)args.get("mode");
			String modeName = Toolbox.formatItemName(mode.toString());
			who.setGameMode(mode);
			if(who.equals(sender))
				Messaging.send(sender, LanguageText.MISC_SET_OWN_MODE.value("mode", modeName));
			else Messaging.send(sender, LanguageText.MISC_SET_MODE.value("player", who.getDisplayName(), "mode", modeName));
		} else return false;
		return true;
	}
	
}

@@ -1022,32 +1022,91 @@
 Int(args%5B0%5D));%0A%09
+%09%09%7D catch(NumberFormatException e) %7B%0A%09%09%09%09mode = null;%0A%09%09%09%7D%0A
 %09%09%09if(mode == nu
@@ -1101,32 +1101,49 @@
 f(mode == null) 
+%7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09
 mode = GameMode.
@@ -1182,248 +1182,47 @@
 %09%09%09%09
-if(!isPlayer) return null;%0A%09%09%09%09setCommand(%22set%22);%0A%09%09%09%09params.put(%22who%22, sender);%0A%09%09%09%09params.put(%22mode%22, mode);%0A%09%09%09%7D catch(RuntimeException e) %7B%0A%09%09%09%09if(!(e instanceof IllegalArgumentException %7C%7C e instanceof NumberFormatException)) throw e;%0A
+%7D catch(IllegalArgumentException e) %7B%0A%09
 %09%09%09%09
@@ -1263,32 +1263,33 @@
 r(args%5B0%5D);%0A%09%09%09%09
+%09
 if(player == nul
@@ -1285,32 +1285,33 @@
 ayer == null) %7B%0A
+%09
 %09%09%09%09%09Messaging.s
@@ -1347,24 +1347,25 @@
 _BAD_MODE);%0A
+%09
 %09%09%09%09%09Messagi
@@ -1396,32 +1396,33 @@
  args%5B0%5D);%0A%09%09%09%09%09
+%09
 return null;%0A%09%09%09
@@ -1414,34 +1414,36 @@
 eturn null;%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09setCommand(%22
@@ -1446,32 +1446,33 @@
 nd(%22view%22);%0A%09%09%09%09
+%09
 params.put(%22who%22
@@ -1481,25 +1481,154 @@
 player);%0A%09%09%09
-%7D
+%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if(!isPlayer) return null;%0A%09%09%09setCommand(%22set%22);%0A%09%09%09params.put(%22who%22, sender);%0A%09%09%09params.put(%22mode%22, mode);
 %0A%09%09%09break;%0A%09

Fix the /mode command to accept named modes without an explicit player.