package me.km.commands;

import java.util.ArrayList;
import me.km.api.GlobalText;
import me.km.api.Module;
import java.util.Arrays;
import me.km.api.ModuleTabCommand;
import me.km.api.Utils;
import me.km.permissions.Permissions;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionUtils;

public class CommandPotion extends ModuleTabCommand
{
    public CommandPotion(Module m) 
    {
        super("potion", m, Arrays.asList(new String[]
        {
            "SPEED", "SLOWNESS", "HASTE", "MINING_FATIGUE", "STRENGTH", "INSTANT_HEALTH",
            "INSTANT_DAMAGE", "JUMP_BOOST", "NAUSEA", "REGENERATION", "RESISTANCE",
            "FIRE_RESISTANCE", "WATER_BREATHING", "INVISIBILITY", "BLINDNESS",
            "NIGHT_VISION", "HUNGER", "WEAKNESS" ,"POISON", "WITHER", "HEALTH_BOOST",
            "ABSORPTION", "SATURATION", "GLOWING", "LEVITATION", "LUCK", "UNLUCK"
        }), 0);  
        super.setDescription("Erzeugt Custom-Potions");
        super.setUsage("/potion [effect] [duration] [amplifier]");
        super.setPermission(Permissions.POTION);
    }

    @Override
    public boolean execute(ICommandSender cs, String[] arg) 
    {
        if(!(cs instanceof EntityPlayer))
        {
            this.getModule().send(cs, GlobalText.onlyPlayer());
            return true;
        }
        EntityPlayer p = (EntityPlayer) cs;
        ItemStack hand = p.getHeldItemMainhand();
        if(hand.getItem() != Items.SPLASH_POTION &&
           hand.getItem() != Items.POTIONITEM &&
           hand.getItem() != Items.LINGERING_POTION)
        {
            this.getModule().send(cs, "Du musst einen Trank in der Hand halten.");
            return true;
        }
        if(arg.length < 1)
        {
            return false;
        }

        try
        {
            NBTTagCompound com = hand.getTagCompound();
            ArrayList<PotionEffect> list = new ArrayList<>();
            list.add(new PotionEffect(Utils.getPotion(arg[0]), Integer.parseInt(arg[1]), Integer.parseInt(arg[2])));
            PotionUtils.addCustomPotionEffectToList(com, list);
            this.getModule().send(cs, "Der Effekt wurde hinzugefügt.");
        }
        catch(NumberFormatException ex)
        {
            this.getModule().send(cs, GlobalText.noIntegerNumber());
            return true;
        }
        catch(NullPointerException ex)
        {
            this.getModule().send(cs, "Der gegebene Potion-Effect existiert nicht.");
            return true;
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            return false;
        }
        return true;
    }
}