package codechicken.nei;

import codechicken.nei.network.NEIClientPacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//TODO FIXME
public class ItemMobSpawner extends ItemBlock {

    private static Map<Integer, EntityLiving> entityHashMap = new HashMap<>();
    private static Map<Integer, String> IDtoNameMap = new HashMap<>();
    public static int idPig;
    private static boolean loaded;
    private static ItemMobSpawner instance;

    public static void register() {
        //GameDataManipulator.replaceItemBlock(Blocks.MOB_SPAWNER, instance = new ItemMobSpawner());
    }

    public static void initRender() {
        //SpawnerRenderer.load(instance);
    }

    public ItemMobSpawner() {
        super(Blocks.MOB_SPAWNER);
        setHasSubtypes(true);
    }

    /**
     * Called from BlockMobSpawner on the client via asm generated onBlockPlacedBy
     */
    public static void onBlockPlaced(World world, BlockPos pos, ItemStack stack) {
        if (!NEIClientConfig.hasSMPCounterPart()) {
            return;
        }

        TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner) world.getTileEntity(pos);
        if (tileentitymobspawner != null) {
            setDefaultTag(stack);
            String mobtype = IDtoNameMap.get(stack.getItemDamage());
            if (mobtype != null) {
                NEIClientPacketHandler.sendMobSpawnerID(pos.getX(), pos.getY(), pos.getZ(), mobtype);
                tileentitymobspawner.getSpawnerBaseLogic().setEntityId(new ResourceLocation(mobtype));
            }
        }
    }

    @Override
    public void addInformation(ItemStack itemstack, @Nullable World worldIn, List<String> list, ITooltipFlag flag) {
        setDefaultTag(itemstack);
        int meta = itemstack.getItemDamage();
        if (meta == 0) {
            meta = idPig;
        }
        Entity e = getEntity(meta);
        list.add("\u00A7" + (e instanceof IMob ? "4" : "3") + IDtoNameMap.get(meta));
    }

    public static EntityLiving getEntity(int ID) {
        EntityLiving e = entityHashMap.get(ID);
        if (e == null) {
            World world = Minecraft.getMinecraft().world;
            //Class<? extends Entity> clazz = EntityList.ID_TO_CLASS.get(ID);
            //try {
            //    e = (EntityLiving) clazz.getConstructor(World.class).newInstance(world);
            //} catch (Throwable t) {
            //    if (clazz == null) {
            //        LogHelper.error("Null class for entity (" + ID + ", " + IDtoNameMap.get(ID));
            //    } else {
            //        LogHelper.errorError("Error creating instance of entity: " + clazz.getName(), t);
            //    }
            //    e = getEntity(idPig);
            //}
            //entityHashMap.put(ID, e);
        }
        return e;
    }

    private static void setDefaultTag(ItemStack itemstack) {
        if (!IDtoNameMap.containsKey(itemstack.getItemDamage())) {
            itemstack.setItemDamage(idPig);
        }
    }

    public static void loadSpawners(World world) {
        /*if (loaded) {
            return;
        }
        loaded = true;
        HashMap<Class<? extends Entity>, String> classToStringMapping = null;//(HashMap<Class<? extends Entity>, String>) EntityList.CLASS_TO_NAME;
        HashMap<Class<? extends Entity>, Integer> classToIDMapping = null;//(HashMap<Class<? extends Entity>, Integer>) EntityList.CLASS_TO_ID;
        for (Class<? extends Entity> entityClass : classToStringMapping.keySet()) {
            if (!EntityLiving.class.isAssignableFrom(entityClass)) {
                continue;
            }
            try {
                EntityLiving entityliving = (EntityLiving) entityClass.getConstructor(new Class[] { World.class }).newInstance(world);
                entityliving.isChild();

                int id = classToIDMapping.get(entityClass);
                String name = classToStringMapping.get(entityClass);

                if (name.equals("EnderDragon")) {
                    continue;
                }

                IDtoNameMap.put(id, name);

                if (name.equals("Pig")) {
                    idPig = id;
                }
            } catch (Throwable ignored) {
            }
        }

        for (Iterator<Entry<Integer, String>> it = IDtoNameMap.entrySet().iterator(); it.hasNext(); ) {
            Entry<Integer, String> e = it.next();
            if (getEntity(e.getKey()).getClass() == EntityPig.class && !e.getValue().equals("Pig")) {
                it.remove();
            }
        }*/
    }

    @Override
    public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> list) {
        if (!NEIClientConfig.hasSMPCounterPart()) {
            list.add(new ItemStack(this));
        } else {
            for (int i : IDtoNameMap.keySet()) {
                list.add(new ItemStack(this, 1, i));
            }
        }
    }
}