from __future__ import division

from local import *

import draw

SCOREBOARD_X = 0

SB_HEADER = 86
SB_TOP = SB_HEADER + 32

#Where the status bar starts, so we don't overwrite it
SB_STATUSBAR = 420

SB_NORMAL_HEIGHT = 40
SB_INTER_HEIGHT = 16 # interleaved height

SB_MAXCLIENTS_NORMAL = ((SB_STATUSBAR - SB_TOP) / SB_NORMAL_HEIGHT)
SB_MAXCLIENTS_INTER = ((SB_STATUSBAR - SB_TOP) / SB_INTER_HEIGHT - 1)

# Used when interleaved

SB_LEFT_BOTICON_X = (SCOREBOARD_X)
SB_LEFT_HEAD_X = (SCOREBOARD_X+32)
SB_RIGHT_BOTICON_X = (SCOREBOARD_X+64)
SB_RIGHT_HEAD_X= (SCOREBOARD_X+96)
# Normal
SB_BOTICON_X = (SCOREBOARD_X+32)
SB_HEAD_X = (SCOREBOARD_X+64)

SB_SCORELINE_X = 112

SB_RATING_WIDTH = (6 * BIGCHAR_WIDTH) # width 6
SB_SCORE_X = (SB_SCORELINE_X + BIGCHAR_WIDTH) # width 6
SB_RATING_X = (SB_SCORELINE_X + 6 * BIGCHAR_WIDTH) # width 6
SB_PING_X = (SB_SCORELINE_X + 12 * BIGCHAR_WIDTH + 8) # width 5
SB_TIME_X = (SB_SCORELINE_X + 17 * BIGCHAR_WIDTH + 8) # width 5
SB_NAME_X = (SB_SCORELINE_X + 22 * BIGCHAR_WIDTH) # width 15

#menuScoreboard = None

def scoresDown(*args):
	#CG_ScoresDown_f - cg_consolecmds.c
	# TODO: MISSIONPACK
	if cg.data.scoresRequestTime + 2000 < cg.state.time:
		# the scores are more than two seconds out of data,
		# so request new ones
		cg.data.scoresRequestTime = cg.state.time
		# TODO: trap_SendClientCommand( "score" )
		
		# leave the current scores up if they were already
		# displayed, but if this is the first hit, clear them out
		if not cg.data.showScores:
			cg.data.showScores = True
			cg.data.numScores = 0
	else:
		# show the cached contents even if they just pressed if it
		# is within two seconds
		cg.data.showScores = True
	
def scoresUp(*args):
	#CG_ScoresUp_f - cg_consolecmds.c
	if cg.data.showScores:
		cg.data.showScores = False
		cg.data.scoreFadeTime = cg.state.time
    
def drawScoreBoard():
	# TODO implement the "missionpack"-stuff - see cg_draw.c:2138
	drawOldScoreBoard()
		
def drawOldScoreBoard():
	# see cg_scoreboard.c:266
	# don't draw amuthing if the menu or console is up
	if cg.data.paused:
		cg.data.deferredPlayerLoading = 0;
		return False
		
	if cg.state.gameType == GT_SINGLE_PLAYER and cg.data.pps.pmType == PM_INTERMISSION:
		cg.data.deferredPlayerLoading = 0;
		return False
	
	# don't draw scoreboard during death while warmup up
	if cg.state.warmup and not cg.data.showScores:
		return False

	if (cg.data.showScores or cg.data.pps.pmType == PM_DEAD or 
			cg.data.pps.pmType == PM_INTERMISSION):
		fade = 1.0
		fadeColor = COLOR_WHITE
	else:
		fc = draw.getFadeAlpha( cg.data.scoreFadeTime, FADE_TIME )
		fadeColor = Color(fc, fc, fc)
		
		if not fc:
			#next time scoreboard comes up, don't print killer
			cg.data.deferredPlayerLoading = 0
			cg.data.killerName = None
			return False
			
		fade = fc
	# fragged by ... line
	if cg.data.killerName:
		# TODO: what does the c-function va do?
		s = "Fragged by %s" % cg.data.killerName
		w = len( s ) * BIGCHAR_WIDTH
		x = ( SCREEN_WIDTH - w ) / 2
		y = 40
		draw.drawBigString( x, y, s, fade )
	
	# TODO: current rank
	if cg.state.gameType < GT_TEAM:
		if cg.data.pps.persistent[PERS_TEAM] != TEAM_SPECTATOR:
			s = ('%s place with %d' % (getPlaceString(cg.state.snap.ps.persistent[PERS_RANK] + 1),
				cg.state.snap.ps.persistent[PERS_SCORE]))
			w = len( s ) * BIGCHAR_WIDTH
			x = ( SCREEN_WIDTH - w ) / 2
			y = 60
			draw.drawBigString( x, y, s, fade )
		else:
			# TODO: implement cg.teamScores
			pass

	y = SB_HEADER

	draw.drawPic( SB_SCORE_X + (SB_RATING_WIDTH / 2), y, 64, 32, cg.media.scoreboardScore)
	draw.drawPic( SB_PING_X - (SB_RATING_WIDTH / 2), y, 64, 32, cg.media.scoreboardPing )
	draw.drawPic( SB_TIME_X - (SB_RATING_WIDTH / 2), y, 64, 32, cg.media.scoreboardTime )
	draw.drawPic( SB_NAME_X - (SB_RATING_WIDTH / 2), y, 64, 32, cg.media.scoreboardName )

	y = SB_TOP

	# If there are more than SB_MAXCLIENTS_NORMAL, use the interleaved scores
	if cg.data.numScores > SB_MAXCLIENTS_NORMAL:
		maxClients = SB_MAXCLIENTS_INTER
		lineHeight = SB_INTER_HEIGHT
		topBorderSize = 8
		bottomBorderSize = 16
	else:
		maxClients = SB_MAXCLIENTS_NORMAL
		lineHeight = SB_NORMAL_HEIGHT
		topBorderSize = 16
		bottomBorderSize = 16

	localClient = False

	if cg.state.gameType >= GT_TEAM:
		#
		# teamplay scoreboard
		#
		y + lineHeight/2
		# TODO: implement teamScores
		#
		# free for all scoreboard
		#
	else:
		n1 = teamScoreboard( y, TEAM_FREE, fade, maxClients, lineHeight )
		y += (n1 * lineHeight) + BIGCHAR_HEIGHT
		n2 = teamScoreboard( y, TEAM_SPECTATOR, fade, maxClients - n1, lineHeight )
		y += (n2 * lineHeight) + BIGCHAR_HEIGHT

def teamScoreboard(y, team, fade, maxClients, lineHeight):
	color = Color(1.0, 1.0, 1.0, fade)
	return maxClients
