
WebMyth.telnetTimeout = 0;
WebMyth.welcomeSceneController = 0;


WebMyth.mythprotocolCommand = function(sceneController, protoCommand, successBanner){
		
	Mojo.Log.info("Starting new protocol command: "+protoCommand);
		
	sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
		method:"mythprotocolCommand",
			parameters:{
				"port":"WebMyth.prefsCookieObject.masterBackendPort", 
				"address":WebMyth.prefsCookieObject.masterBackendIp,
				"protocolVersion": WebMyth.prefsCookieObject.protoVer,
				"command": protoCommand
				},
			onSuccess: function(response) {
				Mojo.Log.info("Protocol command success of %j", response);
				//Mojo.Controller.getAppController().showBanner("Protocol command success: "+response.reply, {source: 'notification'});
				
				if(successBanner) {
					Mojo.Controller.getAppController().showBanner(successBanner, {source: 'notification'});
				}	
	
				}.bind(this),
			onFailure: function(response) {
		  
					Mojo.Log.info("Failed service connection status of %j", response);
					Mojo.Controller.getAppController().showBanner("Protocol command FAIL", {source: 'notification'});
	
				}.bind(this),
		});
	
};


WebMyth.startNewCommunication = function(sceneController1){

	var sceneController = WebMyth.welcomeSceneController;

	
	//Close any existing keep alive processes
	clearInterval(WebMyth.telnetTimeout); 
	WebMyth.frontendRetries = 0;
		
	Mojo.Log.info("Starting new service communication");
		
	 sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
		  method:"startCommunication",
		  parameters:{"port":WebMyth.prefsCookieObject.currentFrontendPort, address:WebMyth.prefsCookieObject.currentFrontendAddress},
		  onSuccess: function(response) {
				Mojo.Log.info("Success service connection status of %j", response);
				
				if(response.hasError){
					Mojo.Controller.getAppController().showBanner(response.error, {source: 'notification'});
				} else {
					Mojo.Controller.getAppController().showBanner("New service connection success", {source: 'notification'});
					//Start trying to keep alive
					WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 1000);
				}
				
	
			}.bind(this),
		  onFailure: function(response) {
					
				WebMyth.telnetConnected = false;
		  
					Mojo.Log.info("Failed service connection status of %j", response);
					Mojo.Controller.getAppController().showBanner("Start new FAIL", {source: 'notification'});
				
				//WebMyth.sendServiceCmd(sceneController, retryCommand, true);
	
			}.bind(this),
		});
	
};

WebMyth.startCommunication = function(sceneController1, retryCommand){

	var sceneController = WebMyth.welcomeSceneController;
	
	
	//Close any existing keep alive processes
	clearInterval(WebMyth.telnetTimeout); 
	WebMyth.frontendRetries = 0;
		
	Mojo.Log.info("Starting service communication");
		
	sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
		  method:"startCommunication",
		  parameters:{"port":WebMyth.prefsCookieObject.currentFrontendPort, address:WebMyth.prefsCookieObject.currentFrontendAddress},
		  onSuccess: function(response) {
				Mojo.Log.info("Success service connection status of %j", response);
				
				
				if(response.hasError){
					Mojo.Controller.getAppController().showBanner(response.error, {source: 'notification'});
				} else {
					Mojo.Controller.getAppController().showBanner("Service connection success", {source: 'notification'});
					
					if(retryCommand){
						Mojo.Log.info("Tring to resend last command");
						
						WebMyth.sendServiceCmd(sceneController, retryCommand);
					
						//Start trying to keep alive
						WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 3000);
					
					} else {
					
						//Start trying to keep alive
						WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 500);
						
					}
				}
	
			}.bind(this),
		  onFailure: function(response) {
		  
				if(retryCommand){
					Mojo.Log.info("Tried to reconnect - hopefully it worked");
					
					//Start trying to keep alive
					WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 1000);
				
				} else {
					Mojo.Log.info("Failed service connection status of %j", response);
					Mojo.Controller.getAppController().showBanner("Start communication FAIL", {source: 'notification'});
					
					WebMyth.telnetConnected = false;
				}
				
				//WebMyth.sendServiceCmd(sceneController, retryCommand, true);
	
			}.bind(this),
		});
	
};

WebMyth.sendServiceCmd = function(sceneController1, value, wasRetry){

	var sceneController = WebMyth.welcomeSceneController;

	
	//Close any existing keep alive processes
	clearInterval(WebMyth.telnetTimeout); 
	WebMyth.frontendRetries = 0;
		
	Mojo.Log.info("Sending service command '"+value+"'");
		
	 sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
		  method:"send",
		  parameters:{"toSend":value},
		  onSuccess: function(response) {
				Mojo.Log.info("Success service send of %j", response);
				//Mojo.Controller.getAppController().showBanner("Service send success: "+response.reply, {source: 'notification'});
				
				if(response.hasError){
					Mojo.Log.error(response.error);
					
					if(wasRetry){
						Mojo.Log.error("Was a retry that failed");
						Mojo.Controller.getAppController().showBanner("Retry send failed", {source: 'notification'});
					} else {
						Mojo.Log.error("Was not a retry, trying to restart communication");
						WebMyth.startCommunication(sceneController, value);
					}
					
				} else {
					
					//Start trying to keep alive
					WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 3000);
				
				}
	
			}.bind(this),
		  onFailure: function(response) {
				Mojo.Log.info("Failed service send status of %j", response);
				Mojo.Controller.getAppController().showBanner("Failed to send - try again", {source: 'notification'});
				
				if(wasRetry){
					Mojo.Log.error("Was a retry that failed");
					Mojo.Controller.getAppController().showBanner("Retry send failed", {source: 'notification'});
				} else {
					Mojo.Log.error("Was not a retry, trying to restart communication");
					WebMyth.startCommunication(sceneController, value);
				}
	
			}.bind(this),
	});
	
};

WebMyth.keepTelnetAlive = function(sceneController1){

	var sceneController = WebMyth.welcomeSceneController;

	WebMyth.frontendRetries++;
		
	//Mojo.Log.info("Keeping telnet connection alive");
	var value = "query location", wasRetry = false;
	
	//Close any existing keep alive processes
	clearInterval(WebMyth.telnetTimeout); 
		
	 sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
		  method:"send",
		  parameters:{"toSend":value},
		  onSuccess: function(response) {
				Mojo.Log.info("Success %s keep alive %j", WebMyth.frontendRetries, response);
					
				if(response.hasError){
					Mojo.Log.error(response.error);
					
					if(wasRetry){
						Mojo.Log.error("Was a retry that failed");
						Mojo.Controller.getAppController().showBanner("Retry send failed", {source: 'notification'});
					} else {
						Mojo.Log.error("Was not a retry, trying to restart communication");
						WebMyth.startCommunication(sceneController, value);
					}
					
				} else if (WebMyth.frontendRetries > 20){
				
					//Go silent after 20 retries/60 seconds
				
					WebMyth.frontendLocation =  "";
				
					Mojo.Log.info("Stopping keep alive due to inactivity");
				
				} else {
				
					WebMyth.frontendLocation =  response.reply;
				
					//Restart function after 3 seconds
					WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 3000);
				
				}
	
			}.bind(this),
		  onFailure: function(response) {
				Mojo.Log.info("Failed to keep alive: %j", response);
	
			}.bind(this),
	});
	
};

WebMyth.playServiceChannel = function(sceneController1, value, wasRetry){

	var sceneController = WebMyth.welcomeSceneController;

	//Close any existing keep alive processes
	clearInterval(WebMyth.telnetTimeout); 
	WebMyth.frontendRetries = 0;
		
	Mojo.Log.info("Sending service play channel "+value);
	
	if(WebMyth.frontendLocation == ""){
		//We have stopped keeping alive - need to restart to get location now
		Mojo.Log.info("We have stopped keeping alive - need to restart to get location now");
		
		WebMyth.startCommunication(sceneController);
		
		if(wasRetry){
			//To keep use from endless loop
			Mojo.Log.error("Was retry - giving up");
			Mojo.Controller.getAppController().showBanner("Failed to start channel play", {source: 'notification'});
		} else {
			Mojo.Log.error("Was not retry - retrying playServiceChannel");
			setTimeout(function() { WebMyth.playServiceChannel(sceneController, value, true); }, 1500);
		}
		
	} else {
				
			if(WebMyth.frontendLocation.search("LiveTV") == -1){
				//Not on liveTV
				Mojo.Log.info("Not on livetv, jumping now - "+WebMyth.frontendLocation);
				sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
					  method:"send",
					  parameters:{"toSend":"jump livetv"},
					  onSuccess: function(response2) {
							Mojo.Log.info("Success jump livetv of %j", response2);
							//WebMyth.sendServiceCommand(sceneController, "play chanid+"+value);
							
							WebMyth.frontendRetries = 0;
							
							//Wait to send channel to let livetv setup
							setTimeout(function() { 
								sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
									  method:"send",
									  parameters:{"toSend":"play chanid "+value},
									  onSuccess: function(response2) {
											Mojo.Log.info("Success play channid of %j", response2);
					
											//Start trying to keep alive
											WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 5000);
											
									  }.bind(this),
									  onFailure: function(response2) {
											Mojo.Log.info("Failed play channid %j", response2);
									  }.bind(this),
								});
								
							}, 3000);				
								
								
					 }.bind(this),
					 onFailure: function(response2) {
							Mojo.Log.info("Failed jump livetv %j", response2);
					  }.bind(this),
				});
			} else {
				//On livetv
				Mojo.Log.info("On livetv, changing channel");
				//WebMyth.sendServiceCommand(sceneController, "play chanid+"+value);
					
				WebMyth.frontendRetries = 0;
					
					
				sceneController.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
					  method:"send",
					  parameters:{"toSend":"play chanid "+value},
					  onSuccess: function(response2) {
							Mojo.Log.info("Success play channid of %j", response2);
				
							//Start trying to keep alive
							WebMyth.telnetTimeout = setTimeout(function() { WebMyth.keepTelnetAlive(sceneController); }, 5000);
								
					  }.bind(this),
					  onFailure: function(response2) {
							Mojo.Log.info("Failed play channid %j", response2);
					  }.bind(this),
				});
					
					
			}

	}
	
};



//upcoming


				this.controller.serviceRequest('palm://com.thewbman.webmyth.service', {
			method:"mythprotocolCommand",
				parameters:{
					"port":"6543", 
					"address":WebMyth.prefsCookieObject.masterBackendIp,
					"protocolVersion": WebMyth.prefsCookieObject.protoVer,
					"command": "QUERY_GETALLPENDING",
					"timeout": 8000								//gives service 8 full seconds to get all data, will return earlier if done
					},
				onSuccess: this.readUpcomingServiceSuccess.bind(this),
				onFailure: this.remoteDbTableFail.bind(this)
			});
		
		
		

