package exampleAlgorithms;

import api.CpsAlgorithm;
import classes.AbstractCpsObject;
import classes.CpsEdge;
import classes.CpsUpperNode;
import ui.controller.Control;
import ui.model.Model;

public class RepairAllEdges implements CpsAlgorithm {

	@Override
	public void runAlgorithm(Model model, Control controller) {
		repairAllEdgesOnMainCanvas(model);
	}

	/**
	 * Repairs all Edges on the main Canvas.
	 * 
	 * @param model
	 *            the Model
	 */
	private void repairAllEdgesOnMainCanvas(Model model) {
		for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
			if (obj instanceof CpsUpperNode) {
				repairAllEdgesInUpperNode((CpsUpperNode) obj);
			}
			for (CpsEdge e : obj.getConnections()) {
				e.setWorkingState(true);
			}
		}
	}

	private void repairAllEdgesInUpperNode(CpsUpperNode uNode) {
		for (AbstractCpsObject obj : uNode.getNodes()) {
			if (obj instanceof CpsUpperNode) {
				repairAllEdgesInUpperNode((CpsUpperNode) obj);
			}
			for (CpsEdge e : obj.getConnections()) {
				e.setWorkingState(true);
			}
		}
	}
}