import pygame
import core.image_loader as image_loader

class TileMap:
    def __init__(self, _map, tile_size, solid_tiles):
        self.map = _map
        self.see_tiles = []
        self.height = len(_map)
        self.width = len(_map[0])
        self.tile_size = tile_size
        self.solid_tiles = solid_tiles
        self.tile_images = image_loader.tile_images
        for image in self.tile_images.keys():
            self.tile_images[image] = pygame.transform.scale(self.tile_images[image], (self.tile_size, self.tile_size))

    def update_see_tiles(self, screen_size, cx, cy):
        self.see_tiles = []
        for y in range(len(self.map)):
            line = []
            for x in range(len(self.map[y])):
                if not (x*self.tile_size-cx > screen_size[0] or y*self.tile_size-cy > screen_size[1]):
                    line.append(self.map[y][x])
            if line:
                self.see_tiles.append(line)

        if self.see_tiles:
            self.height = len(self.see_tiles)
            self.width = len(self.see_tiles[0])
        else:
            self.height = 0
            self.width = 0

    def draw(self, screen, cx, cy):
        for y in range(self.height):
            for x in range(self.width):
                if self.see_tiles[y][x] != "air":
                    screen.blit(self.tile_images[self.see_tiles[y][x]], (x*self.tile_size-cx, y*self.tile_size-cy))

    def place_block(self, x, y, block_type):
        self.map[y][x] = block_type