package ei.engine.ui;

import ei.engine.math.Vector2f;
import ei.engine.math.Vector3f;
import ei.engine.scene.Entity;
import ei.engine.scene.Node;

public class Button extends UiComponent{
	private Entity normalButton;
	private Entity ontopButton;
	private Entity presedButton;
	private Entity disabledButton;
	private Node buttonNode;
	private Vector2f size;
	private boolean enabled;
	
	public Button(String name, Vector2f s){
		super(name);
		buttonNode = new Node(name+"Node");
		size = s;
		enabled = true;
	}
	
	public void setEnabled(boolean b){
		enabled = b;
	}
	
	public void setButtonSprite(Entity s){
		normalButton = s;
	}
	
	public void setOnTopButtonSprite(Entity s){
		ontopButton = s;
	}
	
	public void setPressedButtonSprite(Entity s){
		presedButton = s;
	}
	
	public void setDisabledButtonSprite(Entity s){
		disabledButton = s;
	}
	
	public void mousePos(int x, int y){
		if(!enabled){
			if(disabledButton != null && !buttonNode.contains(disabledButton)){
				buttonNode.clear();
				buttonNode.add(disabledButton);
			}
		}
		else if(size != null){
			Vector3f pos = buttonNode.getLocation();
			if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2)) 
					&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
				if(ontopButton != null && !buttonNode.contains(ontopButton)){
					buttonNode.clear();
					buttonNode.add(ontopButton);
				}
			}
			else{
				if(normalButton != null && !buttonNode.contains(normalButton)){
					buttonNode.clear();
					buttonNode.add(normalButton);
				}
			}
		}
	}
	
	public void mouseDown(int x, int y, int event){
		if(size != null && enabled){
			Vector3f pos = buttonNode.getLocation();
			if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2)) 
					&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
				if(presedButton != null && !buttonNode.contains(presedButton)){
					buttonNode.clear();
					buttonNode.add(presedButton);
				}
				castEvent();
			}
		}
	}
	
	public Node getNode(){
		return buttonNode;
	}
}
