package projectzombie.menu.gui;

import java.util.ArrayList;

import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;

public class GUIButtonGroup implements GUIContainer
{
	private ArrayList<GUIButton> buttons = new ArrayList<GUIButton>();
	private Vec2d pos = new Vec2d(0, 0);
	
	public void setPos(Vec2d pos) {
		this.pos = pos;
		
		for(int i=0;i<buttons.size();i++) {
			GUIButton b = buttons.get(i);
			b.setPos(new Vec2d(pos.x, pos.y + 0.5 * -i * 3.6));
			
			if(i > 0) {
				b.setNeighbour(buttons.get(i - 1), GUISelectableDirection.UP);
			}
			
			if(i < buttons.size() - 1) {
				b.setNeighbour(buttons.get(i + 1), GUISelectableDirection.DOWN);
			}
		}
	}
	
	@Override
	public void add(GUIComponent c) {
		if(c instanceof GUIButton) {
			GUIButton b = (GUIButton) c;
			buttons.add(b);
			setPos(pos);
		}
	}

	@Override
	public void render(Matrix4 matrix, Vec2d mousePos, boolean canHover) {
		for(GUIButton b : buttons) {
			b.render(matrix, mousePos, canHover);
		}
	}

	@Override
	public boolean checkMouseHover(Vec2d pos) {
		for(GUIButton b : buttons) {
			if(b.checkMouseHover(pos)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void onMouseClick(Vec2d pos) {
		for(GUIButton b : buttons) {
			if(b.checkMouseHover(pos)) {
				b.onMouseClick(pos);
			}
		}
	}

	@Override
	public void onBack() {
		for(GUIButton b : buttons) {
			b.onBack();
		}
	}
	
	public GUIButton get(int n) {
		return buttons.get(n);
	}
	
	public int size() {
		return buttons.size();
	}
	
	@Override
	public void onActivate() {
		for(GUIButton b : buttons) {
			b.onActivate();
		}
	}

	@Override
	public void update(Vec2d mousePos) {
		for(GUIButton b : buttons) {
			b.update(mousePos);
		}
	}
	
	@Override
	public void onRightClick(Vec2d pos) {
		for(GUIButton b : buttons) {
			b.onRightClick(pos);
		}
	}
	
	@Override
	public void onScroll(Vec2d mousePos, double amount) {
		for(GUIButton c : buttons) {
			c.onScroll(mousePos, amount);
		}
	}
	
	@Override
	public void clear() {
		buttons.clear();
	}
	
	@Override
	public GUIItemSlot getHoveringItemSlot(Vec2d mousePos) {
		return null;
	}

}
