package CK.IA;

import java.util.concurrent.TimeUnit;

public class GoalSimulator {
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		GoalSimulator goalSimulator = new GoalSimulator();
	}

	private EnvironmentAgent environment;
	private boolean running;

	/**
	 * Initializes the goal simulator as a stand-alone thread, including the environment inside it. After the call the environment will be paused, all
	 * the agents and entities will have been initialized and ready for execution.
	 */
	public GoalSimulator() {
		running = true;
		environment = new EnvironmentAgent();
		initializeEnvironment();
		environment.setPaused(false);
		executeInParallel();
	}

	public void executeInParallel() {
		new Thread("goal simulator thread") {
			public void run() {
				System.out.println("Goal Simulator's thread started");
				while (running) {
					executeCycle();
					if (isPaused()) {
						try {
							Thread.sleep(GoalSimulatorConfig.SIMULATOR_MS_PER_FRAME_PAUSED);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					} else {
						try {
							Thread.sleep(GoalSimulatorConfig.SIMULATOR_MS_PER_FRAME);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}.start();
	}

	public void addSoldierAgents() {
		float soldiers_initial_health = GoalSimulatorConfig.SOLDIER_HEALTH_INITIAL;
		// soldier agents
		for (int i = 0; i < GoalSimulatorConfig.NUMBER_SOLDIERS_RED; i++) {
			environment.addSoldierAgent(new SoldierAgent(0, null, GoalSimulatorConfig.SOLDIER_RADIUS, soldiers_initial_health, environment));
		}
		for (int i = 0; i < GoalSimulatorConfig.NUMBER_SOLDIERS_BLUE; i++) {
			environment.addSoldierAgent(new SoldierAgent(1, null, GoalSimulatorConfig.SOLDIER_RADIUS, soldiers_initial_health, environment));
		}
	}

	public void addSpawnAgents() {
		// spawn agents (ONLY ONE FOR EACH TEAM!!!)
		TimeUnit tu = TimeUnit.SECONDS;
		long spawn_interval_ns = tu.toNanos(GoalSimulatorConfig.SPAWN_INTERVAL_SECONDS);
		long blue_phase_ns = tu.toNanos(GoalSimulatorConfig.SPAWN_PHASE_SECONDS_BLUE);
		long red_phase_ns = tu.toNanos(GoalSimulatorConfig.SPAWN_PHASE_SECONDS_RED);
		environment.addSpawnAgent(new SpawnAgent(0, GoalSimulatorConfig.SPAWN_POSITION_RED.clone(), GoalSimulatorConfig.SPAWN_RADIUS_RED,
				spawn_interval_ns, red_phase_ns, environment));
		environment.addSpawnAgent(new SpawnAgent(1, GoalSimulatorConfig.SPAWN_POSITION_BLUE.clone(), GoalSimulatorConfig.SPAWN_RADIUS_BLUE,
				spawn_interval_ns, blue_phase_ns, environment));
	}

	public void executeCycle() {
		environment.executeCycle();
	}

	public EnvironmentAgent getEnvironment() {
		return environment;
	}

	public void initializeEnvironment() {
		addSpawnAgents();
		addSoldierAgents();
		System.out.println("Goal Simulator's environment initialized");
	}

	public boolean isPaused() {
		return environment.isPaused();
	}

	public boolean isRunning() {
		return running;
	}

	public void setPaused(boolean paused) {
		environment.setPaused(paused);
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

}
