package com.zivilon.cinder_loe.items;

import com.zivilon.cinder_loe.LoECreativeTabs;
import com.zivilon.cinder_loe.entity.*;
import com.zivilon.cinder_loe.entity.corrupt.*;
import com.zivilon.cinder_loe.entity.npc.*;
import com.zivilon.cinder_loe.entity.npc.dwarf.RedDwarfArbalest;
import com.zivilon.cinder_loe.entity.npc.dwarf.RedDwarfBannerBearer;
import com.zivilon.cinder_loe.entity.npc.dwarf.RedDwarfCommander;
import com.zivilon.cinder_loe.entity.npc.dwarf.RedDwarfWarrior;
import com.zivilon.cinder_loe.entity.npc.dwarf.trader.RedDwarfSmith;
import com.zivilon.cinder_loe.entity.npc.elf.trader.BladorthinSmith;
import com.zivilon.cinder_loe.entity.npc.evil_human.*;
import com.zivilon.cinder_loe.entity.npc.evil_human.trader.LimwaithFishmonger;
import com.zivilon.cinder_loe.entity.npc.evil_human.trader.LimwaithShaman;
import com.zivilon.cinder_loe.entity.npc.good_human.*;
import com.zivilon.cinder_loe.entity.npc.radagast.*;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

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

public class CinderLoESpawnEgg extends Item {
    public static List<Class<? extends Entity>> ENTITY_CLASSES = new ArrayList<>();
    public String entity_type;

    public CinderLoESpawnEgg() {
        this.setTextureName("lotr:spawn_egg");
        this.setCreativeTab(LoECreativeTabs.tabSpawnLoE);
        setHasSubtypes(true);
    }

    @Override
    public String getUnlocalizedName(ItemStack item) {
        return "item.spawn_egg_" + getEntityUnlocalizedName(item.getItemDamage()).replaceFirst("^cinder_loe\\.", "");
    }

    public static Class<? extends Entity> getEntityClass(int meta) {
        if (meta >= 0 && meta < ENTITY_CLASSES.size()) {
            return ENTITY_CLASSES.get(meta);
        }
        return Renegade.class;
    }

    public static String getEntityUnlocalizedName(int meta) {
        Class<? extends Entity> entityClass = getEntityClass(meta);
        String unlocalizedName = (String) EntityList.classToStringMapping.get(entityClass);
        return unlocalizedName != null ? unlocalizedName : "Unknown";
    }

    static {
        ENTITY_CLASSES.add(Renegade.class);
        ENTITY_CLASSES.add(RenegadeCaptain.class);
        ENTITY_CLASSES.add(DarkSpider.class);
        ENTITY_CLASSES.add(Wraith.class);
        ENTITY_CLASSES.add(Limwaith.class);
        ENTITY_CLASSES.add(LimwaithBoneWarrior.class);
        ENTITY_CLASSES.add(LimwaithWarrior.class);
        ENTITY_CLASSES.add(LimwaithBlowgunner.class);
        ENTITY_CLASSES.add(LimwaithBannerBearer.class);
        ENTITY_CLASSES.add(LimwaithChieftain.class);
        ENTITY_CLASSES.add(RedDwarfWarrior.class);
        ENTITY_CLASSES.add(RedDwarfArbalest.class);
        ENTITY_CLASSES.add(RedDwarfBannerBearer.class);
        ENTITY_CLASSES.add(RedDwarfCommander.class);
        ENTITY_CLASSES.add(ArnorSoldier.class);
        ENTITY_CLASSES.add(ArnorSoldierArcher.class);
        ENTITY_CLASSES.add(ArnorBannerBearer.class);
        ENTITY_CLASSES.add(BattleNun.class);
        ENTITY_CLASSES.add(ArnorCaptain.class);
        ENTITY_CLASSES.add(BreeSoldier.class);
        ENTITY_CLASSES.add(BreeCrossbowman.class);
        ENTITY_CLASSES.add(BreeOutrider.class);
        ENTITY_CLASSES.add(BreeSoldierBannerBearer.class);
        ENTITY_CLASSES.add(BreeCaptain.class);
        ENTITY_CLASSES.add(FangornElk.class);
        ENTITY_CLASSES.add(FangornBear.class);
        ENTITY_CLASSES.add(FangornWildBoar.class);
        ENTITY_CLASSES.add(FangornWolf.class);
        ENTITY_CLASSES.add(FangornAuroch.class);
        ENTITY_CLASSES.add(BladorthinSmith.class);
        ENTITY_CLASSES.add(LimwaithFishmonger.class);
        ENTITY_CLASSES.add(LimwaithShaman.class);
        ENTITY_CLASSES.add(RedDwarfSmith.class);
        ENTITY_CLASSES.add(CorruptDwarf.class);
        ENTITY_CLASSES.add(CorruptElf.class);
        ENTITY_CLASSES.add(CorruptEnt.class);
        ENTITY_CLASSES.add(CorruptHobbit.class);
        ENTITY_CLASSES.add(CorruptMan.class);
        ENTITY_CLASSES.add(CorruptOrc.class);
        ENTITY_CLASSES.add(HobbitBannerBearer.class);
    }

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

    public static void spawnCustomCreature(World world, Entity entity) {
        if (entity instanceof EntityLiving) {
            EntityLiving entityLiving = (EntityLiving)entity;

            entityLiving.onSpawnWithEgg(null);

            world.spawnEntityInWorld(entity);
            entityLiving.playLivingSound();
        }
    }

    @Override
    public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
        if (!world.isRemote) {
            MovingObjectPosition target = getMovingObjectPositionFromPlayer(world, player, true);
            
            if (target != null && target.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
                int x = target.blockX;
                int y = target.blockY;
                int z = target.blockZ;
                
                y += 1;

                try {
                    Entity entity = getEntityClass(itemStack.getItemDamage()).getConstructor(World.class).newInstance(world);
                    entity.setLocationAndAngles(x + 0.5, y, z + 0.5, MathHelper.wrapAngleTo180_float(world.rand.nextFloat() * 360.0F), 0.0F);
                    spawnCustomCreature(world, entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (!player.capabilities.isCreativeMode) {
            --itemStack.stackSize;
        }
        return itemStack;
    }
}
