/*
    Copyright (C) 2008  Ivo Toby, Internet Voor Ondernemers, http://www.i-v-o.nl / http://syntacticsugar.nl 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Crudder Version : 0.50b
 */

/**
 * This class keeps the state of the data in the current view. Takes care of paging of calls, precaching and stuff
 */

	var crudderDataFactory = Class.create({
		
		initialize : function(config, rpcPath, metaData){
			this.page = 0;
			this.config = config;
			this.rpcPath = rpcPath;
			// determine itemsPerPage:
			this.itemsPerPage = metaData.config.defaultItemsPerPage;
			this.args = $H({});
		},
		
		execute : function(callBack){
			var func = (function(req){this.handleReq(req, callBack)}).bind(this)
			var args = '';// create this by running of some kind of hash

			var RPCObj = new crudderRPC(this.config, this.rpcPath);
			RPCObj.attachWaiter(this.waiter, this);
			RPCObj.attachUnWaiter(this.unWaiter, this);
//			RPCObj.debug = true;
			RPCObj.createCall();
			RPCObj.setCallback(func);
			RPCObj.setMethod('getData');
			this.args.keys().each(
				(function(arg){
					RPCObj.addArgument(arg, this.args.get(arg));
				}).bind(this)
			)
			RPCObj.call();
		},
		
		exportData : function(args){
			// Prepare export, let client download in callBack
			var RPCObj = new crudderRPC(this.config, this.rpcPath);
			RPCObj.attachWaiter(this.waiter, this);
			RPCObj.attachUnWaiter(this.unWaiter, this);
			RPCObj.createCall();
			RPCObj.setCallback(this.downloadExport.bind(this));
			RPCObj.setMethod('export');
			// add arguments received from ehh.. arguments (callee)
			args.keys().each(
				(function(arg){
					RPCObj.addArgument(arg, args.get(arg));
				}).bind(this)
			)
			
			// add internal arguments:
			this.args.keys().each(
				(function(arg){
					RPCObj.addArgument(arg, this.args.get(arg));
				}).bind(this)
			)
			RPCObj.call();
		},
		
		downloadExport : function(req){
			// get file of export and download it:
			if (req.responseJSON.message){
				alert("Something went wrong or there are no records to export");
				return;
			}
			var RPCObj = new crudderRPC(this.config, this.rpcPath);
			RPCObj.debug = true;
			RPCObj.attachWaiter(this.waiter, this);
			RPCObj.attachUnWaiter(this.unWaiter, this);
			RPCObj.debug = true;
			RPCObj.createIframedCall(function(){});
			RPCObj.setMethod('getDownload');
			RPCObj.addArgument("file", req.responseJSON.file);
			RPCObj.callIframe()
		},
		
		handleReq : function(req, callBack){
			// set internal values for paging and stuff
			callBack(req);
		},
		
		set : function(key, value){
			this.args.set(key, value);
		},
		
		waiter : function(){
			$$(".crudderWaiter").each(
				function(elem){elem.show()}
			)
		},

		unWaiter : function(){
			$$(".crudderWaiter").each(
				function(elem){elem.hide()}
			)
		}
		
		
		/** 
		 * functions used to change the state:
		 * - pages
		 * - sorting/ordering
		 * - filtering
		 */
		
		
		
		
	})