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

var ReminderAssistant = Class.create({
    initialize: function(params){
		this.focusField = params.focusField;
        this.focusWindow = params.focusWindow;
        this.personId = params.personId;
        this.reminderText = params.reminderText;
        this.fullName = params.fullName;
		this.hasReminderTextToLoad = params.hasReminderTextToLoad;
    },
    
    setup: function(){
        if (this.fullName && ((typeof this.reminderText) == "string")) {
            var header = Mojo.View.render({
                template: "reminder/header",
                object: this
            });
            this.controller.get("reminderHeader").innerHTML = header;
			this.alreadyReady = true;
        }
        else {
            AppAssistant.contactsService.details(this.personId, this.renderReminder.bind(this), this.controller, {
                collapse: false
            }, this.serviceFailed.bind(this));
        }
        
        this.reminderModel = {
            reminder: this.reminderText
        }
        
		var hintTextToDisplay = ReminderAssistant.reminderTextFieldHintValues.create;
		
		if (this.hasReminderTextToLoad){
			hintTextToDisplay = ReminderAssistant.reminderTextFieldHintValues.loading;
		}
		
        this.reminderAttributes = {
            multiline: true,
            modelProperty: "reminder",
            hintText: hintTextToDisplay
        }
        this.controller.setupWidget("reminderTextField", this.reminderAttributes, this.reminderModel)
        
        this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, {
  				items: [{
  					icon: 'delete',
  					label: 'Clear',
  					command: 'clear'
  				}]
  			});   
			
		if(!this.focusField){
			this.controller.setInitialFocusedElement(null);
		}     
    },
   	
  	handleCommand: function(event){
  		if (event.type === Mojo.Event.command && event.command === 'clear') {
        this.clearReminder();
  		};
  	},
    
    serviceFailed: function(){
        this.controller.stageController.popScene();
    },
    
    renderReminder: function(resp){
		this.contact = new Contact(resp.person);
		this.contact.fullName = this.contact.getFullName();
        var header = Mojo.View.render({
            template: "reminder/header",
            object: this.contact
        });
        this.controller.get("reminderHeader").innerHTML = header;
		this.reminderModel.reminder = resp.person.reminder;
		this.reminderAttributes.hintText = ReminderAssistant.reminderTextFieldHintValues.create;
		this.controller.modelChanged(this.reminderModel);
		if(this.aboutToActivateCallback){
			this.aboutToActivateCallback();
			delete this.aboutToActivateCallback;
		}
    },
    
    activate: function(){
		if(this.aboutToActivateCallback){
			delete this.aboutToActivateCallback;
		}
        if (this.focusWindow) {
            this.controller.stageController.activate();
            this.focusWindow = false;
        }
    },
	
	aboutToActivate:function(cb){
		if (this.alreadyReady) {
			console.log("already ready")
			cb();
		}
		else {
			console.log("Not yet ready")
			this.aboutToActivateCallback = cb;
		}
	},
    
    clearReminder: function(){
        this.reminderModel.reminder = null;
        this.controller.stageController.popScene();
    },
    
    cleanup: function(){
        AppAssistant.contactsService.setPersonDetails(this.controller, this.personId, this.reminderModel);
    }
})

ReminderAssistant.reminderTextFieldHintValues = {
	"loading" : $L("Loading reminder text..."),
	"create" : $L("Create new reminder...")
};
