"""
startup.py - Main startup screen
"""

import common
import os,sys
import pygame,pygame.image
import levels,sounds,gfx,resources,FpsClock,player,main,highscore

buttons = { 'Start':
            {'Active': None,
             'Inactive': None },
            'Highscore':
            {'Active': None,
             'Inactive': None }
            }

menu_state = 0

def run():
    pygame.init()

    sdl_flags = 0
    if '-fullscreen' in sys.argv:
        sdl_flags |= pygame.FULLSCREEN
    if '-speed' in sys.argv:
        common.speed = 1
    else: common.speed = 0

    common.diff = 1
    if '-diff' in sys.argv:
        diff = sys.argv.index('-diff')+1
        if diff < len(sys.argv):
            diff = int(sys.argv[diff])
            if diff in [0,1,2]:
                common.diff = diff
    if '-nosound' in sys.argv:
        common.nosound = 1
    else: common.nosound = 0

    pygame.display.set_caption('YaSS')
        
    if pygame.display.mode_ok(common.resolution, sdl_flags, 16):
        common.screen = pygame.display.set_mode(common.resolution, sdl_flags, 16)
        print "GFX Info:",pygame.display.Info()
        print "Driver:",pygame.display.get_driver()
    else:
        print "Lacking support for 16bit mode: ",resolution,sdl_flags
        sys.exit()

    load_all()

def load_all():
    global buttons
    #
    # Initialize everything now
    #
    pygame.mouse.set_visible(0)
    levels.init()    
    highscore.init()
    common.mainscreen = pygame.image.load("images%smainscreen.jpg"%os.sep).convert()
    common.screen.blit(common.mainscreen,(0,0))
    loading_font = pygame.font.Font('bluebold.ttf', 64)
    loading_surface = loading_font.render("Loading..",1,(0,255,0),(0,0,0)).convert()
    loading_surface.set_colorkey((0,0,0))
    res = common.resolution
    r = loading_surface.get_rect()
    common.screen.blit(loading_surface,(res[0]/2-r[2]/2,res[1]/2-r[3]/2))
    pygame.display.update()
    while pygame.event.poll().type != pygame.NOEVENT: pass
    common.sound = sounds.Sound()
    common.key_state = pygame.key.get_pressed()
    gfx.loadBackgrounds()    
    resources.loadResources()
    common.clock = FpsClock.FpsClock(40,1)

    # load buttons
    buttons['Start']['Inactive']     = pygame.image.load("images%sbut_start_1.gif"%os.sep).convert()
    buttons['Start']['Active']       = pygame.image.load("images%sbut_start_2.gif"%os.sep).convert()
    buttons['Highscore']['Inactive'] = pygame.image.load("images%sbut_high_1.gif"%os.sep).convert()
    buttons['Highscore']['Active']   = pygame.image.load("images%sbut_high_2.gif"%os.sep).convert()
    
    for x in buttons.keys():
        for y in buttons[x].keys():
            buttons[x][y].set_colorkey((0,0,0))

    start_screen()

def show_buttons():
    res = common.resolution
    r = buttons['Start']['Active'].get_rect()
    if menu_state == 0:
        common.screen.blit(buttons['Start']['Active'],(res[0]/2-r[2]/2, 150))
    else:
        common.screen.blit(buttons['Start']['Inactive'],(res[0]/2-r[2]/2, 150))
    if menu_state == 1:
        common.screen.blit(buttons['Highscore']['Active'],(res[0]/2-r[2]/2, 250))
    else:
        common.screen.blit(buttons['Highscore']['Inactive'],(res[0]/2-r[2]/2,250))
    pygame.display.update()

def start_screen():
    global menu_state
    common.game_finished = 0
    common.screen.blit(common.mainscreen,(0,0))
    res = common.resolution
    tmp = pygame.image.load("images%syass.jpg"%os.sep).convert()
    #tmp.set_colorkey((0,0,0))
    r = tmp.get_rect()
    common.screen.blit(tmp, (res[0]/2-r[2]/2,50))

    show_buttons()

    while not common.game_finished:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:            
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == pygame.K_DOWN:
                if menu_state == 0: menu_state = 1
                elif menu_state == 1: menu_state = 0
                show_buttons()
                common.sound.menu()
            elif event.key == pygame.K_UP:
                if menu_state == 0: menu_state = 1
                elif menu_state == 1: menu_state = 0
                show_buttons()
                common.sound.menu()
            elif event.key == pygame.K_RETURN or \
                 event.key == pygame.K_SPACE:
                if menu_state == 0:
                    levels.level = 0
                    common.player = player.Player([300,common.resolution[1]-40,30,40])
                    main.start()
                elif menu_state == 1:
                    highscore.show()
                    break

    if common.game_finished:
        highscore.check(common.player.points)
    start_screen()
        
if __name__ == '__main__': run()
