package electrosphere.controls;

import electrosphere.renderer.ui.events.MouseEvent;
import electrosphere.renderer.ui.events.ScrollEvent;

/**
 * A low level control inside the engine
 */
public class Control {

    public static enum ControlType {
        KEY,
        MOUSE_BUTTON,
        MOUSE_MOVEMENT,
        MOUSE_SCROLL,
    }

    ControlType type;
    boolean state;
    int keyValue;
    String name;
    String description;
    boolean rebindable;
    ControlMethod onPress;
    ControlMethod onRelease;
    ControlMethod onRepeat;
    ControlMethod onClick;
    MouseCallback onMove;
    ScrollCallback onScroll;

    float pressFrame = 0;
    float repeatTimeout = 0;

    public boolean isIsKey() {
        return type == ControlType.KEY;
    }

    public boolean isIsMouse() {
        return type == ControlType.MOUSE_BUTTON;
    }

    public boolean isState() {
        return state;
    }

    public ControlType getType(){
        return type;
    }

    public int getKeyValue() {
        return keyValue;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    public Control(ControlType type, int keyValue, boolean rebindable, String name, String description) {
        this.type = type;
        this.keyValue = keyValue;
        this.state = false;
        this.rebindable = rebindable;
        this.name = name;
        this.description = description;
    }

    public void setOnPress(ControlMethod method){
        onPress = method;
    }

    public void setOnRelease(ControlMethod method){
        onRelease = method;
    }

    public void setOnRepeat(ControlMethod method){
        onRepeat = method;
    }

    public void setOnMove(MouseCallback method){
        onMove = method;
    }

    public void setOnClick(ControlMethod method){
        onClick = method;
    }

    public void setOnScroll(ScrollCallback callback){
        onScroll = callback;
    }

    public void onPress(){
        if(onPress != null){
            onPress.execute();
        }
    }

    public void onRelease(){
        if(onRelease != null){
            onRelease.execute();
        }
    }

    public void onRepeat(){
        if(onRepeat != null){
            onRepeat.execute();
        }
    }

    public void onMove(MouseEvent event){
        if(onMove != null){
            onMove.execute(event);
        }
    }

    public void onClick(){
        if(onClick != null){
            onClick.execute();
        }
    }

    public void onScroll(ScrollEvent event){
        if(onScroll != null){
            onScroll.execute(event);
        }
    }

    public float getPressFrame(){
        return this.pressFrame;
    }

    public void setPressFrame(float frame){
        pressFrame =  frame;
    }

    public float getRepeatTimeout(){
        return this.repeatTimeout;
    }

    public void setRepeatTimeout(float timeout){
        repeatTimeout = timeout;
    }

    public boolean isRebindable(){
        return rebindable;
    }

    public String getName(){
        return name;
    }

    public String getDescription(){
        return description;
    }
    

    public interface ControlMethod {
        public void execute();
    }

    public interface MouseCallback {
        public void execute(MouseEvent event);
    }

    public interface ScrollCallback {
        public void execute(ScrollEvent event);
    }
    
}
