package com.galaxyvictor.gui;

import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

public class Window {

	private OnClickListener clickListener;
	private ArrayList<GuiComponent> childs = new ArrayList<GuiComponent>();
	
	public Window(){
		setOnClickListener(new OnClickListener() {

			@Override
			public boolean onMousePressed(float x, float y) {
				boolean clicked = false;
				for(GuiComponent child : childs){
					if(child.inside(x, y)){
						clicked = clicked || child.onMousePressed(x, y);
					}
				}
				return clicked;
			}
		});
	}
	
	public void addChild(GuiComponent child){
		childs.add(child);
	}
	
	public void render(){
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		for(GuiComponent child : childs){
			child.render();
		}
	}
	
	public void setOnClickListener(OnClickListener clickListener) {
		this.clickListener = clickListener;
	}
	
	public boolean onMousePressed(float x, float y){
		for(GuiComponent child : childs){
			if(child.inside(x, y) && child.onMousePressed(x, y)){
				return true;
			}
		}
		if(this.clickListener == null){
			return false;
		}
		return this.clickListener.onMousePressed(x, y);
	}

}
