package jesse.keeblarcraft.gui.ShopKeeperGUI;

import jesse.keeblarcraft.gui.Generics.ClickableLayer;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;

import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class Scrollbar extends ClickableLayer {
    private boolean isDragging = false;
    private boolean isLocked = false;
    private int scrollbarStart = 0;
    private int scrollbarEnd = 0;
    private double scrollDistance; // The current value of where the bar is between scrollbarEnd and scrollbarStart

    public Scrollbar(Identifier texture, Text layerName, int width, int height, int startX, int startY, int scrollbarStart, int scrollbarMaxLength) {
        super(texture, layerName, width, height, startX, startY);
        this.scrollbarStart = scrollbarStart;
        this.scrollbarEnd = startY + scrollbarMaxLength;
    }

    @Override
    protected void onDrag(double mX, double mY, double dX, double dY) {
        // We only want to enable dragging if this feature is being toggled
        if (dY > 0) {
           dY = 1.0;
        } else {
            dY = -1.0;
        }

        if (isDragging && mY >= this.scrollbarStart && mY <= this.scrollbarEnd && !isLocked) {
            this.MoveLayer(this.getX(), (int)mY - (this.getHeight() / 2));
            UpdateScrollDistance(mY);
        }
    }

    public void SetLocked(boolean b) {
        this.isLocked = b;
    }

    public double GetScrollDistance() {
        return this.scrollDistance;
    }

    // You must check to make sure mouseY is in between scrollbarEnd and scrollbarStart before calling this or the
    // number will be VERY wrong!!!
    private void UpdateScrollDistance(double mouseY) {
        // We subtract the floor from mouseY to do our math from 0 up
        mouseY -= scrollbarStart;
        double tmpScrollDistance = (mouseY / (scrollbarEnd - scrollbarStart));

        // Floating point arithmetic sucks; so this is just bounds-clearing
        if (tmpScrollDistance < 0) {
            tmpScrollDistance = 0.01;
        } else if (tmpScrollDistance > 1) {
            tmpScrollDistance = 0.99;
        }
        this.scrollDistance = tmpScrollDistance;

//        System.out.println("Scrollbar distance: " + scrollDistance);
    }

    @Override
    public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
        // We intentionally switch the scroll direction since it is backwards
        // The number set to is the speed of scrolling
        if (verticalAmount > 0) {
            verticalAmount = -3;
        } else {
            verticalAmount = 3;
        }


        int newY = this.getY() + (int) verticalAmount;
//        System.out.println("Mouse move. newY: " + newY + " -> this.getY()=" + this.getY());

        // For the time being these values are going to stick and feel the best with the scrollbar
        // Probably should be fixed in the future so no magic numbers need to exist here. Oh well.
        if (newY >= this.scrollbarStart - 12 && newY <= this.scrollbarEnd-9 && !isLocked) {
            this.MoveLayer(this.getX(), newY);
            UpdateScrollDistance(newY);
        }

        return true;
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        SetDragging(true);
        return true;
    }

    @Override
    public boolean mouseReleased(double mouseX, double mouseY, int button) {
        SetDragging(false);
        return true;
    }

    public Boolean IsDragging() {
        return this.isDragging;
    }

    public void SetDragging(boolean b) {
        this.isDragging = b;
    }

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