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

var MissedcallAssistant = Class.create({
    initialize: function(displayName, displayNumber, picture, contact, message, timeStamp) {
    	QDLogger.log( "MissedcallAssistant::initialize", displayName);
		this.appControl = Mojo.Controller.getAppController();
		this.contact = contact;
		this.updateFields(displayName, displayNumber, picture, message, timeStamp);
		this.windowName = "missed";
		this.appAssistant = Mojo.Controller.getAppController().assistant;
		this.telListener = this.appAssistant.telephonyEventListener;
		this.contacts = this.appAssistant.contacts; 
		this.announcer = this.appAssistant.announcer;
		this.acked = false;
		this.timeStamp = timeStamp;
		this.onDisplayEvent = this.onDisplayEvent.bind(this); 
		
    },
    
    setup: function(){
		QDLogger.log( "MissedcallAssistant::setup");
		if (this.contacts.isNumberValid(this.number)) {
			this.controller.get('redial_button').addEventListener(Mojo.Event.tap, this.onRedial.bindAsEventListener(this));
		} else {
			this.controller.get('redial_button').hide();
		}
		this.controller.get('cancel_button').addEventListener(Mojo.Event.tap, this.onCancel.bindAsEventListener(this));
		this.renderScene();
		// stay up for 1 minute
		this.missedTimeout = window.setTimeout(this.closeWindow.bind(this), MissedcallAssistant.kTimeoutMs);
		this.controller.listen(this.controller.stageController.document, Mojo.Event.deactivate, this.onBlur.bind(this));
		this.controller.get('missed_message').addEventListener(Mojo.Event.tap, this.onContactTap.bindAsEventListener(this));
		this.controller.get('contactPictureContainer').addEventListener(Mojo.Event.tap, this.onContactTap.bindAsEventListener(this));
		this.telListener.displayStateRegisterCallback(this.onDisplayEvent);
		this.controller.stageController.indicateNewContent(true);
    },
	
	
	cleanup: function() {
		// opens dashboard if user didn't explicitly close this window (this covers
		// being minimized via center button
		
		if (this.acked === false) {
			this.announcer.announceMissedDashboard(this.number, this.contact, this.timeStamp)
		}
		
		window.clearTimeout(this.missedTimeout);
		this.missedTimeout = undefined;

		this.telListener.displayStateUnregisterCallback(this.onDisplayEvent);
	},
	
	
	onBlur: function() {
		this.closeWindow();
	},
			
	updateFields: function(name, number, picture, message, timeStamp) {
		QDLogger.log( "MissedcallAssistant::updateFields", number);
		if (name == undefined || name == "unknown")
			name = "";
		this.displayName = name;
		this.number = number;
		this.displayNumber = this.formatNumber(number);
		this.pictureLoc = this.formatPicture();

		// if the phone number is the same as the contact name, blank out the number
		if (this.displayNumber === this.displayName) {
			this.displayNumber = "";
		}
	
		// ensure empty label is displayed as empty
		this.displayLabel = (this.contact.labelFormatted ? this.contact.labelFormatted  : "");
		var  tempDateString = Mojo.Format.formatDate(new Date(parseInt(timeStamp)), {time:'short'});
		this.message = message.interpolate({timeStamp: tempDateString});
		QDLogger.log( "MissedcallAssistant::updateFields", "end");
	},
	
	renderScene: function(){ 
        QDLogger.log( "MissedcallAssistant::renderScene");
		 
        var content = Mojo.View.render({
            object: this,
            template: "missedcall/missed"
        });
        this.controller.get('missed_message').update(content);
		
		if (this.contact && this.contact.id) {
			this.controller.get('contactPicture').src = this.pictureLoc;
			this.controller.get('contactPictureContainer').show();
			this.controller.get('missedIcon').hide();
		} 
    },
	
    onCancel: function() {
		this.acked = true;
		this.closeWindow();
	},
    
    onRedial: function(){
        QDLogger.log( "MissedcallAssistant::onRedial");
        this.appAssistant.launchDial(this.number);
        this.acked = true;
        this.closeWindow();
    },
    
	closeWindow: function() {
		this.appControl.closeStage(this.windowName);
	},
	
	formatNumber: function(number){
		if (number)
			return FormatPhoneNumber(number);
		else
			return Messages.unknownNumber;
	},
	
	formatPicture: function() {
		if (this.contact && this.contact.pictureLocSmall) {
			return this.contact.pictureLocSmall
		} else {
			return "images/generic-details-view-avatar.png";
		}
	},
	
	// launches the contact app with contact card or pseudocard depending
	// on if the passed line has a contact book entry or not
	onContactTap: function(event){
		event.stop();
		
		if (this.contact && this.contact.id) {
			TelephonyCommands.contactsLaunchWithId(this.contact.id);
		} else if (this.contacts.isNumberValid(this.number)) {
			TelephonyCommands.contactsLaunchWithPseudocard(this.number);
		}
		
		this.acked = true;
		this.closeWindow();
	},
	
	// if the time is more than the timeout length when the screen goes on
	// minimize this to a dashboard so other notifications are visible
	onDisplayEvent: function(payload) {
		if (payload.event && payload.event == 'displayOn') {
			if ((Date.now() - this.timeStamp)  > MissedcallAssistant.kTimeoutMs) {
				this.closeWindow();
			}
		}	
	}
    
});

MissedcallAssistant.kTimeoutMs = 60 * 1000;
