import pygame, sys
from pygame.locals import *
 
pygame.init()

FPS = 60
FramePerSec = pygame.time.Clock()

WHITE = (255, 255, 255)

# --------------------------------------------------------------------------------------------
# OTHER VARS
# --------------------------------------------------------------------------------------------

# Screen information
RES = WIDTH, HEIGHT = 1100, 619

# Game related
input_up_down_accepted : bool = True

# dino specific
cactus_type : int = 0

# general
check_music_update = False

# Velocities
MOVE_UP_SPEED : int = 20

# setting up backdrops
background_subway = pygame.image.load("floor_top_down.png")
pause_screen = pygame.image.load("pause_screen_nobg.png")
    
# --------------------------------------------------------------------------------------------
# BASIC GAME SETUP
# --------------------------------------------------------------------------------------------

DISPLAYSURF = pygame.display.set_mode(RES)
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("El Mexicano Y Su Bicicleta")

def load_image(name):
    image = pygame.image.load(name)
    return image

# --------------------------------------------------------------------------------------------
# CLASSES: PLAYER
# --------------------------------------------------------------------------------------------

# states and what not
MOVE_UP : bool = False
MOVE_DOWN : bool = False
current_lane : str = "middle"

# Animation related
current_frame : int = 0
animation_time : int = 0
clock = pygame.time.Clock()


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.side_bike = []
        self.side_bike.append(load_image("Player1.png"))
        self.side_bike.append(load_image("Player2.png"))
        self.side_bike.append(load_image("Player3.png"))
        self.side_bike.append(load_image("Player4.png"))
        self.top_bike = []
        self.top_bike.append(load_image("Player_top_down.png"))
        self.counter = 0
        self.score_countdown = 0

        self.index = 0
        self.image = self.side_bike[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = (160, 450)

    def handle_up_down(self, MOVE_SPEED : int):
        global MOVE_UP
        global MOVE_DOWN
        global current_lane
        global input_up_down_accepted
        global stop_game

        if MOVE_UP == True:
            if current_lane != "upper":
                if current_lane == "middle":
                    if self.rect.center > (160, 300):
                        self.rect.move_ip(0, -MOVE_SPEED)
                    if self.rect.center <= (160, 301):
                        self.rect.center = (160, 300)
                        input_up_down_accepted = True
                        current_lane = "upper"
                        MOVE_UP = False
                
                if current_lane == "lower":
                    if self.rect.center > (160, 450):
                        self.rect.move_ip(0, -MOVE_SPEED)
                    if self.rect.center <= (160, 451):
                        self.rect.center = (160, 450)
                        input_up_down_accepted = True
                        current_lane = "middle"
                        MOVE_UP = False
        
        if MOVE_DOWN == True:
            if current_lane != "lower":
                if current_lane == "upper":
                    if self.rect.center < (160, 450):
                        self.rect.move_ip(0, MOVE_SPEED)
                    if self.rect.center >= (160, 449):
                        self.rect.center = (160, 450)
                        input_up_down_accepted = True
                        current_lane = "middle"
                        MOVE_DOWN = False

                if current_lane == "middle":
                    if self.rect.center < (160, 600):
                        self.rect.move_ip(0, MOVE_SPEED)
                    if self.rect.center >= (160, 599):
                        self.rect.center = (160, 600)
                        input_up_down_accepted = True
                        current_lane = "lower"
                        MOVE_DOWN = False

    def handle_animation(self, ANIMATION_NAME : str):
        if ANIMATION_NAME == "SIDE_VIEW":
            self.counter += 1
            if self.counter >= 4:
                self.counter = 0
                self.index += 1
            if self.index >= len(self.side_bike):
                self.index = 0
            self.image = self.side_bike[self.index]
        if ANIMATION_NAME == "TOP_DOWN":
            self.image = self.top_bike[0]

P1 = Player()

sprites = pygame.sprite.Group()
sprites.add(P1)

while True:
    pressed_keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if input_up_down_accepted == True:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    print("up")                 # SPAGHETTI WHEEEEEEEEE
                    MOVE_UP = True
                    input_up_down_accepted = False
                if event.key == pygame.K_s:
                    print("down")               # I LOVE DI PASTA
                    MOVE_DOWN = True
                    input_up_down_accepted = True

        if event.type == QUIT:
            pygame.quit()
            sys.exit()


    DISPLAYSURF.blit(background_subway, (0,0))
    for entity in sprites:
            DISPLAYSURF.blit(entity.image, entity.rect)
    P1.handle_up_down(10)
    P1.handle_animation("TOP_DOWN")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

    pygame.display.update()
    FramePerSec.tick(FPS)