package com.zivilon.cinder_loe.mixins;

import lotr.client.LOTRItemRendererManager;
import lotr.client.render.item.LOTRRenderLargeItem;

import net.minecraft.client.resources.IResourceManager;
import net.minecraft.item.Item;

import java.lang.reflect.Field;

import org.objectweb.asm.Opcodes;

import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import lotr.client.render.item.LOTRRenderBannerItem;
import lotr.client.render.item.LOTRRenderBlownItem;
import lotr.client.render.item.LOTRRenderBow;
import lotr.client.render.item.LOTRRenderCrossbow;
import lotr.client.render.item.LOTRRenderElvenBlade;
import lotr.client.render.item.LOTRRenderInvTableCommand;
import lotr.client.render.item.LOTRRenderLargeItem;
import lotr.client.render.item.LOTRRenderSkullStaff;
import lotr.client.render.tileentity.LOTRRenderAnimalJar;
import lotr.common.LOTRMod;
import lotr.common.item.LOTRItemSword;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.item.Item;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;

@Mixin(LOTRItemRendererManager.class)
public abstract class MixinLOTRItemRendererManager {

/*    @Inject(method = "func_110549_a(Lnet/minecraft/client/resources/IResourceManager;)V",
            at = @At(value = "JUMP", opcode = Opcodes.IFEQ, ordinal = 0, shift = At.Shift.BEFORE),
            locals = LocalCapture.CAPTURE_FAILSOFT,
            remap = false)
    private void afterIsLargeSet(IResourceManager resourceManager, CallbackInfo ci, int i, Field[] fields, Field field, Item item, LOTRRenderLargeItem largeItemRenderer, boolean isLarge) {
        System.out.println("\u001B[35mVerifying item in renderer manager...");
        System.out.println("Name of item is " + item.getUnlocalizedName());
        System.out.println("Is crossbow: " + (item instanceof lotr.common.item.LOTRItemCrossbow));
        System.out.println("Is bow: " + (item instanceof lotr.common.item.LOTRItemBow));
        System.out.println("Is sword: " + (item instanceof lotr.common.item.LOTRItemSword));
        System.out.println("Is large: " + isLarge);
    }*/
    @Shadow
    private static List<LOTRRenderLargeItem> largeItemRenderers;

    @Overwrite
    public void onResourceManagerReload(IResourceManager resourceManager) {
        largeItemRenderers.clear();
        try {
            for (Field field : LOTRMod.class.getFields()) {
                System.out.println("Found field " + field.getName() + " of type " + field.getType().getName());
                if (field.getName().equals("spearRedDwarf")) {
                    System.out.println("Found spearRedDwarf");
                    System.out.println("Is type Item: " + (field.get(null) instanceof Item));
                }
                if (field.get(null) instanceof Item) {
                    Item item = (Item)field.get(null);
                    MinecraftForgeClient.registerItemRenderer(item, null);
                    LOTRRenderLargeItem largeItemRenderer = LOTRRenderLargeItem.getRendererIfLarge(item);
                    boolean isLarge = (largeItemRenderer != null);
                    System.out.println("\u001B[35mVerifying item in renderer manager...");
                    System.out.println("Name of item is " + item.getUnlocalizedName());
                    System.out.println("Is crossbow: " + (item instanceof lotr.common.item.LOTRItemCrossbow));
                    System.out.println("Is bow: " + (item instanceof lotr.common.item.LOTRItemBow));
                    System.out.println("Is sword: " + (item instanceof lotr.common.item.LOTRItemSword));
                    System.out.println("Is large: " + isLarge);
                    if (item instanceof lotr.common.item.LOTRItemCrossbow) {
                        MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer)new LOTRRenderCrossbow());
                    } else if (item instanceof lotr.common.item.LOTRItemBow) {
                        MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer)new LOTRRenderBow(largeItemRenderer));
                    } else if (item instanceof LOTRItemSword && ((LOTRItemSword)item).isElvenBlade()) {
                        double d = 24.0D;
                        if (item == LOTRMod.sting)
                            d = 40.0D;
                        MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer)new LOTRRenderElvenBlade(d, largeItemRenderer));
                    } else if (isLarge) {
                        MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer)largeItemRenderer);
                    }
                    if (largeItemRenderer != null)
                        largeItemRenderers.add(largeItemRenderer);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(LOTRMod.commandTable), (IItemRenderer)new LOTRRenderInvTableCommand());
        MinecraftForgeClient.registerItemRenderer(LOTRMod.hobbitPipe, (IItemRenderer)new LOTRRenderBlownItem());
        MinecraftForgeClient.registerItemRenderer(LOTRMod.commandHorn, (IItemRenderer)new LOTRRenderBlownItem());
        MinecraftForgeClient.registerItemRenderer(LOTRMod.conquestHorn, (IItemRenderer)new LOTRRenderBlownItem());
        MinecraftForgeClient.registerItemRenderer(LOTRMod.banner, (IItemRenderer)new LOTRRenderBannerItem());
        MinecraftForgeClient.registerItemRenderer(LOTRMod.orcSkullStaff, (IItemRenderer)new LOTRRenderSkullStaff());
        Iterator<Item> itItems = Item.itemRegistry.iterator();
        while (itItems.hasNext()) {
            Item item = itItems.next();
            if (item instanceof lotr.common.item.LOTRItemAnimalJar)
                MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer)new LOTRRenderAnimalJar());
        }
    }
}
