import os
from PIL import Image, ImageDraw, ImageFont

def generate_tier_list_logos(names, width=300, height=300, bg_color="#2a2d32", text_color="#ffffff"):
    """
    Generates text-based logo squares for tier lists.
    Default setup: Dark gray background with white text, 1:1 aspect ratio.
    """
    # Create output directory
    output_dir = "tier_list_logos"
    os.makedirs(output_dir, exist_ok=True)
    
    for name in names:
        # Create base canvas image
        img = Image.new("RGB", (width, height), color=bg_color)
        draw = ImageDraw.Draw(img)
        
        # Determine optimal font size based on string length
        # Standard tier list squares look best with thick, readable fonts
        font_size = int(height * 0.22) if len(name) > 5 else int(height * 0.28)
        
        try:
            # Attempts to load a standard sans-serif system font
            font = ImageFont.truetype("arial.ttf", font_size)
        except IOError:
            try:
                font = ImageFont.truetype("DejaVuSans-Bold.ttf", font_size)
            except IOError:
                font = ImageFont.load_default()
        
        # Center the text within the calculated dimensions
        try:
            bbox = draw.textbbox((0, 0), name, font=font)
            text_width = bbox[2] - bbox[0]
            text_height = bbox[3] - bbox[1]
        except AttributeError:
            # Fallback for older versions of Pillow
            text_width, text_height = draw.textsize(name, font=font)

        x = (width - text_width) // 2
        y = (height - text_height) // 2 - (text_height // 6) # Slight optical alignment adjustment
        
        # Draw the brand text onto canvas
        draw.text((x, y), name, fill=text_color, font=font)
        
        # Format the file path and save
        clean_filename = name.lower().replace(" ", "_")
        file_path = os.path.join(output_dir, f"{clean_filename}.png")
        img.save(file_path, "PNG")
        print(f"Generated logo for: {name} -> {file_path}")

if __name__ == "__main__":
    # Your specific list of German cybersecurity / research institutes
    institutes = ["Author"]
    
    # Run generator with a standard 1:1 ratio (perfect for tier lists)
    generate_tier_list_logos(institutes, width=300, height=300)
