class WhatPanel {
	constructor() {
		this._eventSupport = new MyEventSupport();
		this._selectedItem = null;

		this.emptyWhatDefault = {"name":"Schema","onclick":"javascript:TheLastApp.what.select(null)"};

		
		this.currentWhat = this.emptyWhatDefault
		this._objectStack = [this.emptyWhatDefault];

	}
	
	init(controller) {	
		if (controller != null) {
			this.controller = controller;

			this.controller._eventSupport.addEventListener("objectStackWillChange",this,this.renderObjectStack.bind(this));
			this.controller._eventSupport.addEventListener("select",this,this.onSelect.bind(this));
		}
		return this;
	}

	
	get objectStack() {
		return this._objectStack;
	}
	set objectStack(newValue) {
		this.controller._eventSupport.getEvent("objectStackWillChange").fire(this,{"oldVlaue":this._objectStack,"newValue":newValue,"action":"reset"});
		this._objectStack = newValue;
	}
	
	push2objectStack(newValue) {
		var oldValue = this._objectStack;
		this.controller._eventSupport.getEvent("objectStackWillChange").fire(this,{"oldVlaue":oldValue,"newValue":newValue,"action":"push"});
		this._objectStack.push(newValue);
	}
	

	setCurrentWhat(newValue) {
		this.previousValue = this._objectStack.pop();
		this.controller._eventSupport.getEvent("objectStackWillChange").fire(this,{"oldVlaue":this.previousValue,"newValue":newValue,"action":"set"});
		this._objectStack.push(newValue);
	}
	
	
	
	//caled from onclick in the document.elements
	select(value) {
		var item = value;
		if (item == null)
			item = {type:"StructrSchema",name:"Structr Schema"};
		console.log(item);
		this.controller._eventSupport.getEvent("select").fire(this,item);
		
	}
	
	//the event called by select()
	onSelect(event,data) {
		this._selectedItem = data;
	}
	
	
	renderObjectStack(event,data) {
		var This = event.target;
		
		
		var whatItemBean = document.weBeans.lookup("DefaultItem");
		var element = document.getElementById("whatStack");
		data.newValue.onclick ="javascript:TheLastApp.what.select('"+data.newValue.name+"')";

		whatItemBean.apply(element,data.newValue);
		
		for(var i = This._objectStack.length-1; i>0; i--) {
			whatItemBean.append(element,This._objectStack[i-1]);
		}
		
		
	}
	
	
	
}