/* Copyright 2009 Palm, Inc.  All rights reserved. */
/*global Mojo, Class, $L */
/*jslint rhino:true, undef:true */
var ExcelAssistant = Class.create({

	xlsCmdMenu: [
		{},
		{ items: [
			{ icon:'back', command:'prev-cmd' },
			{ template: 'document/command_bar', command: 'edit-cmd'},
			{ icon:'forward', command:'next-cmd' }
		]},
		{}
	],

	initialize: function(docAssistant) {
		this.docAssistant = docAssistant;
		this.controller = docAssistant.controller;
		this.serviceList = [];
		this.gotSheetList = false;
		this.onPopupMenuChoose = this.onPopupMenuChoose.bind(this);
	},

	docLoaded: function() {
		if (!this.gotSheetList) {
			this.showXLSOutline(false);
		}
	},

    orientationChanged: function(orientation) {
        Mojo.Log.info("Excel: orientation changed to: " + orientation);
    },

	getCommandMenu: function() {
		return this.xlsCmdMenu;
	},

	showXLSOutline: function(do_show) {
		//Mojo.Log.info("showXLSOutline");

		if (!this.gotSheetList) {
			this.controller.serviceRequest( 'palm://com.palm.office/excel/getSheetList',
				{
					parameters: { docid: this.docAssistant.document_id },

					onSuccess: function(payload) {
						if (payload.errorCode !== undefined || payload.list === undefined) {
							Mojo.Log.error("getSheetList failed to get note: " + Object.toJSON( payload ) );
							return;
						}

						this.serviceList = payload.list;
						this.docAssistant.setPageCount(this.serviceList.length);

						this.gotSheetList = true;

						if (do_show) {
							this.resetCheckmark();
							this.controller.stageController.pushScene('outline', this.docAssistant, this.serviceList);
						}
						else {
							this.showSheetCnt();
						}
					}.bind(this),

					onFailure: function(payload) {
						Mojo.Log.error("getSheetList failed: " + Object.toJSON( payload ) );
					}
				});
		}
		else {
			if (do_show) {
				this.resetCheckmark();
				this.controller.stageController.pushScene('outline', this.docAssistant, this.serviceList);
			}
		}
	},


	gotoPage: function(sheet_num)
	{		
		// Only jump to sheet if the sheet_num does not match the current page
		if (sheet_num != this.docAssistant.getCurrentPage())
		{
			this.controller.serviceRequest( 'palm://com.palm.office/excel/jumpToSheet',
				{
					parameters: { docid: this.docAssistant.document_id, sheet: sheet_num },

					onSuccess: function(payload) 
					{
						if (payload.errorCode !== undefined || payload.cursheet === undefined) {
							Mojo.Log.error("gotoPage failed to get note: " + Object.toJSON( payload ) );
							return;
						}

						this.docAssistant.setCurrentPage(payload.cursheet);
						this.showSheetCnt();
					}.bind(this),

					onFailure: function(payload) 
					{
						Mojo.Log.error("gotoPage failed: " + Object.toJSON( payload ) );
					}
				});
		}
	},


	getComment: function()
	{
		this.controller.serviceRequest( 'palm://com.palm.office/excel/getLastComment',
			{
				parameters: { docid: this.docAssistant.document_id },

				onSuccess: function(payload) {
					if (payload.errorCode !== undefined || payload.comment === undefined || payload.ycoord === undefined) {
						Mojo.Log.error("gotoPage failed to getLastComment: " + Object.toJSON( payload ) );
						return;
					}

					// Position the comment correctly
					//Mojo.Log.info("payload.ycoord = '%s'", payload.ycoord);
					var xlsCommentBottom = 0;

					if (parseInt(payload.ycoord, 10) > 210)
					{
						if (this.docAssistant.isHidden) {
							xlsCommentBottom = 433 - parseInt(payload.ycoord, 10);
						}
						else {
							xlsCommentBottom = 383 - parseInt(payload.ycoord, 10);
						}
					}
					else
					{
						if (this.docAssistant.isHidden) {
							xlsCommentBottom = 262 - parseInt(payload.ycoord, 10);
						}
						else {
							xlsCommentBottom = 212 - parseInt(payload.ycoord, 10);
						}
					}

					this.docAssistant.commentsDialog(payload.comment, xlsCommentBottom);
				}.bind(this),

				onFailure: function(payload) {
					Mojo.Log.error("getLastComment failed: " + Object.toJSON( payload ) );
				}
			});
	},

	showPopupMenu: function() {

		this.controller.popupSubmenu({
			onChoose: this.onPopupMenuChoose,
			items: [{label: $L('Sort ascending'), command: 'excel-sortaz-cmd', righticon:'sortaz'},
					{label: $L('Sort descending'), command: 'excel-sortza-cmd', righticon:'sortza'},
					{label: $L('Hide selected'), command: 'excel-hide-cmd'}]
		});
	},

	showPopupMultiMenu: function() {

		this.controller.popupSubmenu({
			onChoose: this.onPopupMenuChoose,
			items: [{label: $L('Hide selected'), command: 'excel-hide-cmd'},
					{label: $L('Show hidden'), command: 'excel-show-cmd'}]
		});
	},

	onPopupMenuChoose: function(value) {

		var lHandle;

		switch (value)
		{
			case 'excel-copy-cmd':
				//Mojo.Log.info("excel.js: copy command");
				this.controller.serviceRequest( 'palm://com.palm.office/excel/copySelection',
					{
						parameters: {
									  docid: this.docAssistant.document_id,
									  column: "/var/tmp/webkit-clipboard"
									},

						onSuccess: function(payload) {
						},

						onFailure: function(payload) {
							Mojo.Log.error("copySelection failed: " + Object.toJSON( payload ) );
						}
					});

				break;

			case 'excel-sortaz-cmd':
				//Mojo.Log.info("excel.js: sortaz command");

				this.controller.serviceRequest( 'palm://com.palm.office/excel/sortAscending',
						{
							parameters: { docid: this.docAssistant.document_id, column: 1 },

							onSuccess: function(payload) {
								if (payload.errorCode !== undefined || payload.column === undefined) {
									Mojo.Log.error("sortAscending failed to get note: " + Object.toJSON( payload ) );
									return;
								}

								this.docAssistant.adapter.invalidateScreen();
							}.bind(this),

							onFailure: function(payload) {
								Mojo.Log.error("sortAscending failed: " + Object.toJSON( payload ) );
							}
						});
			break;

			case 'excel-sortza-cmd':
				//Mojo.Log.info("excel.js: sortza command");

				this.controller.serviceRequest( 'palm://com.palm.office/excel/sortDescending',
					{
						parameters: { docid: this.docAssistant.document_id, column: 1 },

						onSuccess: function(payload) 
						{
							if (payload.errorCode !== undefined || payload.column === undefined) {
								Mojo.Log.error("sortDescending failed to get note: " + Object.toJSON( payload ) );
								return;
							}

							this.docAssistant.adapter.invalidateScreen();
						}.bind(this),

						onFailure: function(payload) {
							Mojo.Log.error("sortDescending failed: " + Object.toJSON( payload ) );
						}
					});
				break;

			case 'excel-hide-cmd':
				//Mojo.Log.info("excel.js: hide command");

				this.controller.serviceRequest( 'palm://com.palm.office/excel/hideSelection',
					{
						parameters: { docid: this.docAssistant.document_id },

						onSuccess: function(payload) {
						},

						onFailure: function(payload) {
							Mojo.Log.error("hideSelection failed: " + Object.toJSON( payload ) );
						}
					});
				break;

			case 'excel-show-cmd':
				//Mojo.Log.info("excel.js: unhide all command");

				this.controller.serviceRequest( 'palm://com.palm.office/excel/unhideSelection',
					{
						parameters: { docid: this.docAssistant.document_id },

						onSuccess: function(payload) {
						},

						onFailure: function(payload) {
							Mojo.Log.error("unhideSelection failed: " + Object.toJSON( payload ) );
						}
					});
				break;
		}
	},


	showSheetCnt: function() {
		this.docAssistant.updatePageFootOf();
	},


	resetCheckmark: function()
	{
		for (var i = 0; i < this.serviceList.length; i++)
		{
			if (i == this.docAssistant.getCurrentPage()) {
				this.serviceList[i].status = 'images/check.png';
			}
			else {
				this.serviceList[i].status = '';
			}
		}
	}
});
