﻿/*
Required modules:
	eKonx.Base

Required initialization:
	eKonx.InitCache()
creates cache with common objects
*/
Ext.ns("eKonx.Cache");

eKonx.InitCache = function ()
{
	eKonx.Cache = Ext.apply(eKonx.Cache, eKonx.Json("Cache/GetCache", { method: "POST" }));
	eKonx.CurrentUser = eKonx.Cache.User;
	for (var i = 0; i < eKonx.CurrentUser.Permissions.length; i++)
		eval("eKonx.CurrentUser.Permissions." + eKonx.CurrentUser.Permissions[i].SystemName + "=true;");
};

eKonx.Cache.GetJsonStore = function(data, root)
{
	data = data || [];
	return new Ext.data.JsonStore(
		{
			autoDestroy: false,
			autoLoad: true,
			root: root,
			idProperty: 'ID',
			proxy: new Ext.data.MemoryProxy(data),
			fields: ['ID', 'Name']
		});
}
eKonx.Cache.GetStore = function(name, addEmptyField)
{
	var cacheDescription = eKonx.Cache.GetCacheByName(name);
	return eKonx.Cache.GetJsonStore(cacheDescription.Cache, cacheDescription.RootName);
};

/// <summary>
///  name can be path: "RieltItems.AppartmentStates"
/// </summary>
/// <returns>
/// { Cache: cache object, RootName: name of cache array }
/// for example,
/// for request GetCacheByName("Countries")
///		returns { Cache: eKonx.Cache, RootName: "Countries", Data: eKonx.Cache["Countries"]}
/// for request GetCacheByName("RieltItems.AppartmentStates")
///		returns { Cache: eKonx.Cache.RieltItems, RootName: "AppartmentStates", Data: eKonx.Cache.RieltItems["AppartmentStates"] }
///
/// to get real array use:
/// var scope = eKonx.Cache.GetCacheByName(cacheName);
/// scope = scope.Cache[scope.RootName];
/// </returns>
eKonx.Cache.GetCacheByName = function(name, notAddEmptyFirst)
{
	var result = eKonx.Cache.GetEmpty();
	var sourceCache = eKonx.Cache.FindCacheByName(name);
	if (sourceCache.cache[sourceCache.rootName])
	{
		result =
		{
			Cache: sourceCache.cache,
			RootName: sourceCache.rootName,
			Data: Array.deepClone(sourceCache.cache[sourceCache.rootName])
		};
	}

	//if (result.Data[0].Name && result.Data[0].Name!=" ")
	if (!notAddEmptyFirst)
	{
		result.Data.unshift({ Name: " ", ID: 0 });
	}
	return result;
};

eKonx.Cache.FindCacheByName = function(name)
{
	var result =
	{
		cache: [],
		rootName: ""
	};
	var names = name.split(".");
	result.rootName = names[0];
	result.cache = eKonx.Cache;
	if (names.length > 1)
	{
		for (var i = 0; i < names.length - 1; i++)
		{
			result.cache = result.cache[names[i]];
		}
		result.rootName = names[names.length - 1];
	}
	return result;
}

eKonx.Cache.GetEmpty = function()
{
	var empty =
	{
		Cache: eKonx.Cache,
		RootName: null,
		Data: []
	};
	return empty;
}

eKonx.Cache.GetComboBox = function(cacheName, defaultText, config)
{
	return new Ext.form.ComboBox(
		Ext.apply(
		{
			xtype: 'combo',
			typeAhead: true,
			triggerAction: 'all',
			lazyRender: true,
			mode: 'local',
			emptyText: defaultText,
			displayField: "Name",
			valueField: "ID",
			store: eKonx.Cache.GetStore(cacheName)
		},
		config));
};


eKonx.Cache.GetColumn = function(cacheName, headerText, dIndex, defaultText, colWidth, comboConfig, idPrefix)
{
	if (!Ext.isDefined(comboConfig) || (!comboConfig)) comboConfig = {};
	if (!Ext.isDefined(idPrefix) || (!idPrefix)) idPrefix = "colId";
	if (comboConfig.idPrefix) idPrefix = comboConfig.idPrefix;
	return new Ext.grid.Column(
	{
		header: headerText,
		dataIndex: dIndex,
		width: (colWidth ? colWidth : 150),
		sortable: ((comboConfig && comboConfig.sortable) || false),
		hiddenName: idPrefix + cacheName.replace(".", "_"),
		editable: (comboConfig.editable === false) ? comboConfig.editable : true,
		lazyRender: true,
		editor: new Ext.form.ComboBox(eKonx.Cache.GetComboBox(cacheName, defaultText, comboConfig)),
		renderer: function(value)
		{
			var record = this.editor.findRecord("ID", value);
			return record ? record.get(this.editor.displayField) : "<span class='x-form-empty-field'>" + this.editor.emptyText + "</span>";
		}
	});
};


eKonx.Cache.GetByID = function(cacheName, id)
{
	var scope = eKonx.Cache.GetCacheByName(cacheName).Data;
	if (!scope) return null;
	for (var i = 0; i < scope.length; i++)
		if (scope[i].ID == id) return scope[i];
	return null;
};

eKonx.Cache.FindByID = function(cacheName, id)
{
	var sourceCache = eKonx.Cache.FindCacheByName(cacheName);
	var result = null;
	if (sourceCache.cache[sourceCache.rootName])
	{
		Ext.each(sourceCache.cache[sourceCache.rootName], function(item)
		{
			if (item.ID == id)
			{
				result = item;
				return false;
			}
		});
	}
	return result;
};




