package ui.controller;

import java.awt.Point;
import java.util.ArrayList;

import classes.CpsEdge;
import classes.CpsNode;
import classes.AbstractCpsObject;
import classes.HolonObject;
import classes.HolonSwitch;
import classes.Position;
import interfaces.CategoryListener;
import interfaces.ObjectListener;
import ui.model.Model;

/**
 * Controller for the Canvas.
 * 
 * @author Gruppe14
 */
public class CanvasController {

	private Model model;
	private MultiPurposeController mpC;

	/**
	 * Constructor.
	 * 
	 * @param model
	 *            the Model
	 * @param mp
	 *            the MultipurposeController
	 */
	public CanvasController(Model model, MultiPurposeController mp) {
		this.model = model;
		this.mpC = mp;
	}

	/**
	 * Add an CpsObject to the model and notify the ObjectListener for update.
	 * 
	 * @param object
	 *            CpsObject to be added.
	 */
	public void addObject(AbstractCpsObject object) {
		model.getCvsObjIdx().put(object.getID(), model.getObjectsOnCanvas().size());
		model.getObjectsOnCanvas().add(object);
		notifyObjListeners();
	}

	/**
	 * Add a new Object.
	 * 
	 * @param object
	 *            the Object
	 */
	public void addNewObject(AbstractCpsObject object) {
		object.setSav("CVS");
		object.setConnections(new ArrayList<CpsEdge>());
		addObject(object);
	}

	/**
	 * adds the ObjectListener.
	 * 
	 * @param objLis
	 *            ObjectListener
	 */
	public void addObjectListener(ObjectListener objLis) {
		model.getObjectListeners().add(objLis);
	}

	/**
	 * notifies all listeners about changes in the Canvas.
	 */
	public void notifyObjListeners() {
		for (ObjectListener l : model.getObjectListeners()) {
			l.onChange(model.getObjectsOnCanvas());
		}
	}

	/**
	 * Deletes an CpsObject on the Canvas and its connections.
	 * 
	 * @param obj
	 *            AbstractCpsObject
	 */
	public void deleteObjectOnCanvas(AbstractCpsObject obj) {
		CpsEdge e = null;
		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
			for (CpsEdge p : cps.getConnections()) {
				if (p.getA() == obj || p.getB() == obj) {
					e = p;
				}
			}
			if (!model.getClipboradObjects().contains(cps)) {
				cps.getConnectedTo().remove(e);
			}
			model.getEdgesOnCanvas().remove(e);
		}
		mpC.decIdx(obj.getID(), model.getCvsObjIdx());
		model.getCvsObjIdx().remove(obj.getID());
		model.getObjectsOnCanvas().remove(obj);

	}

	/**
	 * Add an edge to the Canvas.
	 * 
	 * @param edge
	 *            the edge
	 */
	public void addEdgeOnCanvas(CpsEdge edge) {
		model.getEdgesOnCanvas().add(edge);
	}

	/**
	 * Removes an Edge from the Canvas.
	 * 
	 * @param edge
	 *            the edge to remove
	 */
	public void removeEdgesOnCanvas(CpsEdge edge) {
		edge.getA().getConnections().remove(edge);
		edge.getB().getConnections().remove(edge);
		model.getEdgesOnCanvas().remove(edge);
	}

	/**
	 * Copy all Selected Objects.
	 */
	@SuppressWarnings("unchecked")
	public void copyObjects() {
		model.setClipboradObjects((ArrayList<AbstractCpsObject>) model.getSelectedCpsObjects().clone());
	}

	/**
	 * Paste all Selected Objects.
	 * 
	 * @param p
	 *            the mouse Position
	 */
	public void pasteObjects(Point p) {
		model.getSelectedCpsObjects().clear();
		AbstractCpsObject tCps = null;
		int x = Integer.MAX_VALUE, y = Integer.MAX_VALUE;

		// Location whre to copy the Elements
		for (AbstractCpsObject cps : model.getClipboradObjects()) {
			if (cps.getPosition().x < x) {
				x = cps.getPosition().x;
			}
			if (cps.getPosition().y < y) {
				y = cps.getPosition().y;
			}
		}

		ArrayList<AbstractCpsObject> tempList = new ArrayList<>();

		// Objects
		for (AbstractCpsObject cps : model.getClipboradObjects()) {
			if (cps instanceof HolonObject) {
				tCps = new HolonObject((HolonObject) cps);
			} else if (cps instanceof HolonSwitch) {
				tCps = new HolonSwitch((HolonSwitch) cps);
			} else {
				tCps = new CpsNode("Node");
			}
			tCps.setPosition(new Position(p.x + (cps.getPosition().x - x), p.y + (cps.getPosition().y - y)));
			tempList.add(tCps);
			addObject(tCps);
			// MODEL.getSelectedCpsObjects().add(tCps);
		}

		// Edges
		boolean newEdge = true;
		for (AbstractCpsObject cps : model.getClipboradObjects()) {
			for (CpsEdge e : cps.getConnectedTo()) {
				// A and B of e in the copied Elements?
				if (model.getClipboradObjects().indexOf(e.getA()) != -1
						&& model.getClipboradObjects().indexOf(e.getB()) != -1) {
					AbstractCpsObject a = tempList.get(model.getClipboradObjects().indexOf(e.getA()));
					AbstractCpsObject b = tempList.get(model.getClipboradObjects().indexOf(e.getB()));
					// was this Edge created or not?
					for (CpsEdge et : tempList.get(model.getClipboradObjects().indexOf(cps)).getConnectedTo()) {
						for (CpsEdge etA : et.getA().getConnectedTo()) {
							if (et.getA() == a && et.getB() == b) {
								newEdge = false;
							}
						}
						for (CpsEdge etB : et.getB().getConnectedTo()) {
							if (et.getA() == a && et.getB() == b) {
								newEdge = false;
							}
						}
					}
					if (newEdge) {
						CpsEdge tempE = new CpsEdge(tempList.get(model.getClipboradObjects().indexOf(e.getA())), // A
								tempList.get(model.getClipboradObjects().indexOf(e.getB())), /* B */
								e.getCapacity());
						addEdgeOnCanvas(tempE);
					}
					newEdge = true;
				}
			}
		}

	}

	/**
	 * Cut all Selected Objects.
	 */
	@SuppressWarnings("unchecked")
	public void cutObjects() {
		model.setClipboradObjects((ArrayList<AbstractCpsObject>) model.getSelectedCpsObjects().clone());

		for (AbstractCpsObject cps : model.getClipboradObjects()) {
			deleteObjectOnCanvas(cps);
		}

		model.getSelectedCpsObjects().clear();
	}
}
