/*
 * Cellblock general-purpose management console, a component of
 * the Gloto Platform.  See www.gloto.com for more information,
 * terms, and licensing.
 *
 * Copyright (C) 2005-2010, Gloto Corp.
 * http://www.cellblock.com/terms.htm
 */

function API() {
	var api = this;
	var gloto = new Gloto.Api(config.getApiKey());

	this.call=function(method,params,callback){gloto.call(method,params,callback);};

	this.ping = function(number, callback) {
		var params = {
				num:number
		};

		gloto.call("Ping", params, callback);
	};

	this.getCellblockInfo = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("CellblockInfo", params, callback);
	};

	this.setFrameCaption = function(id, callback) {

		var params = {
				id :id,
				caption :util.getElement("caption").value
		};

		gloto.call("SetFrameCaption", params, callback);
	};

	this.rotateFrameLeft = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("RotateFrameLeft", params, callback);
	};

	this.rotateFrameRight = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("RotateFrameRight", params, callback);
	};


	/* NOTE: THIS METHOD HAS A SCREWY IMPLEMENTATION B/C OF RAPID FIRE ASYNCH API CALLS.  REVISIT IMPLEMENTATION */
	this.setFrameAttributeValues = function(form) {
		var id = form["id"].value;
		for ( var i = 0; i < form.length; i++) {
			if (form[i].name != "" && form[i].name != "id") {
				var key = form[i].name;
				var value = form[i].value;
				var callback = function(data) {
					if (!util.valid(data)) {
						util.error(data);
					}
				};
				api.setFrameAttributeValue(id, key, value, callback);
			}
		}
		util.console.println("Saved: Attribute Values");
	};

	this.setFrameAttributeValue = function(id, name, value, callback) {
		var params = {
				id :id,
				attributeName :name,
				attributeValue :value
		};

		gloto.call("SetFrameAttributeValue", params, callback);
	};

	this.attachFrameAttribute = function(id, name, callback) {
		var params = {
				id :id,
				attributeName :name,
				attributeValue :""
		};

		gloto.call("SetFrameAttributeValue", params, callback);
	};

	this.approveFrame = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("ApprovePendingFrame", params, callback);
	};

	this.deleteFrame = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("DeleteFrame", params, callback);
	};

	this.getSession = function(callback) {
		var params = {};
		gloto.call("GetSessionInfo", params, callback);
	};

	this.createRealmSkin = function(name, attributes, callback) {
		var params = {
				name :name,
				realmId :config.getRealm(),
				attributeList :attributes
		};

		gloto.call("CreateDisplayModel", params, callback);
	};

	this.deleteRealmSkin = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("DeleteDisplayModel", params, callback);
	};

	this.getRealmAttributeValue = function(attribute, callback) {
		var params = {
				realmId :config.getRealm(),
				attributeName :attribute
		};

		gloto.call("GetRealmAttributeValue", params, callback);
	};

	this.getRealmAttributeValues = function(callback) {
		var params = {
				realmId :config.getRealm()
		};

		gloto.call("GetRealmAttributeValues", params, callback);
	};

	this.setRealmAttribute = function(attribute, value, callback) {
		var params = {
				realmId :config.getRealm(),
				attributeName :attribute,
				attributeValue :value
		};

		gloto.call("SetRealmAttributeValue", params, callback);
	};

	this.getRealmSkins = function(callback) {
		var params = {
				realmId :config.getRealm(),
				offset :0,
				count :20
		};

		gloto.call("GetDisplayModels", params, callback);
	};

	this.setRealmSkin = function(id, attributes, action, callback) {
		var params = {
				id :id,
				attributes :attributes,
				action :action
		};

		gloto.call("SetDisplayModel", params, callback);
	};

	this.createTemplate = function(name, mobile, callback) {
		var params = {
				realmId :config.getRealm(),
				name :name,
				mobile :mobile
		};

		gloto.call("CreateDisplayModel", params, callback);
	};

	this.deleteTemplate = api.deleteRealmSkin;

	this.getTemplateTags = function(callback) {
		var params = {
				tagName :"*"
		};

		gloto.call("GetDocumentation", params, callback);
	};

	this.getTemplateCss = function(callback) {
		var params = {
				tagStyle :"*"
		};

		gloto.call("GetDocumentation", params, callback);
	};

	this.createTemplateProperty = function(id, attributes, callback) {
		var params = {
				realmId :config.getRealm(),
				id :id,
				attributes :attributes,
				action :"update"
		};

		gloto.call("SetDisplayModel", params, callback);
	};

	this.deleteTemplateProperty = function(id, attributes, callback) {
		var params = {
				realmId :config.getRealm(),
				id :id,
				attributes :attributes,
				action :"delete"
		};

		gloto.call("SetDisplayModel", params, callback);
	};

	this.getTemplate = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("GetDisplayModel", params, callback);
	};

	this.getMyTemplates = function(callback) {
		var params = {
				realmId :config.getRealm(),
				mobile :true,
				offset :0,
				count :10
		};

		gloto.call("GetDisplayModels", params, callback);
	};

	this.getDefaultTemplates = function(callback) {
		var params = {
				realmId :null,
				mobile :true,
				offset :0,
				count :10
		};

		gloto.call("GetDisplayModels", params, callback);
	};

	this.getTemplateEngines = function(callback) {
		var params = {
				realmId :config.getRealm(),
				engineName :"*"
		};

		gloto.call("GetDocumentation", params, callback);
	};


	this.approveCellblockComment = function(id, index, callback) {
		var params = {
				id :id,
				index :index
		};
		gloto.call("PostComment", params, callback);
	};

	this.deleteCellblockComment = function(id, index, callback) {
		var params = {
				id :id,
				index :index
		};

		gloto.call("DeleteComment", params, callback);
	};

	this.getCellblockComments = function(offset, count, filter, callback) {
		var params = {
				id :config.getId(),
				offset :offset,
				count :count,
				filter :filter
		};

		gloto.call("GetComments", params, callback);
	};

	this.approveFrameComment = function(frameId, index, callback) {
		var params = {
				frameId :frameId || config.get("frame"),
				index :index
		};
		gloto.call("PostComment", params, callback);
	};

	this.deleteFrameComment = function(frameId, index, callback) {
		var params = {
				frameId :frameId || config.get("frame"),
				index :index
		};

		gloto.call("DeleteComment", params, callback);
	};

	this.getFrameComments = function(id, offset, count, filter, callback) {
		var frameId = id || config.get("frame");
		var params = {
				frameId :frameId,
				offset :offset || 0,
				count :count || 10,
				filter :filter
		};

		gloto.call("GetComments", params, callback);
	};


	this.postComment = function(comment, target, callback) {
		var params = {
				comment :comment,
				source :"web"
		};

		if (target.cellblock) {
			params.id = target.cellblock;
		}
		if (target.realm) {
			params.realmName = target.realm;
		}
		if (target.user) {
			params.userName = target.user;
		}
		if (target.frame) {
			params.frameId = target.frame;
		}

		gloto.call("PostComment", params, callback);
	};

	this.sendToPhone = function(number, carrier, callback) {
		var params = {
				id :config.getId(),
				phone :number,
				carrier :carrier,
				serviceKey: "BGC"
		};

		gloto.call("SendCellblockToPhone", params, callback);
	};

	this.sendFrameToPhone = function( frameId, number, callback )
	{
		var params = {
				frameId: frameId,
				phone: number,
				format: "text",
				serviceKey: "",
				pageName: "watch"
		};

		gloto.call( "SendCellblockToPhone", params, callback );
	};

	this.login = function(username, password, callback) {
		var params = {
				screenName :username,
				password :password,
				realmId :config.getRealm(),
				clear :true
		};

		gloto.call("Login", params, callback);
	};

	this.realmLogin = function(password, callback) {
		var params = {
				realmId :config.getRealm(),
				realmPassword :password,
				clear :true
		};

		gloto.call("RealmLogin", params, callback);
	};

	this.logout = function(callback) {
		var params = {};

		gloto.call("Logout", params, callback);
	};

	this.getFrameInfo = function(callback) {
		var params = {
				frameId :config.get("frame")
		};
		gloto.call("GetFrameInfo", params, callback);
	};

	this.getPreviousFrame = function(lastIndex, callback) {
		var params = {
				id :config.getId(),
				direction :-1,
				maxWIndex :-1,
				lastIndex :lastIndex
		};

		gloto.call("More", params, callback);
	};

	this.getNextFrame = function(lastIndex, callback) {
		var params = {
				id :config.getId(),
				direction :1,
				maxWIndex :-1,
				lastIndex :lastIndex
		};

		gloto.call("More", params, callback);
	};

	this.getFrameRating = function(callback) {
		var params = {
				frameId :config.get("frame")
		};
		gloto.call("GetRating", params, callback);
	};

	this.getCellblockRating = function(callback) {
		var params = {
				id :config.getId()
		};

		gloto.call("GetRating", params, callback);
	};

	this.setFrameRating = function(rating, callback) {
		var params = {
				rating :rating,
				frameId :config.get("frame")
		};

		gloto.call("SetRating", params, callback);
	};

	this.setCellblockRating = function(rating, callback) {
		var params = {
				rating :rating,
				id :config.getId()
		};

		gloto.call("SetRating", params, callback);
	};

	this.numRealmCellblocks = function(callback) {
		var params = {
				realmId :config.getRealm()
		};

		gloto.call("NumRealmCellblocks", params, callback);
	};

	this.getRealmInfo = function(callback) {
		var params = {
				realmId :config.getRealm()
		};

		gloto.call("GetRealmInfo", params, callback);
	};

	this.numRealmUsers = function(callback) {
		var params = {
				realmId :config.getRealm()
		};

		gloto.call("NumRealmUsers", params, callback);
	};

	this.getRealmCellblocks = function(offset, count, callback) {
		var params = {
				offset :offset,
				count :count,
				realmId :config.getRealm(),
				extended : "basic,counts"
		};

		gloto.call("GetRealmCellblocks", params, callback);
	};

	this.getRealmUsers = function(offset, count, minPriv, maxPriv, callback) {
		var params = {
				offset :offset,
				count :count,
				minPrivilege :minPriv,
				maxPrivilege :maxPriv,
				realmId :config.getRealm()
		};

		gloto.call("GetUsersInRealm", params, callback);
	};

	this.getUserAttributeValue = function(user, attribute, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :user,
				attributeName :attribute
		};

		gloto.call("GetUserAttributeValue", params, callback);
	};

	this.getUserAttributeValues = function(user, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :user
		};

		gloto.call("GetUserAttributeValues", params, callback);
	};

	this.setCellblockAttributeValue = function(id, name, value, callback) {
		var params = {
				id :id,
				attributeName :name,
				attributeValue :value
		};

		gloto.call("SetCellblockAttributeValue", params, callback);
	};

	this.deleteCellblockAttributeValue = function(attribute, callback) {
		var params = {
				realmId: config.getRealm(),
				attribute: attribute,
				target: "CELLBLOCK"
		};

		gloto.call("DeleteAttributeDefinition", params, callback);
	}; 

	this.getCellblockAttributeValues = function(id, callback) {
		var params = {
				realmId :config.getRealm(),
				id :id
		};

		gloto.call("GetCellblockAttributeValues", params, callback);
	};

	this.getAttributeDefinitions = function(offset, count, callback) {
		var params = {
				realmId :config.getRealm(),
				offset: offset,
				count: count
		};

		gloto.call("GetAttributeDefinitions", params, callback);
	};

	this.createFrameAttributeDefinition = function(attribute, type, callback) {
		var params = {
				realmId: config.getRealm(),
				attributeName: attribute,
				attributeType: ( type != null ) ? type : "STRING",
						isPublic: true
		};

		gloto.call("CreateFrameAttributeDefinition", params, callback);
	}; 	  

	this.deleteFrameAttributeDefinition = function(attribute, callback) {
		var params = {
			realmId: config.getRealm(),
			attribute: attribute,
			target: "FRAME"
		};

		gloto.call("DeleteAttributeDefinition", params, callback);
	}; 	  	
	
	this.setCellblockReceipt = function(id, enabled, subject, message, callback) {
		var params = {
				id :id,
				receiptEnabled :enabled,
				receiptSubject :subject,
				receiptBody :message
		};

		gloto.call("SetCellblockInfo", params, callback);
	};

	this.setCellblockTitleAndEmail = function( id, title, email, callback) {
		var params = {
				id: id,
				name: title,
				email: email
		};

		gloto.call( "SetCellblockInfo", params, callback );

	};

	this.setCellblockInfo = function(id, preview, callback) {
		var params = {
				id :id,
				preview :preview,
				receiptEnabled :config.get("receiptEnabled"),
				receiptSubject :config.get("receiptSubject"),
				receiptBody :config.get("receiptMessage")
		};

		gloto.call("SetCellblockInfo", params, callback);
	};

	this.setCellblockInfoExtended = function(id, from, callback) {
		var params = {
				id :id,
				receiptFromAddress :from
		};

		gloto.call("SetCellblockInfoExtended", params, callback);
	};

	this.setUserInfo = function(user, email, password, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :user,
				email :email,
				password :password
		};

		gloto.call("SetUserInfo", params, callback);
	};

	this.setUserAttributeValue = function(user, attribute, value, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :user,
				attributeName :attribute,
				attributeValue :value
		};

		gloto.call("SetUserAttributeValue", params, callback);
	};

	this.setPreviewStatus = function(id, preview, callback) {
		var params = {
				id :id,
				preview :preview
		};

		gloto.call("SetCellblockInfo", params, callback);
	};

	this.setPreviewCommentStatus = function(id, preview, callback) {
		var params = {
				id :id,
				previewComments :preview
		};

		gloto.call("SetCellblockInfo", params, callback);
	};

	this.createCellblock = function(title, email, callback) {
		var params = {
				title :title,
				realmId :config.getRealm(),
				email :email,
				previewComments :true
		};

		gloto.call("CreateRealmCellblock", params, callback);
	};

	this.createUser = function(name, email, password, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :name,
				email :email,
				password :password
		};
		gloto.call("CreateUser", params, callback);
	};

	this.createRealmUser = function(name, email, password, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :name,
				email :email,
				privilege :10,
				password :password
		};

		gloto.call("CreateUser", params, callback);
	};

	this.deleteCellblock = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("DeleteCellblock", params, callback);
	};

	this.deleteUser = function(user, callback) {
		var params = {
				realmId :config.getRealm(),
				screenName :user
		};

		gloto.call("DeleteUser", params, callback);
	};

	this.numFrames = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("NumFrames", params, callback);
	};

	this.numPendingFrames = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("NumPendingFrames", params, callback);
	};

	this.numAcceptedFrames = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("NumAcceptedFrames", params, callback);
	};

	this.frameInfo = function(id, offset, count, callback) {
		var params = {
				id :id,
				offset :offset,
				count :count
		};

		gloto.call("FrameInfoForCellblock", params, callback);
	};

	this.getFrameInfoWithComments = function(id, offset, count, sortBy, callback) {
		var params = {
				id :id,
				offset :offset,
				count :count,
				extended:true,
				direction:-1
		};

		if( sortBy != null ){
			params.sortBy = sortBy;
		}else{
			params.sortBy = "order";
		}

		gloto.call("FrameInfoForCellblock", params, callback);
	};

	this.pendingFrameInfo = function(id, offset, count, callback) {
		var params = {
				id :id,
				offset :offset,
				count :count
		};

		gloto.call("PendingFrameInfoForCellblock", params, callback);
	};

	this.acceptedFrameInfo = function(id, offset, count, callback) {
		var params = {
				id :id,
				offset :offset,
				count :count
		};

		gloto.call("AcceptedFrameInfoForCellblock", params, callback);
	};

	this.getAcceptedFrameInfoWithComments = function(id, offset, count, sortBy, callback) {
		var params = {
				id :id,
				offset :offset,
				count :count,
				extended:true,
				direction:-1
		};

		if( sortBy != null ){
			params.sortBy = sortBy;
		}else{
			params.sortBy = "order";
		}

		gloto.call("FrameInfoForCellblock", params, callback);
	};

	this.getFrameAttributes = function(id, callback) {
		var params = {
				id :id
		};

		gloto.call("GetFrameAttributeValues", params, callback);
	};

	this.getPendingFrameComments = function(id, callback) {
	};

	this.setAnnotation = function(props, save, callback) {
		var params = {
				'frameId' :config.get("frame")
		};
		if (save != null) {
			params.save = save;
		}

		for (key in props) {
			params[key] = props[key];
		}

		gloto.call("SetAnnotation", params, callback);
	};

	this.getLanguageFilterInfo = function(callback) {
		var params = {
				realmId :config.getRealm(),
				offset :0,
				count :100
		};

		gloto.call("GetLanguageFilterInfo", params, callback);
	};

	this.setLanguageFilterInfo = function( id, word, replacement, suffix, callback ) {
		var params = {
				realmId:config.getRealm()
		};

		if( word != null ){
			params.word = word;
		}

		if( replacement != null){
			params.replacement = replacement;
		}

		if( suffix != null ){
			params.suffixList = suffix;
		}

		if( id != null ){
			params.dictId = id;
		}

		gloto.call("SetLanguageFilterInfo", params, callback);
	};

	this.clearLanguageFilter = function( callback ) {
		var params = {
				realmId:config.getRealm(),
				clear: true
		};

		gloto.call("SetLanguageFilterInfo", params, callback);
	};

	this.toggleLanguageFilter = function( enabled, callback ) {
		var params = {
				realmId: config.getRealm(),
				enabled: enabled
		};

		gloto.call("SetLanguageFilterInfo", params, callback);
	};

	this.getEventInfo = function(startTime, callback) {
		var params = {
				realmId :config.getRealm()
		};
		if (startTime)
			params.startTime = startTime;

		gloto.call("GetEventInfo", params, callback);
	};

	this.moveApprovedFrame = function(id, position, callback) {
		var params = {
				id :id,
				newPosition :position
		};

		gloto.call("MoveApprovedFrame", params, callback);
	};

	this.getAnnotation = function( id, callback )
	{
		var params = {
				frameId: id
		};

		gloto.call( "GetAnnotation", params, callback );
	};

	this.parseSpreadsheet = function( handle, callback )
	{
		var params = {
				handle: handle
		};

		gloto.call( "ParseSpreadsheet", params, callback );
	};

	this.getTransactionResult = function( id, callback )
	{
		var params = {
				id: id
		};

		gloto.call( "GetTransactionResult", params, callback );
	};
}

var api = new API();
