# Pygame
import pygame
from pygame.locals import *

# PGU
from pgu import engine

# Mòduls propis
import conf
from pbar import ProgressBar


# Classe joc
class Joc(engine.Game):

    # Initialize screen, pygame modules, clock... and states.
    def __init__(self):
        super().__init__()
        self.screen = pygame.display.set_mode(conf.mides_pantalla, SWSURFACE)
        self.crono = pygame.time.Clock()
        self._init_state_machine()

    # Creates and stores all states as attributes
    def _init_state_machine(self):
        self.jugant = Jugant(self)

    # Calls the main loop with the initial state.
    def run(self): 
        super().run(self.jugant, self.screen)

    # Tick is called once per frame. It shoud control de timing.
    def tick(self):
        self.crono.tick(conf.fps)   # Limits the maximum FPS


# A state may subclass engine.State.
class Jugant(engine.State):

    # The init method should load data, etc.  The __init__ method
    # should do nothing but record the parameters.  If the init method
    # returns a value, it becomes the new state.
    def init(self):
        grup = pygame.sprite.Group() # grup de Sprites
        pb1 = ProgressBar( (conf.amplePant-170, 30), 150, 20,
                           (0, 200, 0), (250, 250, 250) )
        pb1.percent = 20
        grup.add(pb1)
        pb2 = ProgressBar( (conf.amplePant-170, 80), 150, 30,
                           (0, 200, 200), (250, 250, 250) )
        pb2.percent = 75
        grup.add(pb2)
        pb3 = ProgressBar( (conf.amplePant//2, conf.altPant//2),
                           (3*conf.amplePant)//4, conf.altPant//10,
                           (255, 0, 0), (200, 200, 200) )
        grup.add(pb3)
        self.all_sprites = grup
        self.pbc = pb3 # la progressBar controlable és la 3a


    # The paint method is called once.  If you call repaint(), it
    # will be called again.
    def paint(self,screen):
        screen.fill(conf.color_fons)

    ##
    ##Every time an event occurs, event is called.  If the event method
    ##returns a value, it will become the new state.
    ##::
    def event(self, event): 
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                self.pbc.percent = max(0, self.pbc.percent-5)
            elif event.key == K_RIGHT:
                self.pbc.percent = min(100.0, self.pbc.percent+5)

    # Loop is called once a frame.  It should contain all the logic.
    # If the loop method returns a value it will become the new state.
    def loop(self):
        self.all_sprites.update()

    # Update is called once a frame.  It should update the display.
    def update(self,screen):
        # en aquest cas concret, no cal fer screen.fill(conf.color_fons)
        self.all_sprites.draw(screen)
        pygame.display.flip()


# Programa principal
def main():
    game = Joc()
    game.run()


# Crida el programa principal només si s'executa el mòdul:
#
#   python3 main_barra.py
#
# o bé
#
#   python3 -m main_barra
#
# Importa les funcions i les classes, però no executa el programa
# principal si s'importa el mòdul:
#
#   import joc
if __name__ == "__main__":
    main()
