/**
 * @author rogerstringer
 */
var selectAssistantClass = null;
var MAX_CHECK_BOXES    = 10;
var newLevelId         = -1;
var oldLevelId         = -1;

var SelectAssistant = Class.create ({
	
	initialize: function(contextName, levelName, levelChoices) {
		/* this is the creator function for your scene assistant object. It will be passed all the 
		   additional parameters (after the scene name) that were passed to pushScene. The reference
		   to the scene controller (this.controller) has not be established yet, so any initialization
		   that needs the scene controller should be done in the setup function below. */
		selectAssistantClass = this;
		if (contextName && levelName)  this.contextLevel = {'context' : contextName, 'level':levelName};
		if (levelChoices) this.levelNames      = levelChoices;
	},

	setup: function() {
		
		this.controller.get('selectContext').update ("Context: " + this.contextLevel.context);
		
		for (var i = 0; i < this.levelNames.length && i < MAX_CHECK_BOXES; i++) {
			var checkName = "selectCheck-" + i;
			var labelName = "selectName-" + i;
			if (levelNames[i].name == this.contextLevel.level) {
				var markerName = "marker-" + i;
				//this.selectArrayModel[i].value = true;
				oldLevelId = i;
				//this.controller.get(labelName).update(this.levelNames[i].name.toUpperCase() + " **");
				this.controller.get(labelName).update(this.levelNames[i].name.toUpperCase());
				$(markerName).style.display = "inline";
			}
			else {
				this.controller.get(labelName).update(this.levelNames[i].name.toUpperCase());
				// Setting up a checkbox widget
				this.selectArrayModel[i].value = false;
				this.controller.setupWidget(checkName, {}, this.selectArrayModel[i]);
				this.controller.get(checkName).observe(Mojo.Event.propertyChange, this.handleCheckChange.bindAsEventListener(this));
			}
		} // end for loop
	},
		
	handleCheckChange: function (event) {
		var i;
		newLevelId = -1;
		for (i = 0; i < this.levelNames.length && i < MAX_CHECK_BOXES; i++) {
			if (this.selectArrayModel[i].value == true) {
				if (newLevelId > -1) {
					this.selectArrayModel[newLevelId].value = false; // just-in-case
					this.controller.modelChanged(this.selectArrayModel[newLevelId], this);
				}
				newLevelId = i;
			}
		}
		if (newLevelId == -1) return; // nothing selected, no action
		this.confirmSelection (newLevelId);
	},
	
	confirmSelection: function(newlevelId) {
		var newLevel = this.levelNames[newLevelId].name;
		var text = "Please confirm you want to change log level for context \"" 
				+ this.contextLevel.context + "\" from \""
				+ this.contextLevel.level.toUpperCase() + "\" to \"" + newLevel.toUpperCase() + "\"?";
		var that = this;
		this.controller.showAlertDialog({
			onChoose: function(value){
				if (value == 'ok') {
					that.postLogLevelChange(that.levelNames[newLevelId].name);
				}
				else {
					that.selectArrayModel[newLevelId].value = false;
					this.controller.modelChanged(this.selectArrayModel[newLevelId], this);
				}
			},
			title: $L("CONFIRMATION"),
			message: text,
			allowHTMLMessage: true,
			choices: [
				{label: $L('Yes'), value: 'ok', type: 'color'},
				{label: $L('No'), value: 'cancel', type: 'color'}
			]
		});
	},
	
	postLogLevelChange: function (name) {
		// Update log level and exit
		this.selectArrayModel[newLevelId].value = false;
		this.controller.modelChanged(this.selectArrayModel[newLevelId], this);
		$("levelTable").style.display = 'none';
		// set context level
		this.controller.serviceRequest("palm://com.palm.logctld", {
			method: 'setloglevel',
			parameters: {
				'context': this.contextLevel.context,
				'level': name
			},
			onSuccess: function (result) {
				$('levelTable').style.display = 'block';
				if (result.returnValue && result.returnValue === "true") {
					Mojo.Controller.stageController.popScenesTo('level');
				}
			},
			onFailure: function(result) {
				console.log("failed");
				$('levelTable').style.display = 'block';
				Mojo.Controller.stageController.popScenesTo('level');
			}
		});
			

	},

	showDialogBox: function (title, message) {
		this.controller.showAlertDialog({
			onChoose: function(value){
			},
			title: (title && title.length > 0 ? title : $L("Information")),
			message: (message ? message : ''),
			choices: [{label: $L('OK'),	value: 'ok', type: 'color'}]
		});
	},

	activate: function(event) {
		/* put in event handlers here that should only be in effect when this scene is active. For
		   example, key handlers that are observing the document */
	},


	deactivate: function(event) {
		/* remove any event handlers you added in activate and do any other cleanup that should happen before
		   this scene is popped or another scene is pushed on top */
	},

	cleanup: function(event) {
		/* this function should do any cleanup needed before the scene is destroyed as 
		   a result of being popped off the scene stack */
	},

	contextLevel: {
		"context":"&lt;global&gt;", 
		"level":"debug"
	},
	
	levelNames: [
		{"name":"none"},
		{"name":"emerg"},
		{"name":"alert"},
		{"name":"crit"},
		{"name":"err"},
		{"name":"warning"},
		{"name":"notice"},
		{"name":"info"},
		{"name":"debug"},
	],

	selectArrayModel: [
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
		{value: false, disabled: false},
	],

});
