package com.zivilon.cinder_loe.carriage.gui;

import com.zivilon.cinder_loe.carriage.CarriageDestination;
import com.zivilon.cinder_loe.carriage.CarriageUtils;
import com.zivilon.cinder_loe.network.PacketCarriageSelect;
import lotr.client.gui.LOTRGuiScreenBase;
import lotr.common.LOTRMod;
import lotr.common.network.LOTRPacketHandler;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;

import java.util.List;

public class GuiCarriageMenu extends LOTRGuiScreenBase {
    private final List<CarriageDestination> destinations;
    private final List<Integer> costs;
    private final RenderItem itemRenderer = new RenderItem();

    public GuiCarriageMenu(List<CarriageDestination> dests, List<Integer> costs) {
        this.destinations = dests;
        this.costs = costs;
    }

    @Override
    public void initGui() {
        buttonList.clear();
        int centerX = width / 2;
        int centerY = height / 2;
        int yStart = centerY - (destinations.size() * 12);
        int id = 0;

        for (int i = 0; i < destinations.size(); i++) {
            CarriageDestination dest = destinations.get(i);
            int cost = costs.get(i);

            // Strip quotes from name
            String displayName = dest.name.replace("\"", "");

            boolean hasEnough = CarriageUtils.hasEnoughCoins(mc.thePlayer, cost);
            String text = displayName + "  " + cost; // show cost as number only, icon drawn separately

            if (!hasEnough) {
                text = EnumChatFormatting.DARK_GRAY + text;
            }

            GuiButton button = new GuiButton(id++, centerX - 100, yStart + id * 25, 200, 20, text);
            button.enabled = hasEnough;
            buttonList.add(button);
        }
    }

    @Override
    protected void actionPerformed(GuiButton button) {
        CarriageDestination selected = destinations.get(button.id);
        LOTRPacketHandler.networkWrapper.sendToServer(new PacketCarriageSelect(selected.name));
        mc.thePlayer.closeScreen();
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        drawDefaultBackground();
        drawCenteredString(
                "Choose your destination",
                width / 2,
                height / 2 - (destinations.size() * 12) - 20,
                0xFFFFFF
        );
        super.drawScreen(mouseX, mouseY, partialTicks);

        for (int i = 0; i < buttonList.size(); i++) {
            GuiButton button = (GuiButton) buttonList.get(i);
            int iconX = button.xPosition + button.width - 18;
            int iconY = button.yPosition + 2;

            RenderHelper.enableGUIStandardItemLighting();
            itemRenderer.renderItemIntoGUI(
                    fontRendererObj,
                    mc.getTextureManager(),
                    new ItemStack(LOTRMod.silverCoin),
                    iconX,
                    iconY
            );
            RenderHelper.disableStandardItemLighting();
        }
    }

}
