package ui.controller;

import java.util.ArrayList;

import classes.Category;
import classes.AbstractCpsObject;
import classes.HolonElement;
import classes.HolonObject;
import ui.model.Model;

/**
 * Controller for Objects.
 * 
 * @author Gruppe14
 */
public class ObjectController {

	private Model model;
	private MultiPurposeController mpC;

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

	/**
	 * init default Power supply of the Power Plant.
	 */
	public void initHolonElements() {
		addNewElementIntoCategoryObject("Energy", "Power Plant", "Power", 1, 10000);
		addNewElementIntoCategoryObject("Building", "House", "TV", 2, -250);
		addNewElementIntoCategoryObject("Building", "House", "Fridge", 1, -500);
		addNewElementIntoCategoryObject("Building", "House", "Radio", 1, -100);
		addNewElementIntoCategoryObject("Building", "House", "PC", 3, -250);
		addNewElementIntoCategoryObject("Building", "House", "Light", 5, -50);
		addNewElementIntoCategoryObject("Building", "House", "Solar Panels", 1, 300);
	}

	/**
	 * Adds Element into a Object.
	 * 
	 * @param object
	 *            the Object
	 * @param element
	 *            the Element
	 */
	public void addElement(HolonObject object, HolonElement element) {
		object.getEleIdx().put(element.getEleName(), object.getElements().size());
		object.getElements().add(element);
	}

	/**
	 * Adds Element into a Object on the Canvas.
	 * 
	 * @param object
	 *            the Object
	 * @param element
	 *            the Element
	 */
	public void addElementIntoCanvasObject(HolonObject object, HolonElement element) {
		element.setSav("CVS");
		element.setObj(Integer.toString(object.getID()));
		addElement(object, element);
	}

	/**
	 * Add a new Element into a Object on the Canvas.
	 * 
	 * @param id
	 *            the Object ID
	 * @param element
	 *            the Name of the Element
	 * @param amount
	 *            the Amount
	 * @param energy
	 *            the Energy
	 */
	public void addNewElementIntoCanvasObject(int id, String element, int amount, float energy) {
		HolonElement ele = new HolonElement(element, amount, energy);
		addElementIntoCanvasObject((HolonObject) mpC.searchByID(id), ele);
	}

	/**
	 * Add Element into a Object in Category.
	 * 
	 * @param category
	 *            the Category
	 * 
	 * @param object
	 *            the Object
	 * @param element
	 *            the Element
	 */
	public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
		element.setSav(category);
		element.setObj(object);
		addElement((HolonObject) mpC.searchCatObj(mpC.searchCat(category), object), element);
	}

	/**
	 * Add a new Element into a Object in Category.
	 * 
	 * @param category
	 *            the Category
	 * @param object
	 *            the Object
	 * @param element
	 *            the Element Name
	 * @param energy
	 *            the Energy
	 * @param amount
	 *            the amount
	 */
	public void addNewElementIntoCategoryObject(String category, String object, String element, int amount,
			float energy) {

		HolonElement ele = new HolonElement(element, amount, energy);
		addElementIntoCategoryObject(category, object, ele);
	}

	/**
	 * deletes a Element from a given Object.
	 * 
	 * @param obj
	 *            the Oject
	 * @param ele
	 *            the Element
	 */
	public void deleteElement(HolonObject obj, HolonElement ele) {
		System.out.println(ele.getEleName() + " and " + obj.getEleIdx());
		mpC.decIdx(ele.getEleName(), obj.getEleIdx());
		obj.getEleIdx().remove(ele.getEleName());
		obj.getElements().remove(ele);
	}

	/**
	 * deletes a selectedObject.
	 * 
	 * @param obj
	 *            Cpsobject
	 */
	public void deleteSelectedObject(AbstractCpsObject obj) {
		model.getSelectedCpsObjects().remove(obj);
	}

	/**
	 * add an Object to selectedObject.
	 * 
	 * @param obj
	 *            AbstractCpsobject
	 */
	public void addSelectedObject(AbstractCpsObject obj) {
		model.getSelectedCpsObjects().add(obj);
	}

	/**
	 * deletes a Element from a given Canvas Object.
	 * 
	 * @param id
	 *            the ID
	 * @param eleid
	 *            the Element ID
	 */
	public void deleteElementInCanvas(int id, int eleid) {
		HolonObject object = (HolonObject) mpC.searchByID(id);
		HolonElement element = mpC.searchEleById(object, eleid);
		// mpC.searchEle(object, ele);
		System.out.println(object.getName() + " and " + element.getEleName());
		deleteElement(object, element);
	}

	/**
	 * deletes a Element from a given Category Object.
	 * 
	 * @param cat
	 *            the Category
	 * @param obj
	 *            the Object
	 * @param ele
	 *            the Element
	 */
	public void deleteElementInCategory(String cat, String obj, String ele) {
		Category category = mpC.searchCat(cat);
		HolonObject object = (HolonObject) mpC.searchCatObj(category, obj);
		HolonElement element = mpC.searchEle(object, ele);

		deleteElement(object, element);
	}

	/**
	 * Returns the ID of the selected Object 0 = no Object is selected.
	 * 
	 * @param id
	 *            the ID of the selected Object
	 */
	public void setSelectedObjectID(int id) {
		model.setSelectedObjectID(id);
	}

	/**
	 * sets clipBoardObjects.
	 * 
	 * @param list
	 *            of CpsObjects
	 */
	public void setClipboardObjects(ArrayList<AbstractCpsObject> list) {
		model.setClipboradObjects(list);
	}
}
