package com.elmakers.mine.bukkit.magic.command;

import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MagicAPI;
import com.elmakers.mine.bukkit.api.spell.SpellTemplate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MagicGiveCommandExecutor extends MagicTabExecutor {
	public MagicGiveCommandExecutor(MagicAPI api) {
		super(api);
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!api.hasPermission(sender, "Magic.commands.mgive"))
        {
            sendNoPermission(sender);
            return true;
        }

        if (args.length == 0 || args.length > 3)
		{
            sender.sendMessage("Usage: mgive [player] <item> [count]");
			return true;
		}

        String playerName = null;
		String itemName = null;
        String countString = null;

        if (args.length == 1) {
            itemName = args[0];
        } else if (args.length == 3) {
            playerName = args[0];
            itemName = args[1];
            countString = args[2];
        } else {
            playerName = args[0];
            Player testPlayer = Bukkit.getPlayer(playerName);
            if (testPlayer == null) {
                itemName = args[0];
                countString = args[1];
            } else {
                itemName = args[1];
            }
        }

        int count = 1;
        if (countString != null) {
            try {
                count = Integer.parseInt(countString);
            } catch (Exception ex) {
                sender.sendMessage("Error parsing count: " + countString + ", should be an integer.");
                return true;
            }
        }

        Player player = null;
        if (playerName != null) {
            player = Bukkit.getPlayer(playerName);
        }

        if (player == null) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Console usage: mgive <player> <item> [count]");
                return true;
            }
            player = (Player)sender;
        }

        if (itemName.equalsIgnoreCase("xp")) {
            api.giveExperienceToPlayer(player, count);
            sender.sendMessage("Gave " + count + " experience to " + player.getName());
            return true;
        } else {
            Mage mage = api.getMage(player);
            ItemStack item = api.createItem(itemName, mage);
            if (item == null) {
                sender.sendMessage(ChatColor.RED + "Unknown item type " + itemName);
                return true;
            }
            item.setAmount(count);
            String displayName = api.describeItem(item);
            sender.sendMessage("Gave " + count + " " + displayName + " to " + player.getName());
            api.giveItemToPlayer(player, item);
        }

        return true;
	}

	@Override
	public Collection<String> onTabComplete(CommandSender sender, String commandName, String[] args) {
		List<String> options = new ArrayList<String>();
        if (!sender.hasPermission("Magic.commands.mgive")) return options;

		if (args.length == 1) {
            options.addAll(api.getPlayerNames());
		}

        if (args.length == 1 || args.length == 2) {
            Collection<SpellTemplate> spellList = api.getSpellTemplates();
            for (SpellTemplate spell : spellList) {
                options.add(spell.getKey());
            }
            Collection<String> allWands = api.getWandKeys();
            for (String wandKey : allWands) {
                options.add(wandKey);
            }
            for (Material material : Material.values()) {
                options.add(material.name().toLowerCase());
            }
		}
		return options;
	}
}

@@ -2579,32 +2579,285 @@
 %0A        %7D else 
+if (itemName.equalsIgnoreCase(%22sp%22)) %7B%0A            Mage mage = api.getMage(player);%0A            mage.addSkillPoints(count);%0A            sender.sendMessage(%22Gave %22 + count + %22 skill points to %22 + player.getName());%0A            return true;%0A        %7Delse 
 %7B%0A            Ma
@@ -4228,16 +4228,78 @@
       %7D%0A
+            options.add(%22xp%22);%0A            options.add(%22sp%22);%0A
 %09%09%7D%0A%09%09re

Add "sp" as an option to /mgive