import pygame

from pygame.locals import *

from locals import *
import util

class Options:
    #logo = None
    bg = None

    PSYCHOMODE = 0
    SOUND = 1
    MUSIC = 2
    NAME = 3

    def __init__(self, screen):
        self.screen = screen

        self.title = util.bigfont.render("Asetukset", True, COLOR1)        

        if not Options.bg:
            Options.bg = pygame.image.load("menu/zombie-bg.jpg")

        self.menu = ()
        self.refresh() # sets self.menu
        self.selection = 0
        self.t = 0

    def refresh(self):
        self.menu = ("Psykedeliatila: " + (Variables.psychomode and "on" or "off"),
        	     "Ääniefektit: " + (Variables.sound and "on" or "off"),
                     "Musiikki: " + (Variables.music and "on" or "off"),
                     "Pelaajan nimi: " + Variables.name)

    def run(self):
        done = False

        while not done:
            self.screen.blit(Options.bg, self.screen.get_rect())

            rect = self.title.get_rect()
            rect.centerx = MENUCENTER
            rect.top = MENUTOP

            #self.screen.blit(self.title, rect)
            
            for i in xrange(len(self.menu)):
                self.render(i)

            pygame.display.flip()

            self.t += 1

            nextframe = False
            while not nextframe:
                pygame.event.post(pygame.event.wait())
                for event in pygame.event.get():
                    if event.type == QUIT or \
                        event.type == KEYDOWN and event.key == K_ESCAPE:
                        self.selection = -1
                        done = True
                        nextframe = True
                    elif event.type == NEXTFRAME:
                        nextframe = True
                    elif event.type == JOYAXISMOTION:
                        if event.axis == 1:
                            if event.value < -0.5:
                                self.move_up()
                            if event.value > 0.5:
                                self.move_down()
                        elif event.axis == 0:
                            if event.value < -0.5:
                                self.change_left()
                            if event.value > 0.5:
                                self.change_right()
                    elif event.type == JOYBUTTONDOWN:
                        if event.button == 0:
                            #done = True
                            self.change_right()
                        elif event.button == 1:
                            done = True
                    elif event.type == KEYDOWN:
                        if event.key == K_UP:
                            self.move_up()
                        elif event.key == K_DOWN:
                            self.move_down()
                        elif event.key == K_LEFT:
                            self.change_left()
                        elif event.key == K_RIGHT:
                            self.change_right()
                        elif self.selection == Options.NAME:
                            if event.key == K_BACKSPACE:
                                if len(Variables.name) != 0:
                                    Variables.name = Variables.name[:-1]
                            elif event.key == K_SPACE or event.unicode != " ":
                                if len(Variables.name) < 32:
                                    Variables.name += event.unicode
                            self.refresh()
                        elif event.key == K_SPACE or event.key == K_RETURN:
                            #done = True
                            self.change_right()

        util.save_config()
        return self.selection

    def change_left(self):
        if self.selection == Options.PSYCHOMODE:
            Variables.psychomode = not Variables.psychomode
        elif self.selection == Options.SOUND:
            Variables.sound = not Variables.sound
        elif self.selection == Options.MUSIC:
            Variables.music = not Variables.music
            if Variables.music:
                pygame.mixer.music.play(-1)
            else:
                pygame.mixer.music.stop()
        self.refresh()

    def change_right(self):
        # They're all bools right now, so...
        self.change_left()

    def move_down(self):
        self.selection += 1
        if self.selection >= len(self.menu):
            self.selection = len(self.menu) - 1

    def move_up(self):
        self.selection -= 1
        if self.selection < 0:
            self.selection = 0

    def render(self, id):
        color = COLOR1
        if self.selection == id:
            color = COLOR2

        title = self.menu[id]
        image = util.bigfont.render(title, 1, color)
        rect = image.get_rect()
        rect.centerx = MENUCENTER
        rect.top = MENUTOP + 50 + id * rect.height * 1.1

        self.screen.blit(image, rect)
