package me.benjozork.onyx.ui;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

import me.benjozork.onyx.object.Drawable;
import me.benjozork.onyx.ui.object.Action;
import me.benjozork.onyx.ui.object.ActionEvent;

/**
 * Created by Benjozork on 2017-03-19.
 */
public abstract class UIElement extends Drawable {

    private Array<Action> actions = new Array<Action>();

    private UIContainer parent;

    private String identifier;

    private boolean justHovered = false;

    private Vector2 dimensions = new Vector2();

    public UIElement(float x, float y) {
        super(new Vector2(x, y));
    }

    public UIElement(Vector2 position) {
        super(position);
    }

    public void update(float dt) {
        if (hovering()) {
            if (justHovered) return;
            triggerEvent(ActionEvent.HOVERED);
            justHovered = true;
        } else {
            justHovered = false;
        }
    }

    @Override
    public abstract void init();

    @Override
    public abstract void update();

    @Override
    public abstract void draw();

    public void triggerEvent(ActionEvent e) {
        for (Action a : actions) {
            if (a.getEvent() == e) {
                a.run();
            }
        }
    }

    public boolean clickElement(Vector2 localPosition) {
        click(localPosition);
        triggerEvent(ActionEvent.CLICKED);
        return true;
    }

    public abstract boolean click(Vector2 localPosition);

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public UIContainer getParent() {
        return parent;
    }

    public Array<Action> getActions() {
        return actions;
    }

    public void addAction(String identifier, Runnable action, ActionEvent event) {
        actions.add(new Action(this, identifier, action, event));
    }

    public float getWidth() {
        return dimensions.x;
    }

    public void setWidth(float v) {
        dimensions.x = v;
        bounds.width = v;
    }

    public float getHeight() {
        return dimensions.y;
    }

    public void setHeight(float v) {
        dimensions.y = v;
        bounds.height = v;
    }

    public void resize(float dx, float dy) {
        this.dimensions.x += dx;
        this.bounds.width += dx;
        this.dimensions.y += dy;
        this.bounds.height += dy;
    }

    public void setDimensions(float w, float h) {
        this.dimensions.x = w;
        this.bounds.width = w;
        this.dimensions.y = h;
        this.bounds.height = h;
    }

}

@@ -1088,24 +1088,141 @@
 id draw();%0A%0A
+    /**%0A     * Triggers an event and runs all the actions bound to this event%0A     * @param e the event type%0A     */%0A
     public v
@@ -1375,32 +1375,171 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Call a click event%0A     * @param localPosition the position of the click%0A     * @return if the click was successful%0A     */%0A
     public boole
@@ -1897,32 +1897,131 @@
 ntifier;%0A    %7D%0A%0A
+    /**%0A     * The UIContainer in which the element is stored%0A     * @return the container%0A     */%0A
     public UICon
@@ -2134,32 +2134,221 @@
 actions;%0A    %7D%0A%0A
+    /**%0A     * Adds a new action to the element%0A     * @param identifier the action's identifier%0A     * @param action the code to execute%0A     * @param event the event to listen to%0A     */%0A
     public void 

add UIElement javadocs