package FIT.Menus;

import java.util.ArrayList;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import FIT.AgentGUI;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Buttons extends org.eclipse.swt.widgets.Composite {
	private AgentGUI myParent;
	private ArrayList<Button> buttons = new ArrayList<Button>();
	private final int S = 5;
	

	public Buttons(Composite parent, int style, int num, int rows, String[] labels, String[] tasks) {
		super(parent, style);
		myParent = (AgentGUI)this.getParent();
		initGUI(num, rows, labels, tasks);
	}

	private void initButtons (double num, double rows, String[] labels, String[] tasks) {
		int cols = (int)Math.ceil(num / rows);
		int leftOver = (int)num % cols;
		int X = this.getSize().x / cols;
		int Y = (int)(this.getSize().y / rows);
		int offset = ((cols-leftOver)*X/2);
		if (leftOver == 0) rows++;
		int i;
		for (i = 0; i < rows-1; i++) {
			for (int j = 0; j < cols; j++) {
				if (labels.length > buttons.size() && labels[buttons.size()] != null) {
					makeButton(0, X, Y, i, j, labels[buttons.size()], tasks[buttons.size()]);
				} else {
					makeButton(0, X, Y, i, j, "", "");
				}
			}
		}
		for (int j = 0; j < leftOver; j++) {
			if (labels.length > buttons.size() && labels[buttons.size()] != null) {
				makeButton(offset, X, Y, i, j, labels[buttons.size()], tasks[buttons.size()]);
			} else {
				makeButton(offset, X, Y, i, j, "", "");
			}
		}
		
	}
	
	private void initGUI(int num, int rows, String[] labels, String[] tasks) {
		try {
			this.setLayout(new FormLayout());
			this.setSize(600, 350);
			initButtons (num, rows, labels, tasks);
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void makeButton (int start, int X, int Y, int i, int j, String label, String task) {
		Button temp = new Button(this, SWT.PUSH | SWT.CENTER);
		FormData Data = new FormData();
		Data.width = X - 2*S;
		Data.height = Y - 2*S;
		Data.left =  new FormAttachment(0, 1000, start + j*X + S);
		Data.top =  new FormAttachment(0, 1000, i*Y + S);
		temp.setLayoutData(Data);
		final int id = buttons.size() + 1;
		final String taskid = task;
		temp.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				System.out.println("You pressed button " + id);
				myParent.handleButton(taskid);
			}
		});
		temp.setText(label);
		buttons.add(temp);
	}
	

	
}
