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

var FormattedCall =  Class.create({
	initialize: function(id, lineState) {
		if (!lineState || !lineState[id]) {
			QDLogger.error( "FormattedCall", "null call");
		}
		
		// it's a conference if it has a conference ID
		if (lineState[id].confId !== undefined)
			this.formatConferenceCall(id, lineState);
		else
			this.formatCall(id, lineState);
	},
	
	// given the line number and the service's list of calls
	// build object whose properties will be Mojo.Rendered into
	// template, including array of member calls
	formatConferenceCall: function(lineNumber, callMsg) {
		QDLogger.log( "formatConferenceCall");
		this.state = this.formatState(callMsg[lineNumber].state);
        this.conferenceLabel = this.formatConferenceLabel();
		// TODO: not sure why i didn't have this in the first place
		this.contact = this.conferenceLabel;
		this.lineNumber = lineNumber;
		// build array of member calls
		this.members = [];
		for (var i = 0; i < callMsg[lineNumber].calls.length; i++) {
			QDLogger.log( "formatConferenceCall", "adding member");
			this.members[i] = new FormattedCall(i, callMsg[lineNumber].calls);
		}
	},
	
	// return the label for the conference call
	formatConferenceLabel: function() {
		return Messages.conferenceCall;
	},
	
	// given the line number and the service's list of calls
	// build call object whose properties will be Mojo.Rendered into
	// template
    formatCall: function(lineNumber, callMsg){
        QDLogger.log( "formatCall", "LINE/STATE: ",  lineNumber , callMsg[lineNumber].state);
       
		this.number = this.formatNumber(callMsg[lineNumber].number);
		this.picture = callMsg[lineNumber].contact.pictureLoc || "images/contacts-unknown-icon-large.png";
		this.state = this.formatState(callMsg[lineNumber].state);
		this.contact = this.formatContact(this.formatNumber(callMsg[lineNumber].number), callMsg[lineNumber].contact.name, callMsg[lineNumber].cnapName),
		this.lineNumber = lineNumber;
		this.id = callMsg[lineNumber].id;
		
		// ensure empty label is displayed as empty
		this.displayLabel = (callMsg[lineNumber].contact.labelFormatted ? callMsg[lineNumber].contact.labelFormatted : "");
		this.numberCaption = ((callMsg[lineNumber].contact.name !== undefined || callMsg[lineNumber].contact.id !== undefined) ? this.number : " ");
		
    },
    
	// given a phone number (or empty/unknown value), return the formatted and
	// localized version 
    formatNumber: function(number){
        if (number) {
			return FormatPhoneNumber(number);
		} else {
			return Messages.unknownNumber;
		}
    },
    
	// given at least one of the formatted number, name from contacts,
	// and name from cnap, return the right value
	formatContact: function(number, contactName, cnapName){
        if (contactName) 
            return contactName;
        else if (cnapName && cnapName != "unknown") 
            return cnapName;
        else if (number)
			return number
		else
            return "";
    },
    
	// give a state of the phone call, return the localized value
    formatState: function(state){
		switch (state) {
			case TelephonyCallState.ACTIVE:
				return "";
			case TelephonyCallState.DIALING: 
				return Messages.callStateDialing;
			case TelephonyCallState.DIALPENDING: 
				return Messages.callStateDialing;	
			case TelephonyCallState.DISCONNECTPENDING:
				return Messages.callStateEnding;
			case TelephonyCallState.DISCONNECTED:
				return Messages.callStateEnded;
			case TelephonyCallState.HOLD:
				return Messages.callStateHold;
			default:
				return state;
		}
    },
    
});