#!/usr/bin/python

import pygame
import math
import sys

from pygame.locals import *

from menu import Menu
#from game import Game
from highscores import Highscores
from options import Options
import game

import util

from locals import *

def main():
    pygame.init()

    util.load_config()

    if len(sys.argv) > 1:
        for arg in sys.argv:
            if arg == "-psy":
                Variables.psychomode = True
            elif arg == "-nm":
                Variables.music = False
            elif arg == "-ns":
                Variables.sound = False

    pygame.mixer.music.load("music/kalma.ogg")
    if Variables.music:
        pygame.mixer.music.play(-1)

    screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

#    pygame.display.set_icon(util.load_image("kuvake"))
    pygame.display.set_caption("Kalma")

    # Fade in would be nice here...

    splash = pygame.image.load("menu/puskutraktori.png")
    screen.blit(splash, screen.get_rect())
    pygame.display.flip()

    # Just waiting for player to press any key

    done = False
    while not done:
        for e in pygame.event.get():
            if e.type is KEYDOWN:
               done = True

    # ...and fade out here.

    joy = None
    if pygame.joystick.get_count() > 0:
        joy = pygame.joystick.Joystick(0)
        joy.init()

    pygame.time.set_timer(NEXTFRAME, 1000 / FPS)

    main_selection = 0

    while True:
        main_selection = Menu(screen, ("Aloita peli", "Ennätykset", "Asetukset", "Lopeta"), main_selection).run()
        if main_selection == 0:
            score = game.run(screen)
            Highscores(screen, score).run()
        elif main_selection == 1:
            # High Scores
            selection = Highscores(screen).run()
        elif main_selection == 2:
            # Options
            selection = Options(screen).run()
        else: #if main_selection == 3:
            # Quit
            return


if __name__ == '__main__':
    main()
