package de.unidortmund.dependencies;
public class Variable {
	private String name;
	private Slot slot;
	
	private Variable(String name, Slot slot) {
		this.name = name;
		this.slot = slot;
	}
	
	/**
	 * Erzeugt eine einfache Variable mit dem Namen
	 * @param name und dem Wert
	 * @param value.
	 */
	public Variable(String name, Value value) {
		this.name = name;
		slot = new Slot(value, 3);
	}
	
	/**
	 * Erzeugt ein Array mit dem Namen
	 * @param name und den Werten
	 * @param contents
	 */
	public Variable(String name, Value[] values) {
		this.name = name;
		slot = new Slot(values, 3);
	}

	
	public Variable(String name, Variable[] fields) {
		this.name = name;
		slot = new Slot(fields, 3);
	}

	
	/**
	 * Gibt den Pfad der Variablen
	 */
	public String toString() {
		return name + " " + slot.toString();
	}

	public Slot getSlot() {
		return slot;
	}
	

	
	public Slot getFirstSlotAtLevel(int level) {
		if (level == 3) {
			return slot;
		} else {
			return slot.getFirstSlotAtLevel(level);
		}
	}
	
	public boolean isComposite() {
		return slot.isComposite();
	}
	
	public Variable getVariableAtIndex(int index) {
		assert isComposite();
		Slot slot2 = slot.getLastSlotAtLevel(2);
		Slot slot30 = (Slot) slot2.getContent();
		Slot mySlot = Memory.getSlot(3, slot30.getIndex() + index);
		return new Variable(name + "[" + index + "]", mySlot);

	}
	
	public Variable[] getAllIndexVariables() {
		assert isComposite();
		Slot slot2 = slot.getLastSlotAtLevel(2);
		int number = slot2.getNumberOfElements();
		Variable[] variables = new Variable[number];
		int zeroIndex = ((Slot) slot2.getContent()).getIndex();
		for (int i = 0; i < number; i ++) {
			Slot mySlot = Memory.getSlot(3, zeroIndex + i);
			variables[i] = new Variable(name + "[" + i + "]", mySlot);
		}
		return variables;
	}
	
	public Value getValue() {
		assert !isComposite();
		return slot.getValue();
	}


}
