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.Team;
import net.mostlyoriginal.game.component.ui.ButtonListener;
import net.mostlyoriginal.game.component.ui.Transient;
import net.mostlyoriginal.game.events.DragEvent;
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) {

	}

	@Override
	protected void initialize() {
		super.initialize();

		entityFactoryManager.createBasicButton("refresh", 50 + 40 * 5, new ButtonListener() {
			@Override
			public void run() {
				restart();
			}

			@Override
			public boolean enabled() {
				return true;
			}
		}, 50);
	}

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


	/** restart all generation. */
	public void restart() {

		purgeAllTransientEntities();
		clearRoutes();
		clearTeamLayers();
	}

	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);
	}

	private void clearTeamLayers() {
		for (Team team : Team.values()) {
			layerManager.getTeamNavLayer(team).clear();
		}
	}

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

@@ -396,30 +396,25 @@
 nent.ui.
-ButtonListener
+Transient
 ;%0Aimport
@@ -438,35 +438,31 @@
 al.game.
-component.ui.Transi
+events.DeleteEv
 ent;%0Aimp
@@ -1909,197 +1909,64 @@
 %0A%0A%09@
-Overrid
+Subscrib
 e%0A%09p
-rotected void initialize() %7B%0A%09%09super.initialize();%0A%0A%09%09entityFactoryManager.createBasicButton(%22refresh%22, 50 + 40 * 5, new ButtonListener() %7B%0A%09%09%09@Override%0A%09%09%09public void run(
+ublic void listenerMoved(DragEvent event
 ) %7B%0A
-%09%09
 %09%09re
@@ -1978,89 +1978,8 @@
 ();%0A
-%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public boolean enabled() %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D, 50);%0A
 %09%7D%0A%0A
@@ -2021,13 +2021,14 @@
 ved(
- Drag
+Delete
 Even
@@ -2038,12 +2038,10 @@
 vent
+)
  
-)%0A%09
 %7B%0A%09%09
@@ -2060,16 +2060,20 @@
 %7D%0A%0A%0A%09/**
+%0A%09 *
  restart
@@ -2088,16 +2088,18 @@
 eration.
+%0A%09
  */%0A%09pub

Refresh when nodes are deleted.