import pygame
import globals
import ball

#doesnt belong here - will need to move
class Text:
	LetterSize = 32, 26
	
	def __init__(self):
		self.numbers = []
		self.numbers.append(pygame.image.load(globals.String_Numbers))
		
	def DrawNumber(self, num, pos):
		localpos = [num * self.LetterSize[0], 0]
		globals.screen.blit(self.numbers[0], pos, [localpos[0], localpos[1], self.LetterSize[0], self.LetterSize[1]])
	
class Player:

	def __init__(self, table):
		self.table = table
		self.score = 0
		self.oldscore = -1 # forces a score draw
		self.ballsleft = globals.maxballs
		
		self.text = Text()
		
	def Score(self, type):
		pos = [420, 149]
		globals.screen.blit(self.table.tabletop[0], pos, [pos[0], pos[1], 150, 44])        
		
		# Player scoring is done here!!
		if(type == globals.ScoreBumper):
			self.score += 10
		if(type == globals.ScoreElement):
			self.score += 50
		if(type == globals.ScoreSpinner):
			self.score += 100
		if(type == globals.ScoreChute):
			self.score += 1000
		
	def Update(self, ms):
		if(self.oldscore != self.score):
			self.DrawScore()
		self.oldscore = self.score

	# returns true if we have no more balls!! :)
	def Newball(self, ball):
		self.ballsleft -= 1
		self.table.sounds.Play(globals.VoicePolicetheworld, 0)

		if(self.ballsleft < 0):
			return 1
		else:
			self.DrawBalls(ball)
			return 0
		
	def Reset(self):
		pos = [420, 149]
		globals.screen.blit(self.table.tabletop[0], pos, [pos[0], pos[1], 150, 44])        

		self.score = 0
		self.oldscore = -1 # forces a score draw
		self.ballsleft = globals.maxballs
		
	def DrawScore(self):
		temp = self.score
		divisor = 1000000
		pos = [431, 154]
		for i in range(0, 7):
			val = temp / divisor
			self.text.DrawNumber(val, pos)    
			pos = [pos[0] + 19, pos[1]]
			temp -= int(val) * divisor
			divisor = divisor / 10
		
		
	def DrawBalls(self, ball):
		# copy in the tabletop first
		pos = [420, 223]
		globals.screen.blit(self.table.tabletop[0], pos, [pos[0], pos[1], 150, 44])        
		
		# update balls onscreen
		for i in range(0, self.ballsleft):
			ball.DrawBall( [i * 26 + 434, 234] )

