package ui.controller;

import java.awt.Dimension;
import java.util.ArrayList;

import classes.AbstractCpsObject;
import classes.Constants;
import classes.CpsUpperNode;
import classes.HolonBody;
import classes.HolonElement;
import classes.HolonObject;
import classes.SubNet;
import classes.Vector2d;
import ui.model.Model;

public class HolonCanvasController {

	// Ball objects
	private ArrayList<HolonBody> bodies = new ArrayList<>();
	private int subCount;
	private Dimension center;
	private Model model;
	private ArrayList<HolonBody> sortedSize = new ArrayList<>();
	private ArrayList<HolonBody> sortedDist = new ArrayList<>();
	private int toDrag;
	private boolean beingDragged;

	/**
	 * Constructor.
	 * 
	 * @param model
	 *            the Model
	 * @param mp
	 *            the MultipurposeController
	 */
	public HolonCanvasController(Model model) {
		this.model = model;
	}

	// sort the HolonBodys after size
	public void rearrangeAfterSize() {
		int j = 0;
		sortedSize.addAll(bodies);
		insertionSizeSort(sortedSize);
		sortedDist.addAll(bodies);

		ArrayList<Vector2d> pos = insertionDistSort(sortedDist);
		for (int i = 0; i < subCount; i++) {
			for (j = 0; j < subCount; j++) {
				if (sortedSize.get(subCount - 1 - i).getId() == bodies.get(j).getId()) {
					bodies.get(j).position = pos.get(i);
					break;
				}
			}
		}
	}

	// updates the bodies according to the changes of subnets
	public void addNewBodies(int subCount, ArrayList<SubNet> subnets, Dimension center, int HolonBodyScale) {
		this.subCount = subCount;
		this.center = center;
		// find correct color for existing HolonBodys
		ArrayList<HolonBody> newBodies = new ArrayList<>();
		for (int i = 0; i < subCount; i++) {
			for (int j = 0; j < bodies.size(); j++) {
				if (model.getSubNetColors().get(i) == bodies.get(j).getColor()) {
					bodies.get(j).setRadius((subnets.get(i).getObjects().size() * 5 + 10) * HolonBodyScale / 100);
					bodies.get(j).setMass((float) Math.pow((subnets.get(i).getObjects().size() + 1) * 5, 3));
					newBodies.add(bodies.get(j));
					newBodies.get(i).setId(i);
					break;
				}
			}
		}

		bodies = newBodies;
		// adding new HolonBodys
		for (int i = bodies.size(); i < subCount; i++) {
			float radius = (subnets.get(i).getObjects().size() * 5 + 10) * HolonBodyScale / 100;
			HolonBody temp = new HolonBody((float) (center.width + Math.pow(-1, i)), (float)(center.height + Math.pow(-1, i)), radius,
					(float) Math.pow((subnets.get(i).getObjects().size() + 1) * 5, 3), model.getSubNetColors().get(i));
			temp.setId(i);
			bodies.add(temp);
		}
	}

	public int getActiveElements(ArrayList<AbstractCpsObject> objects) {
		int val = 0;
		for (AbstractCpsObject obj : objects) {
			if (obj instanceof HolonObject) {
				for (HolonElement ele : ((HolonObject) obj).getElements()) {
					if (ele.getActive()) {
						val += 1;
					}
				}
			} else if (obj instanceof CpsUpperNode) {
				val += getTotalProduction(((CpsUpperNode) obj).getNodes());
			}
		}
		return val;
	}

	public int getTotalProducers(ArrayList<AbstractCpsObject> objects) {
		float val = 0;
		int prod = 0;
		int tStep = model.getCurIteration();
		for (AbstractCpsObject obj : objects) {
			if (obj instanceof HolonObject) {
				for (HolonElement ele : ((HolonObject) obj).getElements()) {
					if (ele.getEnergyAt()[tStep] > 0 && ele.getActive()) {
						val += ele.getEnergyAt()[tStep] * ele.getAmount();
					}
				}
				if (val > 0)
					prod += 1;
			} else if (obj instanceof CpsUpperNode) {
				val += getTotalProduction(((CpsUpperNode) obj).getNodes());
			}
		}
		return prod;
	}

	public int getTotalElements(ArrayList<AbstractCpsObject> objects) {
		int val = 0;
		for (AbstractCpsObject obj : objects) {
			if (obj instanceof HolonObject) {
				val += ((HolonObject) obj).getElements().size();
			} else if (obj instanceof CpsUpperNode) {
				val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
			}
		}
		return val;
	}

	public float getTotalConsumption(ArrayList<AbstractCpsObject> objects) {
		float val = 0;
		int tStep = model.getCurIteration();
		for (AbstractCpsObject obj : objects) {
			if (obj instanceof HolonObject) {
				for (HolonElement ele : ((HolonObject) obj).getElements()) {
					if (ele.getEnergyAt()[tStep] < 0 && ele.getActive()) {
						val += ele.getEnergyAt()[tStep] * ele.getAmount();
					}
				}
			} else if (obj instanceof CpsUpperNode) {
				val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
			}
		}
		return val;
	}

	public float getTotalProduction(ArrayList<AbstractCpsObject> objects) {
		float val = 0;
		int tStep = model.getCurIteration();
		for (AbstractCpsObject obj : objects) {
			if (obj instanceof HolonObject) {
				for (HolonElement ele : ((HolonObject) obj).getElements()) {
					if (ele.getEnergyAt()[tStep] > 0 && ele.getActive()) {
						val += ele.getEnergyAt()[tStep] * ele.getAmount();
					}
				}
			} else if (obj instanceof CpsUpperNode) {
				val += getTotalProduction(((CpsUpperNode) obj).getNodes());
			}
		}
		return val;
	}

	public void updateBodies(float elapsedSeconds) {
		// step the position of movable objects based off their velocity/gravity
		// and elapsedTime
		for (int i = 0; i < subCount; i++) {
			if (toDrag != bodies.get(i).getId() || !beingDragged) {
				bodies.get(i).position.setX(
						(float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
								- ((bodies.get(i).position.getX() - center.getWidth()) / (50 + subCount))));
				bodies.get(i).position.setY(
						(float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
								- ((bodies.get(i).position.getY() - center.getHeight()) / (50 + subCount))));

				if (Math.abs(bodies.get(i).velocity.getX()) < Constants.epsilon)
					bodies.get(i).velocity.setX(0);
				if (Math.abs(bodies.get(i).velocity.getY()) < Constants.epsilon)
					bodies.get(i).velocity.setY(0);
			}
		}
		checkCollisions();
	}

	// Insertion sort for Sweep and Prune
	public void insertionSort(ArrayList<HolonBody> a) {
		for (int p = 1; p < subCount; p++) {
			Comparable<HolonBody> tmp = a.get(p);
			int j = p;

			for (; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--)
				a.set(j, a.get(j - 1));

			a.set(j, (HolonBody) tmp);
		}
	}

	// Insertion sort for subnet size
	private void insertionSizeSort(ArrayList<HolonBody> a) {
		for (int p = 1; p < subCount; p++) {
			HolonBody tmp = a.get(p);
			int j = p;

			for (; j > 0 && tmp.compareSizeTo(a.get(j - 1)) < 0; j--)
				a.set(j, a.get(j - 1));

			a.set(j, (HolonBody) tmp);
		}
	}

	// Insertion sort for HolonBody distance
	private ArrayList<Vector2d> insertionDistSort(ArrayList<HolonBody> a) {
		ArrayList<Vector2d> pos = new ArrayList<>();
		for (int p = 1; p < subCount; p++) {
			HolonBody tmp = a.get(p);
			int j = p;

			for (; j > 0 && tmp.compareDistTo(a.get(j - 1), center) < 0; j--)
				a.set(j, a.get(j - 1));

			a.set(j, (HolonBody) tmp);
		}
		for (int i = 0; i < subCount; i++)
			pos.add(a.get(i).position);
		return pos;
	}

	public void checkCollisions() {
		insertionSort(bodies);

		for (int i = 0; i < subCount; i++) {
			// Ball to Ball collision
			for (int j = i + 1; j < subCount; j++) {
				if ((bodies.get(i).position.getX() + bodies.get(i).getRadius()) < (bodies.get(j).position.getX()
						- bodies.get(j).getRadius()))
					break;

				if ((bodies.get(i).position.getY() + bodies.get(i).getRadius()) < (bodies.get(j).position.getY()
						- bodies.get(j).getRadius())
						|| (bodies.get(j).position.getY() + bodies.get(j).getRadius()) < (bodies.get(i).position.getY()
								- bodies.get(i).getRadius()))
					continue;

				bodies.get(i).resolveCollision(bodies.get(j));

			}
		}

	}

	public ArrayList<HolonBody> getBodies() {
		return bodies;
	}

	public void setDrag(boolean beingDragged) {
		this.beingDragged = beingDragged;
	}

	public void setBodyToDrag(int i) {
		this.toDrag = i;
	}

}
