package exampleAlgorithms;

import api.CpsAlgorithm;
import ui.controller.Control;
import ui.model.Model;
import ui.view.UpperNodeCanvas;
import classes.*;

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.setState(true);
			}
		}
	}

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