import xml.etree.ElementTree as ET

class TileManager:
    def __init__(self, path, surface, texture, cell_size):
        self.tilesheet_texture = texture
        self.tile_positions = {}
        self.tilesheet_surface = surface
        self.cell_size = cell_size
        self.load_tilemap(path)
        
    def load_tilemap(self, xml_path):
        tree = ET.parse(xml_path)
        root = tree.getroot()
        
        for tile in root.findall('SubTexture'):
            name = tile.get('name').replace('.png', '')
            x = int(tile.get('x'))
            y = int(tile.get('y'))
            width = int(tile.get('width'))
            height = int(tile.get('height'))
            
            self.tile_positions[name] = (x, y, width, height)
            
    def get_tile_rect(self, tile_name):
        if tile_name in self.tile_positions:
            return self.tile_positions[tile_name]
        return None
    
    def get_tile_names(self):
        return self.tile_positions.keys()
    
    def get_tilesheet_surface(self):
        return self.tilesheet_surface
    
    def get_tilesheet_texture(self):
        return self.tilesheet_texture