import pygame
import sys
from scripts.entities import *
from scripts.utils import *
from scripts.tilemap import *
from scripts.clouds import Clouds

class Game:
    def __init__(self):
        pygame.init()


        pygame.display.set_caption("Jump and run spiel")
        self.screen = pygame.display.set_mode((640, 480))
        self.display = pygame.Surface((320, 240))



        self.clock = pygame.time.Clock()
        self.running = True

        self.player = PhysicsEntity(self, 'player', (50, 50), (8, 15))

        self.collision_area = pygame.Rect(50, 50, 300, 50)
        self.movement = [False, False]
        
        self.assets = {
            "player":load_image("entities/player.png"),
            "background":load_image("background.png"),
            "decor": load_images("tiles/decor"),
            "grass": load_images("tiles/grass"),
            "large_decor": load_images("tiles/large_decor"),
            "stone": load_images("tiles/stone"),
            "clouds": load_images("clouds")
            }
        self.tilemap = Tilemap(self, 16)
        self.clouds = Clouds(self.assets['clouds'], count=16)
        self.isJumping = False
        self.scroll = [0, 0]


    def run(self):
        while self.running:

            self.display.blit(self.assets['background'], (0,0))
            
            self.scroll[0] += (self.player.rect().centerx - self.display.get_width() / 2 - self.scroll[0]) / 30
            self.scroll[1] += (self.player.rect().centery - self.display.get_height() / 2 - self.scroll[1]) / 30
            render_scroll = (int(self.scroll[0]), int(self.scroll[1]))


            self.clouds.update()
            self.clouds.render(self.display, offset=render_scroll)
            self.tilemap.render(self.display, offset=render_scroll)
            self.player.update(self.tilemap, ((self.movement[1] - self.movement[0]), 0))
            self.player.render(self.display, offset=render_scroll)
            # print(int(self.player.pos[0]))
            # print(int(self.player.pos[1]))
            # print(self.tilemap.tiles_around(self.player.pos))


            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.running = False
                    if event.key == pygame.K_a:
                        self.movement[0] = True
                    if event.key == pygame.K_d:
                        self.movement[1] = True
                    if event.key == pygame.K_SPACE: # and not self.isJumping
                        self.player.velocity[1] -= 3
                        self.isJumping = True
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_a:
                        self.movement[0] = False
                    if event.key == pygame.K_d:
                        self.movement[1] = False


            self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0,0))
            pygame.display.update()
            self.clock.tick(60)


        pygame.quit()
        sys.exit()


game = Game()
game.run()
