package net.mostlyoriginal.game.system.logic;

import com.artemis.Aspect;
import com.artemis.Entity;
import com.artemis.EntitySystem;
import com.artemis.annotations.Wire;
import com.artemis.utils.ImmutableBag;
import net.mostlyoriginal.api.event.common.Subscribe;
import net.mostlyoriginal.api.utils.EntityUtil;
import net.mostlyoriginal.game.component.ui.Transient;
import net.mostlyoriginal.game.events.DeleteEvent;
import net.mostlyoriginal.game.events.DragEvent;
import net.mostlyoriginal.game.events.TeamChangeEvent;
import net.mostlyoriginal.game.manager.EntityFactoryManager;
import net.mostlyoriginal.game.manager.LayerManager;
import net.mostlyoriginal.game.system.logic.analysis.NavigationGridCalculationSystem;
import net.mostlyoriginal.game.system.logic.analysis.PreferredRouteCalculationSystem;
import net.mostlyoriginal.game.system.logic.analysis.RouteCalculationSystem;
import net.mostlyoriginal.game.system.render.layer.DomainSystem;
import net.mostlyoriginal.game.system.render.layer.RoutePlotSystem;
import net.mostlyoriginal.game.system.render.layer.TechpointPressureSystem;
import net.mostlyoriginal.game.system.render.layer.TechpointSymmetrySystem;

/**
 * @author Daan van Yperen
 */
@Wire
public class RefreshHandlerSystem extends EntitySystem {

	EntityFactoryManager entityFactoryManager;

	private LayerManager layerManager;
	private RouteCalculationSystem routeCalculationSystem;
	private RoutePlotSystem routePlotSystem;
	private PreferredRouteCalculationSystem preferredRouteCalculationSystem;
	private TechpointSymmetrySystem techpointSymmetrySystem;
	private TechpointPressureSystem techpointPressureSystem;
	private DomainSystem domainSystem;
	private NavigationGridCalculationSystem navigationGridCalculationSystem;

	public RefreshHandlerSystem() {
		super(Aspect.getAspectForAll(Transient.class));
	}

	@Override
	protected void processEntities(ImmutableBag<Entity> entities) {

	}

	@Subscribe
	public void listenerMoved(DragEvent event) {
		restart();
	}

	@Subscribe
	public void listenerMoved(DeleteEvent event) {
		restart();
	}

	@Subscribe
	public void listenerMoved(TeamChangeEvent event) {
		restart();
	}


	/**
	 * restart all generation.
	 */
	public void restart() {
		clearRoutes();
	}

	private void clearRoutes() {
		// Core Dependencies
		navigationGridCalculationSystem.setDirty(true);
		routeCalculationSystem.setDirty(true);
		preferredRouteCalculationSystem.setDirty(true);

		// Separate view specific:
		routePlotSystem.setDirty(true);
		techpointSymmetrySystem.setDirty(true);
		techpointPressureSystem.setDirty(true);
		domainSystem.setDirty(true);
	}

	public void purgeAllTransientEntities() {
		EntityUtil.safeDeleteAll(getActives());
	}
}

@@ -86,22 +86,31 @@
 artemis.
-Entity
+ComponentMapper
 ;%0Aimport
@@ -128,22 +128,16 @@
 s.Entity
-System
 ;%0Aimport
@@ -192,28 +192,404 @@
 s.utils.
-ImmutableBag
+EntityBuilder;%0Aimport com.artemis.utils.ImmutableBag;%0Aimport net.mostlyoriginal.api.component.basic.Angle;%0Aimport net.mostlyoriginal.api.component.basic.Pos;%0Aimport net.mostlyoriginal.api.component.graphics.Anim;%0Aimport net.mostlyoriginal.api.component.graphics.Color;%0Aimport net.mostlyoriginal.api.component.graphics.Invisible;%0Aimport net.mostlyoriginal.api.component.graphics.Renderable
 ;%0Aimport
@@ -684,16 +684,106 @@
 tyUtil;%0A
+import net.mostlyoriginal.game.G;%0Aimport net.mostlyoriginal.game.api.DelayedEntitySystem;%0A
 import n
@@ -1633,16 +1633,46 @@
 ystem;%0A%0A
+import java.util.LinkedList;%0A%0A
 /**%0A * @
@@ -1746,16 +1746,23 @@
 extends 
+Delayed
 EntitySy
@@ -2247,16 +2247,93 @@
 nSystem;
+%0A%09private Entity refreshIndicator;%0A%0A%09protected ComponentMapper%3CAngle%3E mAngle;
 %0A%0A%09publi
@@ -2444,57 +2444,658 @@
 oid 
-processEntities(ImmutableBag%3CEntity%3E entities) %7B%0A
+initialize() %7B%0A%09%09Anim refresh = new Anim(%22refresh%22);%0A%09%09refresh.scale = 5;%0A%09%09refreshIndicator = new EntityBuilder(world).with(new Pos(0, G.CANVAS_HEIGHT-120), new Color(1f,1f,1f,0.5f), new Angle(0,40,40), refresh, new Renderable(4000)).build();%0A%09%09setPrerequisiteSystems(preferredRouteCalculationSystem);%0A%09%7D%0A%0A%09@Override%0A%09protected void processEntities(ImmutableBag%3CEntity%3E entities) %7B%0A%09%09Angle angle = mAngle.get(refreshIndicator);%0A%09%09angle.rotation += world.delta * -400f;%0A%09%09super.processEntities(entities);%0A%09%7D%0A%0A%09@Override%0A%09protected void collectJobs(ImmutableBag%3CEntity%3E entities, LinkedList%3CJob%3E jobs) %7B%0A%09%09refreshIndicator.edit().add(new Invisible());
 %0A%09%7D%0A
@@ -3443,16 +3443,87 @@
 tes() %7B%0A
+%0A%09%09setDirty(true);%0A%09%09refreshIndicator.edit().remove(Invisible.class);%0A%0A
 %09%09// Cor

Add updating indicator.