package com.zivilon.cinder_loe.mixins;

import lotr.common.inventory.LOTRSlotTrade;
import com.zivilon.cinder_loe.util.Utilities;
import lotr.client.gui.LOTRGuiTrade;
import lotr.common.entity.npc.*;
import lotr.common.entity.npc.LOTRTradeEntries.TradeType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.client.gui.Gui;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.util.StatCollector;

import java.lang.reflect.Field;
import java.util.Map;

@Mixin(LOTRGuiTrade.class)
public abstract class MixinLOTRGuiTrade {
    @Shadow(remap = false)
    private void renderCost(String s, int x, int y) {}
    @Shadow(remap = false)
    private static int lockedTradeColor = -1610612736;
    @Shadow(remap = false)
    private void drawCenteredString(String s, int i, int j, int k) {}

    /**
     * @author Shinare
     * @reason Made spices show up permanently locked after being bought
     */
    @Overwrite(remap = false)
    private void renderTradeSlot(LOTRSlotTrade slot) {
        LOTRTradeEntry trade = slot.getTrade();
        if (trade != null) {
            int lockedPixels = 0;
            boolean inFront = false;
            if (!trade.isAvailable()) {
                lockedPixels = 16;
                inFront = true;
            } else {
                lockedPixels = trade.getLockedProgressForSlot();
                inFront = false;
            }
            if (lockedPixels > 0) {
                GL11.glPushMatrix();
                if (inFront)
                    GL11.glTranslatef(0.0F, 0.0F, 200.0F);
                int x = slot.xDisplayPosition;
                int y = slot.yDisplayPosition;
                drawRect(x, y, x + lockedPixels, y + 16, lockedTradeColor);
                GL11.glPopMatrix();
            }
            if (trade.isAvailable()) {
                int cost = slot.cost();
                if (cost > 0)
                    renderCost(Integer.toString(cost), slot.xDisplayPosition + 8, slot.yDisplayPosition + 22);
            } else {
                if (trade.getLockedProgress() > 75.0F) {
                    renderCost(StatCollector.translateToLocal("container.lotr.trade.spice_locked"), slot.xDisplayPosition + 8, slot.yDisplayPosition + 22);
                } else {
                    drawCenteredString(StatCollector.translateToLocal("container.lotr.trade.locked"), slot.xDisplayPosition + 8, slot.yDisplayPosition + 22, 4210752);
                }
            }
        }
    }
    private static void drawRect(int p_73734_0_, int p_73734_1_, int p_73734_2_, int p_73734_3_, int p_73734_4_) {
        int j1;

        if (p_73734_0_ < p_73734_2_) {
            j1 = p_73734_0_;
            p_73734_0_ = p_73734_2_;
            p_73734_2_ = j1;
        }

        if (p_73734_1_ < p_73734_3_) {
            j1 = p_73734_1_;
            p_73734_1_ = p_73734_3_;
            p_73734_3_ = j1;
        }

        float f3 = (float)(p_73734_4_ >> 24 & 255) / 255.0F;
        float f = (float)(p_73734_4_ >> 16 & 255) / 255.0F;
        float f1 = (float)(p_73734_4_ >> 8 & 255) / 255.0F;
        float f2 = (float)(p_73734_4_ & 255) / 255.0F;
        Tessellator tessellator = Tessellator.instance;
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glDisable(GL11.GL_TEXTURE_2D);
        OpenGlHelper.glBlendFunc(770, 771, 1, 0);
        GL11.glColor4f(f, f1, f2, f3);
        tessellator.startDrawingQuads();
        tessellator.addVertex((double)p_73734_0_, (double)p_73734_3_, 0.0D);
        tessellator.addVertex((double)p_73734_2_, (double)p_73734_3_, 0.0D);
        tessellator.addVertex((double)p_73734_2_, (double)p_73734_1_, 0.0D);
        tessellator.addVertex((double)p_73734_0_, (double)p_73734_1_, 0.0D);
        tessellator.draw();
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glDisable(GL11.GL_BLEND);
    }
}
