package de.frajul.endlessroll.entities.guis;

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.textures.Texture;

public class ToolButtonAnimationLayer extends Gui {

    private final float ANIMATION_TIME = 500;

    private float startWidth;
    private float startHeight;
    private boolean animationRunning = false;
    private float elapsedTime = 0;

    public ToolButtonAnimationLayer(Texture texture, Vector position, float width, float height) {
        super(texture, position, width, height);
        super.setVisible(false);
        this.startWidth = width;
        this.startHeight = height;
    }

    public void startAnimation() {
        reset();
        super.setVisible(true);
        animationRunning = true;
    }

    public void update(float frameTime) {
        if (animationRunning) {
            elapsedTime += frameTime;
            if (elapsedTime >= ANIMATION_TIME) {
                reset();
            }
            float progress = elapsedTime / ANIMATION_TIME;
            super.setAlpha(0.5f - 0.5f * progress);
            float scaleFactor = 1 + 2 * progress;
            super.setWidth(scaleFactor * startWidth);
            super.setHeight(scaleFactor * startHeight);
        }
    }

    public void reset() {
        animationRunning = false;
        elapsedTime = 0;
        super.setVisible(false);
        super.setAlpha(0.5f);
        super.setWidth(startWidth);
        super.setHeight(startHeight);
    }

}
