package jesse.keeblarcraft.gui.Generics;

import jesse.keeblarcraft.Utils.CommonStructures.Pair;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.screen.slot.Slot;
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;

abstract public class ClickableLayer extends ClickableWidget {
    private final Identifier texture;
    private final Text layerName;
    private final List<ClickableLayer> subLayers = new ArrayList<>();
    private final Map<String, Boolean> drawSlots = new HashMap<>(); // layerName is used as menu name for the screen handler

    protected abstract void appendClickableNarrations(NarrationMessageBuilder builder);

    public ClickableLayer(Identifier texture, Text layerName, int width, int height, int startX, int startY) {
        super(startX, startY, width, height, Text.empty());
        this.texture = texture;
        this.layerName = layerName;
    }

    public void ResetLayer(int startX, int startY, int width, int height) {
        this.setX(startX);
        this.setY(startY);
        this.setWidth(width);
        this.setHeight(height);
    }

    protected void ResizeLayer(int width, int height) {
        this.setWidth(width);
        this.setHeight(height);
    }

    protected void MoveLayer(int startX, int startY) {
        this.setX(startX);
        this.setY(startY);
    }

    protected void AddSubLayer(ClickableLayer newLayer) {
        subLayers.add(newLayer);
    }

    protected void ClearSubLayers() {
        this.subLayers.clear();
    }

    public List<ClickableLayer> GetSubLayers() {
        return this.subLayers;
    }

    public Map<String, Boolean> GetDrawableSlots() {
        return this.drawSlots;
    }

    public void AddDrawableSlotByName(String slotName, boolean toggled) {
        this.drawSlots.put(slotName, toggled);
    }

    public void ClearDrawableSlots() {
        for (Map.Entry<String, Boolean> entry : this.drawSlots.entrySet()) {
            entry.setValue(false);
        }
    }

    // This will be called every draw frame
    public void UpdateSubLayers() {}

    @Override
    public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
        context.drawTexture(this.GetTexture(), this.getX(), this.getY(), 0, 0, this.getWidth(), this.getHeight(), this.getWidth(), this.getHeight());
    }

    public Identifier GetTexture() {
        return this.texture;
    }

    public Text GetLayerName() {
        return this.layerName;
    }
}
