var LABEL_DEVMODE_ON = $L("Your device is in Developer Mode. You may use Palm Mojo SDK tools to connect to the device");
var LABEL_DEVMODE_OFF = $L("Turn Developer mode on to turn on Developer Services on the device.");    
var LABEL_POPUP_MSG = $L("Your Device needs to restart for this action to take effect");
var LABEL_POPUP_MSG_TITLE = $L("Restart the Device?");

var ctrlr;

var DevmodeswitcherAssistant = Class.create( {

serviceName: 'palm://com.palm.connectionmanager',
    
initialize : function() {
    Mojo.Log.info("Initialize");

    $('modeSwitchDescription_ON_OFF').innerHTML = LABEL_DEVMODE_OFF;  

    this.onOffToggleOpt= {
	    modelProperty: 'toggleOpt',
	    trueValue: "ON",
	    falseValue: "OFF"		
    }; 

    this.toggleModel= {
	    toggleOpt: "OFF",
	    disabled : false
    };
},

setup: function(){
    Mojo.Log.info("Setup");
    ctrlr=this.controller;
    this.controller.setupWidget('onOffToggle', this.onOffToggleOpt, this.toggleModel);
    setTimeout(this.configure.bind(this), 400);
},

configure :function(){
    Mojo.Log.info("Configure");
    this.controller.get('scrim').hide();
 //   this.controller.modelChanged(this.toggleModel);
    this.getNovacomMode();
    $('onOffToggle').observe('mojo-property-change', this.selectorChangedHandler.bind(this));
    
},

showAlertDialog: function(msgTitle, msg){
    Mojo.Log.info("showAlertDialog called");
    try {
        this.controller.showAlertDialog({
            onChoose: function(value){
            },
            title: msgTitle,
            message: msg,
            choices: [{
                label: "Ok",
                value: "Ok",
                type: 'dismiss'
            }]
        });
    } catch (e) {
        Mojo.Log.info("Exception Error Displaying Popup Dialog", msgTitle, msg);
    }
},

getNovacomMode: function(){
    Mojo.Log.info("getNovacomMode called");
    
    try {
        this.controller.serviceRequest(this.serviceName, {
            method: 'getnovacommode',
            onFailure: function(responseMsg) { 
               var jsonObject = Object.toJSON(responseMsg);
                Mojo.Log.info("getnovacommode onFailure Response" + jsonObject);
            }.bind(this),
            onSuccess: function(responseMsg){
                var jsonObject = Object.toJSON(responseMsg);
                Mojo.Log.info("getnovacommode Response" + jsonObject);
                if (!responseMsg.returnValue) {
                    this.showAlertDialog("Error", "Could not get current Novacom state");
                }
                else {
                    if(responseMsg.isEnabled) {
                        if("ON"!=this.toggleModel.toggleOpt) {
                            this.toggleModel.toggleOpt="ON";
                            this.controller.modelChanged(this.toggleModel);
                                                   }
                    } else {
                        if("OFF"!=this.toggleModel.toggleOpt) {
                            this.toggleModel.toggleOpt="OFF";
                            this.controller.modelChanged(this.toggleModel);
                        }
                    }
                }
            }.bind(this)
        });
    } 
    catch (e) {
        this.showAlertDialog("Exception Error Getting Mode", "Getting Novacom Mode Failed");
    }
},


setNovaComModeEnabled: function(isEnabled) {
    this.controller.serviceRequest(this.serviceName, {
        method: 'setnovacommode',
        parameters: {'isEnabled': isEnabled},
        onFailure: function(responseMsg){ this.getNovacomMode(); }.bind(this),
        onSuccess: function(responseMsg){
            if (!responseMsg.returnValue) {
                this.showAlertDialog("Error Setting Mode", "Setting Device To Developer Mode Failed");
            }
            this.controller.get('scrim').show();
            this.controller.serviceRequest('palm://com.palm.power/shutdown', {
                method: 'machineReboot',
                parameters: { reason: "User initiated/DevModeSwitcher"  }
            });
        }.bind(this)                
    });    
},

selectorChangedHandler: function(e) {
    Mojo.Log.info("selectorChangedHandler");
    var displayMessage = LABEL_POPUP_MSG;
    var displayTitle = LABEL_POPUP_MSG_TITLE;

    if(e.value=="OFF") {
        $('modeSwitchDescription_ON_OFF').innerHTML = LABEL_DEVMODE_OFF;
    } else {
        $('modeSwitchDescription_ON_OFF').innerHTML = LABEL_DEVMODE_ON;
    }
        
        
    this.controller.showAlertDialog({
        onChoose: function(value){
            if (value == "reset") {
                Mojo.Log.info("Reset");         
                this.setNovaComModeEnabled((("ON"==this.toggleModel.toggleOpt) ? true : false));
    	    }
    	    else {
    	        if(e.value=="OFF") {
    	            this.toggleModel.toggleOpt="ON";
    	        } else {
                    this.toggleModel.toggleOpt="OFF";
    	        }
    		this.controller.modelChanged(this.toggleModel);
    	    }
        }, 
    	title: displayTitle,
    	message: displayMessage, 
    	choices: [{
    	    label: $L("Restart The Device"),
	    value: 'reset',
	    type:'negative'
	}, {
	    label: $L("Cancel"),
	    value: "cancel",
	    type: 'alert'
        }]
    });
},
});
