def __init__(self, view, predictor, workQueue, notifier):
		self.font = pygame.font.Font(None, 36)
		self.predictor = predictor
		self.workQueue = workQueue
		self.notifier = notifier
		self.view = view
		self.protocol = ShortServiceProtocol(self.view, self.notifier, self)
class ShortServiceController (ATTController):

	ID = "SHORT_SERVICE"
	
	notifier = None	
	workQueue = None
	predictor = None
	font = None
	view = None
	protocol = None
	servicesList = None
	summary = []
	
	state = 0

	def __init__(self, view, predictor, workQueue, notifier):
		self.font = pygame.font.Font(None, 36)
		self.predictor = predictor
		self.workQueue = workQueue
		self.notifier = notifier
		self.view = view
		self.protocol = ShortServiceProtocol(self.view, self.notifier, self)
		
	def start(self):
		self.servicesList = []
		self.summary = []
	
	def render(self):
		self.view.buildScene()
		self.renderSerialLog()
		self.renderSummary()
		pygame.display.flip()
	
	def renderSerialLog(self):
		
		self.notifier.clearView()
		self.notifier.render()
		
	def renderSummary(self):
		if self.state == 1:
			if len(self.summary) > 0:
				self.view.renderSummary(self.summary)
			
	def process(self, app, event):
		done = False

		if not self.workQueue.empty():
			hit = self.workQueue.get()
			if hit <> "":
				self.processHit(hit)

		self.render()
							
		if app.isPressed(pygame.K_ESCAPE):
			self.clearView()
			if self.state == 0:
				self.buildSummary()
				self.state = 1
				app.pressed = []
				app.myThread.pause()
				self.protocol.pause()
			else:
				self.state = 0
				app.myThread.unpause()
				self.protocol.unpause()
				app.dispatcher.setCurrentController(MenuController.ID)		
		
		return done
	
	def buildSummary(self):
		total = 0
		done = 0
		for service in self.servicesList:
			total += 1
			if service['second'] and service['second']['tstamp'] != "TIMED_OUT":
				done += 1
				
		if total == 0:
			total = 1
		self.summary.append(""+str(float(done/total)*100)+"% done.      Completed="+str(done)+" from Total="+str(total))
		
	def processHit(self, hit):
		(y,x) = self.predictor.predictHit(hit)
		hit['coords'] = (y,x)
		
		logReading = "("+"{0:.0f}".format(y)+","+"{0:.0f}".format(x)+") - "+hit["raw"]
		print logReading
		self.notifier.push(logReading)
		
		self.protocol.processSate(hit)
		
		self.view.drawHit(x, y, hit["side"])
		#self.view.drawHitWithText(x, y, hit["side"], "")
		
	def addServiceEvent(self, selfserviceEvent):
		self.servicesList.append(selfserviceEvent)