/**
 *
 * Copyright (C) 2021 Akorbi Digital RMP
 *
 * This file is part of RunMyProcess SDK-JS.
 *
 * RunMyProcess SDK-JS is free software: you can redistribute it and/or modify
 * it under the terms of the Apache License Version 2.0 (the "License");
 *
 *   You may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
 /**
	Create a new instance of UserLaneList
  	@constructor
	@property {Array} lanes - Array of lanes.
	@property {Array} pools - Array of pools.
	@see User
	@see Resource
 */
function UserLaneList(){
	this.lanes = [];
	this.pools = [];
};
/**
	 @borrows
	 Inherits Resource
*/
UserLaneList.prototype = new  Resource();
/**
	Overrides Resource's loadPreset method.
	@method
	@see Resource#loadPreset
*/
UserLaneList.prototype.loadPreset = function () {
	/**
		Overrides Resource's generateUrl method to return the request url
		@method
		@see Resource#generateUrl
	*/
	this.generateUrl = function () {
		return this.selfUrl;
	};
};
/**
	Overrides Resource's loadSet method to set local variables after request.
	@method
	@param {json} rObject - JSON representation of the loaded data.
	@see Resource#loadSet
*/
UserLaneList.prototype.loadSet = function (rObject) {
	var father = this;
	var ent=this.getArray(rObject, "entry");
	var pList = new LanePoolList();
	var plLinks = [];
	father.lanes = [];
	for (var i = 0; i < ent.length; i++) {
		//LANES
		var lane = new Lane();
		lane.object = ent[i];
		lane.id = ent[i].id;
		lane.categories = lane.getArray(ent[i], "category");
		lane.links = lane.getArray(ent[i], "link");
		lane.selfUrl = ent[i].content.src;
		lane.rights = lane.object.rights;
		lane.author.name = ent[i].author.name;
		lane.author.selfUrl = ent[i].author.uri;
		var ppool = new Pool();
		var poolLinkObject = father.linkObjectSearch('pool',ent[i].link);
		ppool.selfUrl = poolLinkObject.href||'';
		ppool.title = poolLinkObject.title||'';
		lane.pool = ppool;
		father.lanes.push(lane);
		//END LANES
		//POOLS
		var llinks=father.getArray(ent[i], "link");
		plLinks.push(father.linkSearch('pool',llinks));
		//END POOLS
	}
	var unqLinks = plLinks.filter(function(elem, pos) {
		return plLinks.indexOf(elem) === pos;
	});
	for (var i = 0; i < unqLinks.length; i++) {
		var pl=new Pool();
		pl.selfUrl = unqLinks[i];
		pList.pools.push(pl);
	};
	this.pools = pList.pools;
};
/**
   Load pools in a serialized manner
	@method
	@param {object} options - options to be used during the call<br/>
	@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
	@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
	@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
*/
UserLaneList.prototype.loadPoolsSerialized = function (options) {
	var father = this;
	function serialLoad(i){
		try{
			var lOptions = {};
			lOptions.onSuccess = function(){
				if (i < ps.length-1){
					serialLoad(i+1);
				}else{
					options.onSuccess();
				}
			};
			lOptions.onFailure = function(e){
				options.eObject=e;
				father.errorManager(options);
			};
			lOptions.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
			ps[i].load(lOptions);
		}catch (e) {
			options.eObject=e;
			father.errorManager(options);
		};
	};
	var ps = father.pools;
	serialLoad(0);
};
