package com.zivilon.cinder_loe.items;

import com.zivilon.cinder_loe.entity.projectile.EntityWarDart;
import com.zivilon.cinder_loe.LoECreativeTabs;

import lotr.common.dispenser.LOTRDispenseThrowingAxe;
import lotr.common.enchant.LOTREnchantment;
import lotr.common.enchant.LOTREnchantmentHelper;
import lotr.common.entity.projectile.LOTREntityThrowingAxe;
import lotr.common.item.LOTRMaterial;
import lotr.common.recipe.LOTRRecipes;
import net.minecraft.block.BlockDispenser;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.creativetab.CreativeTabs;
import java.util.List;

public class WarDart extends Item {
    public IIcon[] icons;

    public WarDart() {
        this.setHasSubtypes(true);
        setMaxStackSize(4);
        this.setMaxDamage(0);
        setCreativeTab(LoECreativeTabs.tabCombatLoE);
    }

    @Override
    public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
        if (itemStack.getItemDamage() > 0) {
            if (!player.capabilities.isCreativeMode) {
                --itemStack.stackSize;
            }

            world.playSoundAtEntity(player, "lotr:item.dart", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F));

            if (!world.isRemote) {
                world.spawnEntityInWorld(new EntityWarDart(world, player, itemStack));
            }
        }
        return itemStack;
    }

    @Override
    public IIcon getIconFromDamage(int damage) {
        if (damage < 0 || damage >= icons.length) {
            damage = 0;
        }
        return this.icons[damage];
    }

    @Override
    public void registerIcons(IIconRegister iconRegister) {
        this.icons = new IIcon[15];
        this.icons[0] = iconRegister.registerIcon("lotr:war_dart_headless");
        this.icons[1] = iconRegister.registerIcon("lotr:war_dart_copper");
        this.icons[2] = iconRegister.registerIcon("lotr:war_dart_bronze");
        this.icons[3] = iconRegister.registerIcon("lotr:war_dart_iron");
        this.icons[4] = iconRegister.registerIcon("lotr:war_dart_orc");
        this.icons[5] = iconRegister.registerIcon("lotr:war_dart_dwarven");
        this.icons[6] = iconRegister.registerIcon("lotr:war_dart_uruk");
        this.icons[7] = iconRegister.registerIcon("lotr:war_dart_blue_dwarven");
        this.icons[8] = iconRegister.registerIcon("lotr:war_dart_black_uruk");
        this.icons[9] = iconRegister.registerIcon("lotr:war_dart_elven");
        this.icons[10] = iconRegister.registerIcon("lotr:war_dart_gilded_iron");
        this.icons[11] = iconRegister.registerIcon("lotr:war_dart_red_dwarven");
        this.icons[12] = iconRegister.registerIcon("lotr:war_dart_mithril");
        this.icons[13] = iconRegister.registerIcon("lotr:war_dart_ancient");
        this.icons[14] = iconRegister.registerIcon("lotr:war_dart_morgul");
    }

    public static String getDartMaterial(ItemStack item) {
        switch(item.getItemDamage()) {
            case 1:
                return "copper";
            case 2:
                return "bronze";
            case 3:
                return "iron";
            case 4:
                return "orc";
            case 5:
                return "dwarven";
            case 6:
                return "uruk";
            case 7:
                return "blue_dwarven";
            case 8:
                return "black_uruk";
            case 9:
                return "elven";
            case 10:
                return "gilded_iron";
            case 11:
                return "red_dwarven";
            case 12:
                return "mithril";
            case 13:
                return "ancient";
            case 14:
                return "morgul";
            default:
                return "headless";
        }
    }

    @Override
    public String getUnlocalizedName(ItemStack item) {
        return "item.war_dart_" + getDartMaterial(item);
    }

    @Override
    public int getMetadata(int damage) {
        return damage;
    }

    public static double getDamageFromMeta(int meta) {
        switch(meta) {
            case 1: return 5.5D;  // Copper
            case 2: return 5.5D;  // Bronze
            case 3: return 6.0D;  // Iron
            case 4: return 6.5D;  // Orc
            case 5: return 7.0D;  // Dwarven
            case 6: return 7.0D;  // Uruk
            case 7: return 7.0D; // Blue Dwarven
            case 8: return 7.0D; // Black Uruk
            case 9: return 7.0D; // Elven
            case 10: return 6.5D; // Gilded Iron
            case 11: return 7.0D; // Red Dwarven
            case 12: return 9.0D; // Mithril
            case 13: return 12.0D; // Ancient
            case 14: return 6.5D; // Morgul
            default: return 0.0D;
        }
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) {
        int damageMeta = stack.getItemDamage();
        if (damageMeta > 0) {
            double damagePercent = getDamageFromMeta(damageMeta) * (1.0 / 8) * 100.0;
            double roundedDamagePercent = 5 * Math.round(damagePercent / 5.0);
            list.add(EnumChatFormatting.DARK_GREEN + "Ranged damage: " + roundedDamagePercent + "%");
        }
    }

    @Override
    public void getSubItems(Item item, CreativeTabs tab, List list) {
        for (int i = 0; i < icons.length; i++) {
            list.add(new ItemStack(item, 1, i));
        }
    }
}
