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

var MockTelephony = Class.create({
	initialize: function() {
		this.listenerCallbacks = $H();
	}, 
	
    subscribe: function(name, callback) {
		QDLogger.log( "MockTelephony::subscribe","MOCK TELEPHONY SUBSCRIBE " , name);
		this.listenerCallbacks.set(name, callback);
	},
	
	unsubscribe: function(name) {
		QDLogger.log( "MockTelephony::unsubscribe","MOCK TELEPHONY UNSUBSCRIBE " , name);
		this.listenerCallbacks.unset(name);	
	},
	  
    dial : function(number, blockId, success, failure) {
//    	success(this.noCallResponse);
//		this.sendDialingResponse();
		QDLogger.log( "MockTelephony::dial",number);
		setTimeout(this.sendDialingResponse.bind(this), 1000);		
    },

    answer : function(callId) {
    	this.sendCallActiveResponse();
    },
	
	sendStkResponse: function(callId) {
		setTimeout((function(){
	 		this.notifyListeners(stkResponse);		
		}).bind(this), 100);				
	},

	sendDialingResponse : function() {
    	//sending back dialing response for the success callback
    	//so that the init function for active call assistant
    	//knows what data to show on the screen. need to figure out
    	//how to display stuff between the dial request and getting 
    	//a dialing response from the Telephony Server
    	QDLogger.log( "MockTelephony::sendDialingResponse","Mock Telephony sending dialing response!!");
		if (this.currentState == undefined) {
			this.currentState = "singleDialing";
		} else if (this.currentState == "singleActive") {
			this.currentState = "dualLowerDialing";
		}

 		this.notifyListeners(mockResponses.get(this.currentState));
		setTimeout(this.sendCallActiveResponse.bind(this), 1000);		
	},
	
	
	sendCallActiveResponse: function() {
   		QDLogger.log("MockTelephony::sendCallActiveResponse", "Mock Telephony sending active response!!");
		if (this.currentState == "singleDialing" || this.currentState == undefined) {
			this.currentState = "singleActive";
		} else if (this.currentState == "singleActive" || this.currentState == "dualLowerDialing") {
			this.currentState = "dualLowerActive";
		}
 		this.notifyListeners(mockResponses.get(this.currentState));
	},

    disconnect: function() {
		this.currentState = undefined;
		disconnectAllResponse.macroEventCalls.eventCall.eventCalls.causeErrorCode = (PlatformType.isGSM() ? 16 : 25); 
    	setTimeout(this.notifyListeners.bind(this, disconnectAllResponse), 250);
    },
    
    testAllMockStates: function(index) {
		mockKeys = mockResponses.keys();
    	if (index >= mockKeys.size())
    		return;
		this.currentState = mockKeys[index];
  		this.notifyListeners(mockResponses.get(mockKeys[index]));
		setTimeout(this.testAllMockStates.bind(this), 1000, index + 1);
    },

	merge: function() {
		QDLogger.log("MockTelephony::merge", "MOCK MERGING CALL!!!! " , this.currentState);
		this.currentState = "conference";
		this.notifyListeners(mockResponses.get(this.currentState));
	},
		
	extract: function() {
		QDLogger.log( "MockTelephony::extract", "EXTRACTING CALL!!!! " , this.currentState)
	
		 if (this.currentState == "conference") {
		 	QDLogger.log( "MockTelephony::extract","EXTRACTING CALL!!!!");
			this.currentState = "dualLowerActive";
			this.notifyListeners(mockResponses.get(this.currentState));
		}		
	},
	
	testIncoming: function() {
		this.notifyListeners(incomingResponse);
		setTimeout((function(){
			QDLogger.log( "MockTelephony::testIncoming","SEND INCOMING DISCONNECT");
	 		this.notifyListeners(disconnectResponse);		
		}).bind(this),5000);
	},
	
	getCurrentState: function(callback) {
		QDLogger.log("MockTelephony::getCurrentState", "CURRENT STATE!! " , this.currentState);
		if (this.currentState != undefined) {
			callback(mockResponses.get(this.currentState));
		} else {
			Mojo.Log.info("PhoneApp: MockTelephony::getCurrentState %j", noCallResponse);
			callback(noCallResponse);
		}
	},
	
	checkEmergencyMode: function(callback) {
		callback({"extended":{"emergency":false}});
	},
	
	notifyListeners: function(response) {
		this.listenerCallbacks.each(function (pair) {
			QDLogger.log("MockTelephony::notifyListeners", "notifying");
			pair.value(response);
		})
	},
	
	/** handle push-and-hold for active call view stuff **/
	toggleNetworkType: function() {
		Mojo.Controller.getAppController().assistant.gsm = !(PlatformType.isGSM());	
	},
	
	pushActive: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("singleActive"));
	},
	
	pushActiveHeld: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("dualUpperActive"));
	},
	
	pushHeldActive: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("dualLowerActive"));
	},
	
	pushConference: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("conference"));
	},
	
	pushConferenceHeld: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("conferenceHeld"));
	},
	
	pushConferenceAndActive: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("conferenceplussingleactive"));
	},
	
	pushConferenceAndHeld: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("conferenceplussinglehold"));
	},
	
	pushCDMAMergeable: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("cdmaMergeable"));
	},
	
	pushSingleHeld: function() {
		Mojo.Controller.getAppController().assistant.showActiveCall();
		this.notifyListeners(mockResponses.get("singleHeld"));
	},
	
	togglePicture: function() {
		Mojo.Controller.getAppController().assistant.mockPicture =
			!(Mojo.Controller.getAppController().assistant.mockPicture); 
	}
    
});

MockTelephony.useMock = 0;

mockResponses = $H({
singleDialing : 
	{
	"macroEventCalls": 
		{
		"event": "dialing", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 1,
						"conferenceId": 0,
						"cnapName": "unknown",
						"number" : "4158675309",
						"counter":1,
						"callState": "dialing"
					},
					"previousStatus": "",
					"statusChange": "dialing",
					"causerrorCode": "",
					"causeText": ""
				}
			}
		}	
	},
singleActive:
	{ "macroEventCalls":{ 
		"event": "active", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 1,
						"counter":1,
						"conferenceId": 0,
						"cnapName": "unknown",
						"number" : "4158675309",
						"callState": "active"
					},
					"previousStatus": "",
					"statusChange": "active",
					"causerrorCode": "",
					"causeText": ""
				}
			},
		"singleActive": 		
			{
				"id": 1, 
				"conferenceId": 0,
				//"cnapName": "Felix Hoenikker", 
				//"contactName": "Felix Hoenikker", 
				"number" : "4158675309",
				"counter":1,
				"callState": "active" 
			}
		}
	},
	
	singleHeld:
	{ "macroEventCalls":{ 
		"event": "active", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 1,
						"counter":1,
						"conferenceId": 0,
						"cnapName": "unknown",
						"number" : "4158675303",
						"callState": "hold"
					},
					"previousStatus": "",
					"statusChange": "hold",
					"causerrorCode": "",
					"causeText": ""
				}
			},
		"singleHold": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"contactName": "Felix Hoenikker", 
				"number" : "4158675319",
				"counter":1,
				"callState": "hold" 
			}
		}
	},
singleHold:
	{"macroEventCalls":{ 
		"event": "hold", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 1,
						"conferenceId": 0,
						"cnapName": "Felix Hoenikker",
						"contactName": "Felix Hoenikker",
						"counter":1,
						"number" : "4158675311",
						"callState": "hold"
					},
					"previousStatus": "",
					"statusChange": "hold",
					"causerrorCode": "",
					"causeText": ""
				}
			},
		"singleHold": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"counter":1,
				"contactName": "Felix Hoenikker", 
				"number" : "4158675312",
				"callState": "hold" 
			}
		}
	},
dualLowerDialing:
	{"macroEventCalls":{ 
		"event": "dialing", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 2,
						"conferenceId": 0,
						"cnapName": "",
						"contactName": "Charlie Chaplin",
						"number" : "4158675313",
						"counter":2,
						"callState": "dialing"
					},
					"previousStatus": "",
					"statusChange": "dialing",
					"causerrorCode": "",
					"causeText": ""
				}
			},
		"singleActive": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"contactName": "Felix Hoenikker", 
				"number" : "4158675337",
				"counter":1,
				"callState": "active" 
			},
		}
	},
dualUpperActive:
	{"macroEventCalls":{ 
		"event": "active", 
		"singleActive": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"contactName": "Felix Hoenikker", 
				"number" : "4158675336",
				"counter":1,
				"callState": "active",
				"origin":"incoming"
			},
			"singleHold": 		
			{
				"id": 2, 
				"conferenceId": 0,
				"cnapName": "", 
				"contactName": "Charlie Chaplin", 
				"counter":2,
				"number" : "4158675335",
				"callState": "hold",
				"origin":"incoming" 
			} 	 
		}
	},
dualLowerActive:
		{"macroEventCalls":{ 
		"event": "active", 
		"singleHold": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"contactName": "Felix Hoenikker", 
				"number" : "4158675334",
				"counter":1,
				"callState": "hold" 
			},
			"singleActive": 		
			{
				"id": 2, 
				"conferenceId": 0,
				"cnapName": "", 
				"counter":2,
				"contactName": "Charlie Chaplin", 
				"number" : "4158675333",
				"callState": "active" 
			} 	 
		}
	},
conference:
		{"macroEventCalls":{ 
		"event": "info", 
		"conferenceActive": 		
			{
				"size": 2, 
				"calls": 
				[ 
					{
						"id": 1, 
						"conferenceId": 0,
						"cnapName": "Felix Hoenikker", 
						"contactName": "Felix Hoenikker", 
						"number" : "4158675332",
						"callState": "active" 
					},
					{
						"id": 2, 
						"conferenceId": 0,
						"cnapName": "", 
						"contactName": "Charlie Chaplin", 
						"number" : "4158675331",
						"callState": "active" 
					} 
		 		] 
			} 			
		}
	},
	conferenceHeld:
		{"macroEventCalls": {
		"counter": 13,
		"event": "hold",
		"eventCall": {
			"eventCalls": {
				"call": {
					"callState": "active",
					"id": 3,
					"contactName": "unknown(mocked)",
					"counter": 10,
					"number": "4158675330",
					"cnapName": "unknown(mocked)",
					"conferenceId": 0
				},
				"previousStatus": "incoming",
				"statusChange": "active"
			}
		},
		"conferenceHold": {
			"calls": [{
				"callState": "conference_hold",
				"id": 4,
				"contactName": "unknown(mocked)",
				"counter": 5,
				"number": "4158675329",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}, {
				"callState": "conference_hold",
				"id": 2,
				"contactName": "unknown(mocked)",
				"counter": 1,
				"number": "4158675328",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}, {
				"callState": "conference_hold",
				"id": 3,
				"contactName": "unknown(mocked)",
				"counter": 1,
				"number": "4158675327",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}, {
				"callState": "conference_hold",
				"id": 1,
				"contactName": "unknown(mocked)",
				"counter": 1,
				"number": "4158675326",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}],
			"size": 2
		},
	}
	},
conferenceplussingleactive: {
	"macroEventCalls": {
		"counter": 13,
		"event": "active",
		"eventCall": {
			"eventCalls": {
				"call": {
					"callState": "active",
					"id": 3,
					"contactName": "unknown(mocked)",
					"counter": 10,
					"number": "4158675325",
					"cnapName": "unknown(mocked)",
					"conferenceId": 0
				},
				"previousStatus": "incoming",
				"statusChange": "active"
			}
		},
		"conferenceHold": {
			"calls": [{
				"callState": "conference_hold",
				"id": 2,
				"contactName": "unknown(mocked)",
				"counter": 5,
				"number": "4158675324",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}, {
				"callState": "conference_hold",
				"id": 1,
				"contactName": "unknown(mocked)",
				"counter": 1,
				"number": "4158675323",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}],
			"size": 2
		},
		"singleActive": {
			"callState": "active",
			"id": 3,
			"contactName": "unknown(mocked)",
			"counter": 10,
			"number": "4158675322",
			"cnapName": "unknown(mocked)",
			"conferenceId": 0
		}
	}
},
conferenceplussinglehold: {
	"macroEventCalls": {
		"counter": 13,
		"event": "active",
		"eventCall": {
			"eventCalls": {
				"call": {
					"callState": "active",
					"id": 3,
					"contactName": "unknown(mocked)",
					"counter": 10,
					"number": "4158675321",
					"cnapName": "unknown(mocked)",
					"conferenceId": 0
				},
				"previousStatus": "incoming",
				"statusChange": "active"
			}
		},
		"conferenceActive": {
			"calls": [{
				"callState": "conference_active",
				"id": 2,
				"contactName": "unknown(mocked)",
				"counter": 5,
				"number": "4158675320",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}, {
				"callState": "conference_active",
				"id": 1,
				"contactName": "unknown(mocked)",
				"counter": 1,
				"number": "4158675319",
				"cnapName": "unknown(mocked)",
				"conferenceId": -1
			}],
			"size": 2
		},
		"singleHold": {
			"callState": "hold",
			"id": 3,
			"contactName": "unknown(mocked)",
			"counter": 10,
			"number": "4158675318",
			"cnapName": "unknown(mocked)",
			"conferenceId": 0
		}
	}
},
cdmaMergeable:
		{"macroEventCalls":{ 
		"event": "active", 
		"singleHold": 		
			{
				"id": 1, 
				"conferenceId": 0,
				"cnapName": "Felix Hoenikker", 
				"contactName": "Felix Hoenikker", 
				"number" : "4158675317",
				"counter":1,
				"callState": "hold" 
			},
			"singleActive": 		
			{
				"id": 2, 
				"conferenceId": 0,
				"cnapName": "", 
				"counter":2,
				"contactName": "Charlie Chaplin", 
				"number" : "4158675316",
				"callState": "active",
				"origin":"dialing" 
			} 	 
		}
	}
});


disconnectAllResponse = {
		"macroEventCalls":{ 
		"event": "disconnected", 
		"eventCall": 
			{
				"eventCalls": 
				{
					
					"previousStatus": "",
					"statusChange": "disconnected",
					"causeErrorCode": "",
					"causeText": ""
				}
			}
		}
	};

noCallResponse = {
		"macroEventCalls":{ 
		}
	};
		
disconnectResponse = 
	{"macroEventCalls": 
		{"counter": 14 ,
		 "event": "disconnected",
		 "eventCall": 
		 	{"eventCalls": 
				{"call": 
					{"callState":"disconnected",
					"id":1,
					"number":"4158675315",
					"contactName":"unknown(mocked)",
					"counter":2,
					"cnapName":"unknown(mocked)",
					"conferenceId":-1}, 
				"previousStatus": "conference_hold",
				 "statusChange": "disconnected"}
				 }
			}
		};
	
incomingResponse = {
		"macroEventCalls": 
		{
		"event": "zQuery", 
		"eventCall": 
			{
				"eventCalls": 
				{
					"call": {
						"id": 1,
						"conferenceId": 0,
						"cnapName": "",
						"contactName": "Felix Hoenikker",
						"number":"4158675314",
						"callState": "incoming"
					},
					"previousStatus": "",
					"statusChange": "incoming",
					"causerrorCode": "",
					"causeText": ""
				}
			}
		}	
	};


stkResponse = {"eventSTKDT": 
	{"messageId":23, 
	"delayClear":true , 
	"highPriority":true ,
	"encodingType":"" , 
	"encodedText":"" , 
	"decodedText":"This is a Mock STK test"
	}
};

/*
dialingResponse = {
	"eventCalls": 
	{ 	
		"call": 
		{
			"id": 1, 
			"conferenceId": 0,
			"cnapName": "", 
			"contactName": "Felix Hoenikker", 
			"callState": "dialing" 
		}, 
		"previousStatus": "", 
		"statusChange": "", 
		"causerrorCode": "", 
		"causeText": ""  
	}
};
 
activeResponse = {
	"eventCalls": 
	{ 	
		"call": 
		{
			"id": 1, 
			"conferenceId": 0,
			"cnapName": "", 
			"contactName": "Felix Hoenikker", 
			"callState": "active" 
		}, 
		"previousStatus": "", 
		"statusChange": "", 
		"causerrorCode": "", 
		"causeText": ""  
	}
};
*/