"""
highscore.py - keep track of highscores
"""

import common
import pygame,pygame.font
from pygame.locals import *

score = []

def init():
    global score
    try:
        fp = open("highscore.dat","r")
        # read out highscore
        all = fp.readlines()
        for x in all:
            name,high = x.split(":")
            high = int(high)
            score.append([name,high])

        fp.close()

    except IOError:
        score = [ ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ,
                  ['Nobody',0] ]

def save():
    try:
        fp = open('highscore.dat','w')

        for x in score:
            fp.write("%s:%d\n"%(x[0],x[1]))

        fp.close()

    except IOError:
        print "Couldn't save highscore.dat !"

def check(points):
    if points >= score[9][1]:
        newHighscore(points)

def newHighscore(points):
    global score
    name = getName()
    for x in range(10):
        if points >= score[x][1]:
            score = score[:x]+[[name,points]]+score[x:-1]
            break

    save()

def show():

    common.screen.blit(common.mainscreen,(0,0))
    
    fnt = pygame.font.Font('bluebold.ttf',64)
    tmp = fnt.render("HIGHSCORE",1,(0,0,255),(0,0,0))
    tmp.set_colorkey((0,0,0))
    r = tmp.get_rect()
    res = common.resolution
    common.screen.blit(tmp,(res[0]/2-r[2]/2,25))

    fnt = pygame.font.Font('bluebold.ttf',32)

    for x in range(10):
        str = "%s - %4d"%(score[x][0].ljust(10),score[x][1])
        tmp = fnt.render(str,1,(0,255,0),(0,0,0))
        tmp.set_colorkey((0,0,0))
        tmp.set_alpha(255-x*27)
        r = tmp.get_rect()
        common.screen.blit(tmp,(res[0]/2-r[2]/2,75+(res[1]-100)/10*x))

    pygame.display.update()

    while 1:
        event = pygame.event.poll()
        if event.type == pygame.KEYDOWN:
            break
        pygame.time.delay(100)

def getName():
    common.screen.blit(common.current_bg,(0,0))

    fnt = pygame.font.Font('bluebold.ttf',32)
    tmp = fnt.render("New Highscore",1,(0,255,0),(0,0,0))
    tmp.set_colorkey((0,0,0))
    r   = tmp.get_rect()
    res = common.resolution
    common.screen.blit(tmp,(res[0]/2-r[2]/2,25))
    
    tmp = fnt.render("Enter your name:",1,(0,255,0),(0,0,0))
    tmp.set_colorkey((0,0,0))
    r = tmp.get_rect()
    common.screen.blit(tmp,(res[0]/2-r[2]/2,60))

    name = "__________"
    tmp = fnt.render(name,1,(0,255,0),(0,0,0))
    tmp.set_colorkey((0,0,0))
    common.screen.blit(tmp,(res[0]/2-r[2]/2,100))
    name = ""

    pygame.display.update()

    name_set = 0
    while not name_set:
        for x in pygame.event.get([KEYDOWN]):
            if x.key in [K_a,K_b,K_c,K_d,K_e,K_f,
                         K_g,K_h,K_i,K_j,K_k,K_l,
                         K_m,K_n,K_o,K_p,K_q,K_r,
                         K_s,K_t,K_u,K_v,K_w,K_x,
                         K_y,K_z]:
                letter = x.unicode.upper()
                common.screen.blit(common.current_bg,
                                   (0,100),(0,100,res[0],140))
                name += letter
                if len(name) >= 10:
                    name = name[:10]
                tmp = fnt.render( name.rjust(10).replace(' ','_'),1,(0,255,0),(0,0,0))
                tmp.set_colorkey((0,0,0))
                r = tmp.get_rect()
                common.screen.blit(tmp,(res[0]/2-r[2]/2,100))
                pygame.display.update()
                
            elif x.key in [K_ESCAPE,K_RETURN]:
                if len(name) == 0:
                    name = "Nobody"
                return name
            elif x.key == K_BACKSPACE:
                name = name[:len(name)-1]
                tmp = fnt.render( name.rjust(10).replace(' ','_'),1,(0,255,0),(0,0,0))
                tmp.set_colorkey((0,0,0))
                r = tmp.get_rect()
                common.screen.blit(common.current_bg,
                                   (0,100),(0,100,res[0],140))
                common.screen.blit(tmp,(res[0]/2-r[2]/2,100))
                pygame.display.update()
        
    return "Nobody"
