package ertl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;

public class Controller implements ActionListener, WindowListener {
	private View v;
	private LoadBalancer load;
	
	public Controller() {
		this.load = new LoadBalancer(this);
		this.v = new View(this.load, this);
		this.load.start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (this.v.EventAddTask(e)) {
			Task task = this.v.getOverview().getTask();
			if (task != null) this.load.addTask(task);
		}
		if (this.v.EventAddRandomTask(e)) {
			Random random = new Random();
			this.load.addTask(new Task((random.nextInt(300 - 100 + 1) + 100), (random.nextInt(200 - 100 + 1) + 100), (random.nextInt(20 - 5 + 1) + 5)));
		}
		if (this.v.EventAddServer(e)) {
			this.v.getOverview().addServer();
		}
		if (this.v.EventAddRandomServer(e)) {
			Random random = new Random();
			this.load.addServer(0, (random.nextInt(1000 - 300 + 1) + 300), 0, (random.nextInt(1000 - 300 + 1) + 300), (random.nextInt(10 - 3 + 1) + 3));
		}
		if (this.v.EventAgentBased(e)) {
			if (this.v.getOverview().getAgentBased().isSelected()) {
				this.load.setMethod(1);
			} else {
				this.load.setMethod(0);
			}
		}
	}

	public View getV() {
		return v;
	}

	public void setV(View v) {
		this.v = v;
	}

	public LoadBalancer getLoad() {
		return load;
	}

	public void setLoad(LoadBalancer load) {
		this.load = load;
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {
		this.load.exit();
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}
}
