pcwa.core.scope('pcwa.ext.model');

pcwa.ext.model.ModelComponent = function ModelComponent(){
	this.storeId = '';
	this.title = 'record(s)';
	this.store = null;
	this.parentId = '';
	this.id = '';
	this.parentURI = '';
	this.resource = '';
	this.copyId = 1;
};
pcwa.ext.model.ModelComponent.prototype = {
	/**
	 * createStore: Override this method to supply a store for this component.
	 */
	createStore : function createStore(storeId){ return this.store; },
	/**
	 * getRecordDefaults: Override this method to supply default values for any record
	 * created by this component when defaults are not explicity specified.
	 */
	getRecordDefaults : function getRecordDefaults(){ return {}; },
	/**
	 * load: Override this method to supply a specialized load operation.
	 */
	load : function load(config){
		config = config || {};
		this.setUrl(this.getUrl());
		this.getStore().load(config);
	},
	
	isLoading: function isLoading() {
		return this.store.proxy.conn.isLoading();
	},
	
	/**
	 * getUrl: Override this method to supply a customized url.
	 */
	getUrl : function getUrl(){
		var url = '';
		if ( this.parentURI && this.resource ){
			if ( this.parentURI.length > 0 && this.resource.length > 0 ){
				url =	 this.parentURI + 
						(this.parentId ? this.parentId + '/' : '') +
						 this.resource +
						(this.id ? '/' + this.id : '');
			}
		}
		return url;
	},
	setUrl : function setUrl(url){
		this.getStore().proxy.conn.url = url;
	},
	
	clear :	function clear(){
		this.parentId = '';
		this.id = '';
		this.getStore().removeAll();
		return this;
	},
	getStore : function getStore(storeId){
		if ( this.store == null ){
			this.store = this.createStore(storeId || this.storeId);
		}
		return this.store;
	},
	getStoreCopy : function getStoreCopy(){
		var copy = null;
		if ( this.store ){
			copy = this.createStore(this.store.storeId + '-' + this.copyId++);
		}
		return copy;
	},
    getNewRecord: function getNewRecord(data){
    	if ( this.getStore().record ){
    		return new this.store.record(data || this.getRecordDefaults());
    	}else{
    		return null;
    	}
    },
    getId: function getId(){ return this.id; },
    setId: function setId(id){ 
    	this.id = id;
    	this.parentId = '';
    	return this;
    },
    getParentId: function getId(){ return this.parentId; },
    setParentId: function setParentId(id){
    	this.id = '';
    	this.parentId = id;
    	return this;
    },
    setStoreId: function (sId){
    	if ( !this.store ){ this.storeId = sId;	}
    	return this;
    },
	updateRecord : function updateRecord(record, commit){
		var rec = this.getStore().getById(record.id);
		if ( rec ){
			rec.beginEdit();
			for ( var k in record.modified ){
				rec.set(k, record.get(k));
			}
			rec.endEdit();
			if ( !(commit === false) ){
				this.getStore().commitChanges();
			}
		}
	},
	performUpdate: function performUpdate(record, animateEl, options){
		options = options || {};
		dataUpdateManager.updateRecord	({
											record:			record,
											reset:			false,
											callback:		this.updateResponse,
											params:			{
																record:		record,
																animateEl:	animateEl,
																options:	options
															},
											scope:			this
										});
	},
	deleteRecord : function deleteRecord(record, animateEl, options){
		options = options || {};
    	dataUpdateManager.deleteRecord	({
											record:			record,
											reset:			false,
											callback:		this.deleteResponse,
											params:			{
																record:		record,
																animateEl:	animateEl,
																options: options
															},
											scope:			this
    									});
	},
    deleteResponse : function deleteResponse(success, resp, params, errors){
		if ( success ){
			this.getStore().remove(params.record);
			this.getStore().commitChanges();
		} else {
			dataUpdateManager.showErrors(errors, params.animateEl);
		}
		if ( params.options.callback && typeof params.options.callback == 'function' ){
			params.options.callback.call(params.options.scope || window, success, params.options);
		}
	},
    updateResponse : function updateResponse(success, resp, params, errors){
		if ( success ){
			this.getStore().commitChanges();
		} else {
			dataUpdateManager.showErrors(errors, params.animateEl);
		}
		if ( params.options.callback && typeof params.options.callback == 'function' ){
			params.options.callback.call(params.options.scope || window, success, resp, params.options);
		}
	},
	onRequestException: function onRequestException(conn, xhr, o){
		if ( xhr.status != '-1' ){
			dataUpdateManager.showErrors(dataUpdateManager.buildErrors(xhr, this.store.storeId));
		}
	}
};
