/* Copyright 2009 Palm, Inc.  All rights reserved. */

var PoundDataAdvancedAssistant = Class.create({
	
	initialize: function() {
		TelephonyCommands.dataFieldsQuery(this.dataFieldsQueryDone.bind(this));
		
	},
	
	fieldMap: $H({esn: "ESN", prev : "P_REV", meid: "MEID", homeorigvocoder:"Home Original Vocoder",
				  roamorigvocoder: "Roam Original Vocoder", homepagevocoder: "Home Page Vocoder",
				  scm: "SCM", slotmode: "Slot Mode", slotindex: "Slot Index", homenid: "Home NID",
				  homesid: "Home SID", lockcode: "Lock Code"}),
	moreFieldMap: $H({mcc : "MCC", mnc: "MNC", accolc: "Accolc",homesysreg:"Home Sys Reg", fsidreg: "FSID Reg", fnidreg: "FNID Reg"}),
	
	
	setup: function() {
		
		// Model to use for the command menu.
		this.cmdMenuBrowserModel = {
			items: [{}, {}, {}, {}]};
		// Set up view menu with scene header
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuBrowserModel);
			
		this.advListModel = {
            items: PoundData.poundDataAdvList			
        };
		
		this.advMoreListModel = {
            items: PoundData.poundDataAdvMoreList			
        };
				
		this.template = {
			itemTemplate: 'poundDataAdvanced/listitem',            		          
        };
		
		this.lbstextAttributes = {
			enterSubmits: true,
			focus: false,
			multiline: false,		
			maxLength: 40,
			modelProperty: "lbsvalue",
			disabledProperty: 'lbsdisabled',
		};
		
		this.lbsIpModel = {
		       'lbsdisabled': true,
		       'lbsvalue': 'XXX.XXX.XXX.XXX'
		};
		
		this.lbsPortModel = {
		       'lbsdisabled': true,
		       'lbsvalue': 'YYYY'
		};
		
		this.controller.setupWidget('lbsipaddress', this.lbstextAttributes, this.lbsIpModel);
		this.controller.setupWidget('lbsport', this.lbstextAttributes, this.lbsPortModel);
		
		Mojo.Event.listen(this.controller.get('lbsipaddress'), Mojo.Event.tap, this.verifyMSL.bindAsEventListener(this,'lbsip'));
		Mojo.Event.listen(this.controller.get('lbsport'), Mojo.Event.tap, this.verifyMSL.bindAsEventListener(this,'lbsport'));
		Mojo.Event.listen(this.controller.get('lbsipaddress'), Mojo.Event.propertyChange, this.lbsIPPropertyChangeHandler.bindAsEventListener(this));
		Mojo.Event.listen(this.controller.get('lbsport'), Mojo.Event.propertyChange, this.lbsPortpropertyChangeHandler.bindAsEventListener(this));		
					
        this.controller.setupWidget('advlist', this.template, this.advListModel);
		Mojo.Event.listen(this.controller.get('advlist'), Mojo.Event.propertyChange, this.advListChangeHandler.bindAsEventListener(this));
		Mojo.Event.listen(this.controller.get('advlist'), Mojo.Event.listTap, this.verifyMSL.bindAsEventListener(this,'advlist'));
		
		this.controller.setupWidget('morefieldslist', this.template, this.advMoreListModel);
		Mojo.Event.listen(this.controller.get('morefieldslist'), Mojo.Event.propertyChange, this.advMoreListChangeHandler.bindAsEventListener(this));
		Mojo.Event.listen(this.controller.get('morefieldslist'), Mojo.Event.listTap, this.verifyMSL.bindAsEventListener(this,'morefieldslist'));	
		
		this.textAttributes = {
			enterSubmits: true,
			focus: false,
			multiline: false,       
			textCase: Mojo.Widget.steModeLowerCase,
			focusMode: Mojo.Widget.focusSelectMode,
			maxLength: 40,
			modelProperty: "FIELDVALUE",
			disabledProperty: 'DISABLED',
		};
		
		this.controller.setupWidget('datafield', this.textAttributes, {});	
	
	},
	
	activate: function() {
		if (PoundData.mslVerified) {
			this.lbsIpModel.lbsdisabled = false;
			this.lbsPortModel.lbsdisabled = false;
			this.lbsIpModel.lbsvalue = 'XXX.XXX.XXX.XXX';
			this.lbsPortModel.lbsvalue = 'YYYY';
			this.controller.modelChanged(this.lbsIpModel,this);
			this.controller.modelChanged(this.lbsPortModel,this);
		}
	},
	
	cleanup: function() {
		this.controller.commitChanges();
			
		PoundData.poundDataAdvList.clear();
		PoundData.poundDataAdvMoreList.clear();			
	},
		
	dataFieldsQueryDone: function(success, status, dataInfo) {
		if(!dataInfo)
			return;
			
		if (success) {
			var fields = $H(dataInfo);
			
			this.fieldMap.each(function(pair) {
				var obj = new Object();
				obj.FIELDNAME = pair.value;
				obj.FIELDVALUE = fields.get(pair.key);
				if(PoundData.mslVerified) {
					if(pair.key == 'esn' || pair.key == 'prev' || pair.key == 'scm' || pair.key == 'meid')
						obj.DISABLED = true;
					else
						obj.DISABLED = false;
				}
				else 
					obj.DISABLED = true;
					
				//Don't show the lockcode until msl is verified.				
				if(pair.key == "lockcode" && !PoundData.mslVerified)
					this.lockCodeObject = obj;
				else
					PoundData.poundDataAdvList.push(obj);
					
			}.bind(this));
			
			this.moreFieldMap.each(function(pair) {
				var obj = new Object();
				obj.FIELDNAME = pair.value;
				obj.FIELDVALUE = fields.get(pair.key);
				if(PoundData.mslVerified) 
					obj.DISABLED = false;
				else
					obj.DISABLED = true;
				PoundData.poundDataAdvMoreList.push(obj);
			}.bind(this));
			
			this.controller.get('spiaaa').innerHTML = fields.get('spiaaaindicator');					
			this.controller.get('spihome').innerHTML = fields.get('spihaindicator');						
		}		
		 else {
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:false,
		    choices:[
	        {label: 'Done', value:true, type:'dismiss'}    
		    ]
		  	});	  
			}			
		this.controller.modelChanged(this.advListModel, this);
		this.controller.modelChanged(this.advMoreListModel, this);
	},
	
	advListChangeHandler:function(event) {
		
		if(!event.model)
			return;
		
		var key;
		var value = event.value;
		//Get the key from the Hash Map
		this.fieldMap.each(function(pair){
			if (pair.value === event.model.FIELDNAME) {
				key = pair.key;
				return;
			}
		});
		
		TelephonyCommands.dataFieldsSet(key,value,null);
	},
	
	advMoreListChangeHandler:function(event) {
		
		if(!event.model)
			return;
		
		var key;
		var value = event.value;
		//Get the key from the Hash Map
		this.moreFieldMap.each(function(pair){
			if (pair.value === event.model.FIELDNAME) {
				key = pair.key;
				return;
			}
		});
		
		TelephonyCommands.dataFieldsSet(key,value,null);
	},
	
	lbsIPPropertyChangeHandler: function(event) {		
		if(event.value == undefined)
			return;		
		TelephonyCommands.dataFieldsSet('lbsipaddress',event.value,null);
		Event.stop(event);
		this.lbsIpModel.lbsvalue = 'XXX.XXX.XXX.XXX';
		this.controller.modelChanged(this.lbsIpModel,this);
	},	
	
	lbsPortpropertyChangeHandler: function(event) {
		if(event.value == undefined)
			return;		
		TelephonyCommands.dataFieldsSet('lbsportnumber',event.value,null);
		Event.stop(event);
		this.lbsPortModel.lbsvalue = 'YYYY';
		this.controller.modelChanged(this.lbsPortModel,this);
	},
	
	verifyMSL: function(event,id) {
		this.edited = id;
				
		if(PoundData.mslVerified) {
			if(id == 'lbsip') {
				this.lbsIpModel.lbsvalue = '';
				this.controller.modelChanged(this.lbsIpModel,this);			
			}
			else if(id == 'lbsport') {
				this.lbsPortModel.lbsvalue = '';
				this.controller.modelChanged(this.lbsPortModel,this);
			}
			return;
		}
			
		if (id !== 'lbsip' && id !== 'lbsport') {
			if (!event.item) 
				return;
			this.selectedRow = event.index;
			if(event.item.FIELDNAME == 'ESN' || event.item.FIELDNAME == 'P_REV' || event.item.FIELDNAME == 'SCM' || event.item.FIELDNAME == 'MEID')
				return;
		}
		Event.stop(event);
		this.showMSLDialog();
		
	},
	
	verifiedMSL: function(success,msl) {
		
		PoundData.mslVerified = success;
		PoundData.edited = true;
		if(success) {			
			for(var i=0; i<PoundData.poundDataAdvList.length;i++) {
				if (PoundData.poundDataAdvList[i].FIELDNAME == 'ESN' || PoundData.poundDataAdvList[i].FIELDNAME == 'P_REV' ||
				PoundData.poundDataAdvList[i].FIELDNAME == 'SCM' || PoundData.poundDataAdvList[i].FIELDNAME == 'MEID')
					continue;
				PoundData.poundDataAdvList[i].DISABLED = false;						
			}
			//Add lockcode now.
			this.lockCodeObject.DISABLED = false;
			PoundData.poundDataAdvList.push(this.lockCodeObject);					
			this.controller.modelChanged(this.advListModel, this);				
			this.controller.get('advlist').mojo.focusItem(this.advListModel.items[this.selectedRow]);			
			
			for(var i=0; i<PoundData.poundDataAdvMoreList.length;i++)
				PoundData.poundDataAdvMoreList[i].DISABLED = false;	
			this.controller.modelChanged(this.advMoreListModel, this);
			this.controller.get('morefieldslist').mojo.focusItem(this.advMoreListModel.items[this.selectedRow]);			
			
			this.lbsIpModel.lbsdisabled = false;
			this.lbsPortModel.lbsdisabled = false;
			this.lbsIpModel.lbsvalue = '';
			this.lbsPortModel.lbsvalue = '';
			this.controller.modelChanged(this.lbsIpModel,this);
			this.controller.modelChanged(this.lbsPortModel,this);
		}			
		
	},
	
	showMSLDialog: function() {
				
		this.dialogBox = this.controller.showDialog({
			template: 'template/phone-msl-dialog',
			assistant: new MslAssistant(this),		
			preventCancel: true	
		});
	},
	
});
