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

var EraseAssistant = Class.create({
    initialize: function(screen, params){
        this.currentScreen = screen;
        this.params = params || {}
    },
    
    setup: function(){

    Mojo.Log.info("shutdown 1 setup");
        var appMenuModel = {
            visible: true,
            items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
        };
		
		this.controller.get('scrim').hide();
		
        this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, appMenuModel);

		this.restartMemoryModel = {buttonLabel: $L("Restart")};
        this.controller.setupWidget('restart-button', {}, this.restartMemoryModel);
		Mojo.Event.listen($('restart-button'),'mojo-tap', this.restart.bind(this, true));

        Mojo.Log.info("shutdown setup");

        this.shutdownMemoryModel = {buttonLabel: $L("Shut Down")};
        this.controller.setupWidget('shutdown-button', {}, this.shutdownMemoryModel);
	Mojo.Event.listen($('shutdown-button'),'mojo-tap', this.shutdown_confirm_dialog.bind(this));  //false



		this.systemMemoryModel = {buttonLabel: $L("Erase Apps & Data"), buttonClass: "negative"};		
        this.controller.setupWidget('memory-erase-button', {}, this.systemMemoryModel);
		Mojo.Event.listen($('memory-erase-button'),'mojo-tap', this.showDialog.bind(this, "partialErase"));
		
		this.usbMemoryModel = {buttonLabel: $L("Erase USB Drive"), buttonClass: "negative"};
        this.controller.setupWidget('usb-erase-button', {}, this.usbMemoryModel);
		Mojo.Event.listen($('usb-erase-button'),'mojo-tap', this.showDialog.bind(this, "usbErase"));

		this.deviceMemoryModel = {buttonLabel: $L("Full erase"), buttonClass: "negative"};
        this.controller.setupWidget('device-erase-button', {}, this.deviceMemoryModel);
		Mojo.Event.listen($('device-erase-button'),'mojo-tap', this.showFullEraseDialog.bind(this));


    },

    restart: function(){
		var resetMessage = $L("Resetting...");
		Mojo.Controller.getAppController().showBanner(resetMessage, {sentFromBanner: true});
		this.controller.get('scrim').show();
		this.controller.serviceRequest('palm://com.palm.power/shutdown', {
            method: 'machineReboot',
			parameters: {
				reason: "User initiated/DeviceInfo"				
			}
        });
    },

    shutdown_confirm_dialog: function(event){  //full
	//	Mojo.Log.info("shutdown_confirm_dialog");
		var infoMessage = $L("Are you sure? Your phone will be turned off and will not work until you turn it back on."); 

		var buttonMessage = $L("Shut Down"); 
		
		this.controller.showAlertDialog({
		    onChoose: this.shutdownHandler.bind(this),
		    message: infoMessage,
		    choices:[
		        {label:buttonMessage, value:"shutdown", type:'negative'},  
		        {label:$L("Cancel"), value:"keep", type: 'dismiss'},
		    ]
		  });


},

    shutdownHandler: function(value){  //full
		       Mojo.Log.info("shutdownhandler");
		   if(value=="shutdown"){
		var shutdownMessage = $L("Shutting down...");
		Mojo.Controller.getAppController().showBanner(shutdownMessage, {sentFromBanner: true});
		this.controller.get('scrim').show();
		this.controller.serviceRequest('palm://com.palm.power/shutdown', {
                        method: 'machineOff',
			parameters: {
				reason: "User initiated/DeviceInfo"				
			}
        });
		   }
},

    activate: function(){
    },

    deactivate: function(){
    },

    cleanup: function(){
    },
	
	
	handleCommand: function(event) {
		if(event.type == Mojo.Event.command) {
			if (event.command == Mojo.Menu.helpCmd) {
				Event.stop(event);
				var openParams = {
					target: 'http://help.palm.com/device_info/index.html'
				};
				this.controller.serviceRequest('palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {
						id: 'com.palm.app.help',
						params: openParams
					}
				});
			}
		} else if (event.type == Mojo.Event.commandEnable && 
		         (event.command == Mojo.Menu.helpCmd)) {
			event.stopPropagation();
		}
	},

    showDialog: function(eraseType, event) {
						
		var infoMessage, buttonMessage;
		
		if(eraseType === "usbErase") {
			infoMessage = $L("Are you sure? All your personal files stored on the USB drive will be erased.");
			buttonMessage = $L("Erase USB Drive"); 
		} 
		else {
			infoMessage = $L("Are you sure? Applications you installed and all application settings and data will be erased.");
			buttonMessage = $L("Erase Apps & Data"); 
		}
		
		this.controller.showAlertDialog({
		    onChoose: function(value) {
						if(value === "erase") 
							this.eraseHandler(eraseType);
						}.bind(this), 
		    message: infoMessage,
		    choices:[
		        {label:buttonMessage, value:"erase", type:'negative'},  
		        {label:$L("Cancel"), value:"keep", type: 'dismiss'},
		    ]
		  });
		
    },
	
	showFullEraseDialog: function() {
		this.dialogBox = this.controller.showDialog({
			template: 'erase/fullerase-dialog',			
			assistant: new FullEraseAssistant(this),			
		});
	},
	
    eraseHandler: function(value) {
		
		this.systemMemoryModel.disabled = true;
		this.deviceMemoryModel.disabled = true;
		this.usbMemoryModel.disabled = true;
		this.controller.modelChanged(this.systemMemoryModel);
		this.controller.modelChanged(this.deviceMemoryModel);
		this.controller.modelChanged(this.usbMemoryModel);

		var resetMessage = $L("Erasing...");
		Mojo.Controller.getAppController().showBanner(resetMessage, {source: 'notification'});
		this.controller.get('scrim').show();
		
		switch(value) {
			case 'erase': 
				this.deviceInfoRequest = AppAssistant.eraseService.eraseAll(this.controller);
				break;
			case 'secureerase':
				this.deviceInfoRequest = AppAssistant.eraseService.deviceWipe(this.controller);
				break;
			case 'partialErase':
				this.deviceInfoRequest = AppAssistant.eraseService.eraseVar(this.controller);
				break;
			case 'usbErase':
				this.deviceInfoRequest = AppAssistant.eraseService.eraseMedia(this.controller);
				break;
		}
    }
	
});

var FullEraseAssistant = Class.create({
	
	initialize: function(sceneAssistant) {
		this.sceneAssistant = sceneAssistant;
		
	},
	
	setup: function(widget) {
		this.widget = widget;
		$('erase').addEventListener(Mojo.Event.tap, this.erase.bindAsEventListener(this));
		$('secureerase').addEventListener(Mojo.Event.tap, this.secureErase.bindAsEventListener(this));
		$('cancel').addEventListener(Mojo.Event.tap, this.cancel.bindAsEventListener(this));
	},
	
	cancel: function(event) {
		this.widget.mojo.close();	
	},
	
	erase: function(event) {
		this.sceneAssistant.eraseHandler("erase");
		this.widget.mojo.close();	
	},
	
	secureErase: function(event) {
		this.sceneAssistant.eraseHandler("secureerase");
		this.widget.mojo.close();	
	},
	
	
	
});

