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

var InappropriateAssistant = Class.create({
	
    initialize: function(appDetails)
	{
		this._details = appDetails.getDetails();
	},
	
    setup: function()
	{
		this._addReview = this._addReview.bindAsEventListener(this);

		this.controller.get('txt-fa-in-title').innerText = this._details.title;
		this.controller.get('txt-fa-in-subtitle').innerText = this._details.creator;
		this.controller.get('img-fa-in-appIcon').style.backgroundImage = 'url(' + this._details.appIcon + ')';
        
        this.controller.setupWidget(
            'txt-fa-in-comment', 
            {
                focus: true,
                enterSubmits: false,
                multiline: true,
                modifierState: Mojo.Widget.steModeSentenceCase,
                focusMode: Mojo.Widget.focusInsertMode,
                requiresEnterKey: false,
                changeOnKeyPress: false,
            }
        );
		
		this._problemTypeModel = {problemType: 'bug'};
		this._problemTypes = [{label: $L('Bug'), value:'bug'},
								  {label: $L('Offensive Content'), value:'offensive'},
								  {label: $L('Legal Issue'), value:'legal'},
								  {label: $L('Other'), value:'other'}];
								
		this.controller.setupWidget('problemTypeSelector', {label: "", choices: this._problemTypes, modelProperty:'problemType'}, this._problemTypeModel);
		
		
		// Menus
		Weave.Utilities.AppMenu.useDefault(this);
		
		// Set up a command menu
		this.controller.setupWidget(Mojo.Menu.commandMenu, 
		{
			menuClass: 'no-fade', 
		}, {
			items: [
				{},
				{},
				{
	                label: $L('Send'),
	                command: 'addreview',
					icon: 'send'
	            },
			],
		});
    },
	
	activate: function()
	{
		//this.controller.get('addreview').addEventListener(Mojo.Event.tap, this._addReview);
	},
	
	deactivate: function()
	{
		//this.controller.get('addreview').removeEventListener(Mojo.Event.tap, this._addReview);
	},
	
	handleCommand: function(event)
	{
		if (event.type == Mojo.Event.command && event.command == 'addreview')
		{
			this._addReview(event);
		}
	},
	
	_addReview: function(event)
	{
		var self = this;
		Weave.Services.ApplicationServer.addUserComment(this._details.id, this.controller.get('txt-fa-in-comment').mojo.getValue(), 0, Mojo.Locale.current, "", false, true, function(status, response)
		{
			if (status) 
			{
				self.controller.stageController.popScene();
			}
			else
			{
				// ignore errors here
				// Utilities.Errors.displayError(response);
				self.controller.showAlertDialog(
				{
			    	onChoose: function(value)
					{
						if (value == "ok")
							self.controller.stageController.popScene();
					},
					title: $L("Error"),
				    message: $L("Could not submit your report at this time. Try again later."), 
				    choices:
					[
						{label:$L('OK'), value:'ok'}
				    ]
			  	});
			}
		});
	},
});