/*
	Copyright (c) 2006-2007, ePages GmbH
	All Rights Reserved.

*/
dojo.provide('epages.cartridges.de_epages.content.ClassStore');
dojo.require('epages.lang.hash');
dojo.require('epages.io.json');
dojo.require('dojo.data.api.Identity');
dojo.require('dojo.data.util.simpleFetch');

dojo.declare(
	'epages.cartridges.de_epages.content.ClassStore',
	[dojo.data.api.Identity],
	{
		/**
		 * public properties
		 */
		actionInfo         : "JSONContentClasses", // server ViewAction to get children information
		url                : '?',              // request url like '/epages/DemoShop.admin/?'
		objectId           : '',               // objectId for info request
		titleAttribute     : 'NameOrAlias',    // attribute used to show name at tree
		title              : '',               // title of the rode node
		id                 : 'ClassStore',

		/**
		 * private properties
		 */
		_info              : undefined,            // epages.lang.Hash values including information about classes
		_JsonIo            : new epages.io.Json(), // epages.io.Json() JSON communication channel to server
		_serverAttributes  : '',                   // String, list of attributes for server

		constructor: function(keywordParameters){
			dojo.mixin(this,keywordParameters);
			this._info = $H();
			dojo.subscribe('uimessage/stopBusy',this, "_reloadStore");
		},

		_loadList: function(classId) {
			if(classId && !this._info.existsKey(classId)) {
				var jsonData = this._JsonIo.loadSync(this.url, {
					'ViewAction' : this.actionInfo,
					'ObjectID'   : this.objectId,
					'ClassID': classId
				});
				if (jsonData != null && !jsonData.error) {
					// save data at internal structure
					dojo.forEach(jsonData.data, function (el) {
						var info = {
							alias: el.Alias,
							title: el.NameOrAlias,
							objectId: el.ObjectID,
							contentChildClasses: el.ContentChildClasses,
							EffectedVisibilityAttributes: el.EffectedVisibilityAttributes,
							position: el.Position,
							feature: {
								currentValue: el.FeatureCurrentValue ? el.FeatureCurrentValue: undefined,
								maxValue: el.FeatureMaxValue ? el.FeatureMaxValue: undefined
							},
							description : el.Description
						};
						this._info.set(el.ObjectID, info);
					}, this);
				} else {
					return false;
				}
			}
			return true;
		},

		_fetch: function (objectId) {
			var childrenIds = this._info.get(objectId).contentChildClasses;
			var items = [];
			var i;
			dojo.forEach(childrenIds, function(el) {
				i = this._info.get(el);
				if (! i.isDeleted){ 
					items.push(i);
				}
			}, this);
			return items;
		},

		_fetchItems: function(keywordArgs, findCallback, errorCallback){
			if(this._loadList(keywordArgs.query.id) && typeof findCallback == 'function') {
				findCallback(this._fetch(keywordArgs.query.id), keywordArgs);
			} else {
				console.warn('error while _fetch %d', keywordArgs.query.id);
				//errorCallback(keywordArgs);
			}
		},

		fetchItemByIdentity: function(objectId){
			if (this._loadList(objectId)){
				return this._info.get(objectId);
			}
			return undefined;
		},

		hasAttribute: function(node, attributeName){
			return node[attributeName] !== undefined;
		},

		getLabel: function(item){
			return item.title;
		},

		getIdentity: function(item){
			return item.objectId;
		},

		getValue: function(item, attribute){
			console.warn('getValue %s', attribute);
		},

		getValues: function(item, attribute){
			if (attribute == 'children' && this._loadList(item.objectId)) {
				return this._fetch(item.objectId);
			}
			else{
				console.warn('getValues() unknown attribute', attribute);
			}
		},

		isItemLoaded: function(something){
			//console.debug('isItemLoaded');
			return this._info.existsKey(objectId);
		},

		_reloadStore:function(id){
				this._info = $H();
			}
});

dojo.extend(epages.cartridges.de_epages.content.ClassStore,dojo.data.util.simpleFetch);