import pygame
from gameObject import GameObject
from player import Player
from enemy import Enemy

class Game:

    def __init__(self):
        self.width = 800 #to be properties of game class
        self.height = 800
        self.white_color = (255, 255, 255)

        self.game_window = pygame.display.set_mode((self.width, self.height))

        self.clock = pygame.time.Clock() 

        self.background = GameObject(0, 0, self.width, self.height, 'assets/background.png')
        self.treasure = GameObject(375, 50, 50, 50, 'assets/treasure.png')

        self.level = 1.0
        self.reset_map()


    def reset_map(self):
        self.player = Player(375, 700, 50, 50, 'assets/player.png', 6)

        speed = 3 + (self.level)

        if self.level >= 6.0:
            self.enemies = {
                Enemy(0, 600, 50, 50, 'assets/enemy.png', speed),
                Enemy(400, 400, 50, 50, 'assets/enemy.png', speed),
                Enemy(750, 200, 50, 50, 'assets/enemy.png', speed),
                Enemy(200, 500, 50, 50, 'assets/enemy.png', speed),
                Enemy(600, 300, 50, 50, 'assets/enemy.png', speed),
            }
        elif self.level >= 4.0:
            self.enemies = {
                Enemy(0, 600, 50, 50, 'assets/enemy.png', speed),
                Enemy(750, 400, 50, 50, 'assets/enemy.png', speed),
                Enemy(0, 200, 50, 50, 'assets/enemy.png', speed),
            }
        elif self.level >= 2.0:
            self.enemies = {
                Enemy(0, 600, 50, 50, 'assets/enemy.png', speed),
                Enemy(750, 400, 50, 50, 'assets/enemy.png', speed),
            }
        else:
            self.enemies = {
                Enemy(0, 600, 50, 50, 'assets/enemy.png', speed),
            }    
    
    def draw_objects(self):
        self.game_window.fill(self.white_color)
        self.game_window.blit(self.background.image, (self.background.x, self.background.y)) #(x,y) 0,0 is top left
        self.game_window.blit(self.player.image, (self.player.x, self.player.y))
        self.game_window.blit(self.treasure.image, (self.treasure.x, self.treasure.y))
        
        for enemy in self.enemies:
            self.game_window.blit(enemy.image, (enemy.x, enemy.y))

        pygame.display.update()

    def move_objects(self, player_direction):
        self.player.move(player_direction, self.height)
        for enemy in self.enemies:
            enemy.move(self.width)
        
    def check_if_collided(self):
        for enemy in self.enemies:
            if self.detect_collision(self.player, enemy):
                return True
            
        if self.detect_collision(self.player, self.treasure):
            self.level += 1
            return True
        
        return False

    def detect_collision(self, object_1, object_2):
        if object_1.y > (object_2.y + object_2.height): #1 below 2
            return False
        elif (object_1.y + object_1.height) < object_2.y: #1 above 2
            return False

        if object_1.x > (object_2.x + object_2.width):
            return False
        elif (object_1.x + object_1.width) < object_2.x:
            return False
        
        return True

    def run_game_loop(self):
        player_direction = 0
        
        while True: #runs until break statement
            
            #Handle events
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    return #break out of entire game loop
                elif event.type == pygame.KEYDOWN: #any key is pressed
                    if event.key == pygame.K_UP:
                        player_direction = -1
                    elif event.key == pygame.K_DOWN:
                        player_direction = 1
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_UP or pygame.K_DOWN:
                        player_direction = 0

            #Execute logic
            self.move_objects(player_direction)

            #Update display
            self.draw_objects()

            #Detect Collision
            if self.check_if_collided():
                self.reset_map()

            self.clock.tick(60) #60 times per second