import os
import subprocess
import re

# Paths
site_logo_path = "../site/assets/img/logo.svg"
site_favicon_path = "../site/assets/img/favicon.svg"
output_dir = "./extracted-icons"

# Read logo svg content
with open(site_logo_path, "r") as f:
    logo_svg = f.read()

# Read favicon svg content
with open(site_favicon_path, "r") as f:
    favicon_svg = f.read()

# Helper to run convert command
def convert_svg_to_png(svg_content, out_png_path, size="512x512"):
    tmp_svg = out_png_path + ".tmp.svg"
    with open(tmp_svg, "w") as f:
        f.write(svg_content)
    try:
        # Use ImageMagick 'convert' with transparent background handling
        cmd = ["convert", "-background", "none", tmp_svg, "-resize", size, out_png_path]
        subprocess.run(cmd, check=True)
        print(f"Successfully generated: {out_png_path}")
    except Exception as e:
        print(f"Failed to generate {out_png_path}: {e}")
    finally:
        if os.path.exists(tmp_svg):
            os.remove(tmp_svg)

print("Starting generation of icon variations...")

# Generate Variation 1: Rounded Logo (Original design)
convert_svg_to_png(logo_svg, os.path.join(output_dir, "play_store_icon_rounded.png"))

# Generate Variation 2: Square Logo (Recommended for Google Play listing to avoid double-masking)
square_logo_svg = logo_svg.replace('rx="72"', 'rx="0"')
convert_svg_to_png(square_logo_svg, os.path.join(output_dir, "play_store_icon_square.png"))

# Generate Variation 3: Transparent Logo (Foreground graphic only)
transparent_logo_svg = re.sub(r'<rect[^>]*fill="#012459"[^>]*/>', '', logo_svg)
convert_svg_to_png(transparent_logo_svg, os.path.join(output_dir, "play_store_icon_transparent.png"))

# Generate Variation 4: Rounded Favicon 'Y' Logo
convert_svg_to_png(favicon_svg, os.path.join(output_dir, "favicon_logo_rounded.png"))

# Generate Variation 5: Square Favicon 'Y' Logo
square_favicon_svg = favicon_svg.replace('rx="14"', 'rx="0"')
convert_svg_to_png(square_favicon_svg, os.path.join(output_dir, "favicon_logo_square.png"))

print("All icon variations generated successfully!")
