package jesse.keeblarcraft.gui.ShopKeeperGUI;

import jesse.keeblarcraft.GuiMgr.ShopKeeper.ShopKeeperInventory;
import jesse.keeblarcraft.GuiMgr.ShopKeeper.StoreItem;
import jesse.keeblarcraft.gui.Generics.ClickableLayer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// This is the main section in the GUI that will display the stores "merchandise" -- all the items on sale.
public class MerchandiseScroller extends ClickableLayer {
    private final ShopKeeperInventory storeInventory;
    private int scrollAmount = 0; // Indicator for how many tickets the scrollbar is down
    private final int maxDisplayItems = 6;
    private boolean resetDrawLayers = true;

    // Key = item name
    // Value = the drawable slot for it
    // The KVP keeps it so duplicate items don't take up more than 1 store slot
    private final HashMap<String, DrawableStoreItem> drawableInventory;
    List<DrawableStoreItem> drawList = new ArrayList<DrawableStoreItem>();

    public MerchandiseScroller(Identifier texture, Text layerName, int width, int height, int startX, int startY, ShopKeeperInventory storeInventory) {
        super(texture, layerName, width, height, startX, startY);
        this.storeInventory = storeInventory;
        drawableInventory = new HashMap<>();
    }

    public void UpdateScrollAmount(double amount) {
        ClearSubLayers();
        resetDrawLayers = true;
        this.scrollAmount = (int) Math.floor(amount * 100);
    }

    @Override
    public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
        // We only re-render buttons when the catalogue has been scrolled or accessed in some other way
        if (resetDrawLayers)  {
            int drawX = this.getX() + 10;
            int drawY = this.getY() + 5;
            int xScalar = 0;
            int yScalar = 0;
            int oscillator = 0;

            for (int i = scrollAmount; i < scrollAmount + maxDisplayItems; i++) {
                // Make sure we are within the bounds of the size
                if (i < drawList.size()) {
                    DrawableStoreItem storeItem = drawList.get(i);
                    // uv values can be changed for smooth scrolling in the future; but for now with jar-scrolling we will be
                    // scrolling at least 1 height value of the store stuff
                    storeItem.ResetLayer(drawX, drawY, storeItem.getWidth(), storeItem.getHeight());
                    AddSubLayer(storeItem);
                    if (oscillator % 2 == 0) {
                        // Even implies we are going to odd; which means this is xScalar + 1
                        xScalar++;
                        oscillator++;
                    } else {
                        yScalar++;
                        xScalar--;
                        oscillator--;
                    }

                    // drawX and drawY must start in top left of grid -> then add a scalar.
                    // this.getX() + 10 means the MINIMUM spacer is 10 off the X axis
                    //      - width + 15 means spacer is 15 pix * xScalar (first or second in row)
                    // this.getY() + 5 means the MINIMUM spacer is 5 off the Y axi
                    //      - height + 5 mean 5 pixels between each row
                    drawX = this.getX() + 10 + ((storeItem.getWidth() + 15) * xScalar);
                    drawY = this.getY() + 5 + ((storeItem.getHeight() + 5) * yScalar);
                }
            }
            resetDrawLayers = false;
        }
    }

    public Double GetCartTotal() {
        Double finalPrice = 0.0;
        for (Map.Entry<String, DrawableStoreItem> storeItem : this.drawableInventory.entrySet()) {
            finalPrice += storeItem.getValue().GetPrice();
        }
        return finalPrice;
    }

    public Integer Size() {
        return storeInventory.Size();
    }

    public void Add(StoreItem item) {
        storeInventory.Add(item);
        System.out.println("Item cost is " + item.GetCost());
        drawableInventory.put(item.GetUPC(), new DrawableStoreItem(item.GetName(), item.GetCost(), item.GetItem()));
        ResizeDrawlist();
    }

    public void Remove(String upc) {
        storeInventory.Remove(upc);
        ResizeDrawlist();
    }

    private void ResizeDrawlist() {
        drawList.clear();
        for (Map.Entry<String, DrawableStoreItem> entry : drawableInventory.entrySet()) {
            drawList.add(entry.getValue());
        }
    }

    @Override
    protected void appendClickableNarrations(NarrationMessageBuilder builder) {}
}
