import os
from pathlib import Path
from PIL import Image, ImageDraw

# Ermittle Icon-Verzeichnis aus Umgebungsvariable oder relativ zum Projekt
PROJECT_DOC = os.environ.get('PROJECT_DOC')
if PROJECT_DOC:
    ICON_DIR = Path(PROJECT_DOC) / "img" / "Icons"
else:
    # Fallback: relativ zum lib-Verzeichnis
    ICON_DIR = Path(__file__).parent.parent / "doc" / "img" / "Icons"

# Load base icon
base = Image.open(ICON_DIR / "dxfCs.png").convert("RGBA")

def add_flag(flag_draw_fn, name):
    img = base.copy()
    draw = ImageDraw.Draw(img)
    # flag area (top-left)
    x0, y0, x1, y1 = 243, 170, 512, 360

    flag_draw_fn(draw, x0, y0, x1, y1)
    path = ICON_DIR / f"{name}.png"
    img.save(path)
    print(f"Flagge erstellt: {path}")
    return str(path)

# Define flags
def flag_en(draw, x0, y0, x1, y1):
    """Union Jack (UK-Flagge)"""
    # Blauer Hintergrund
    draw.rectangle([x0, y0, x1, y1], fill="#012169")

    cx = (x0 + x1) / 2
    cy = (y0 + y1) / 2
    w = x1 - x0
    h = y1 - y0

    # Weiße Diagonalen (Schottland)
    white_diag_width = 40
    # Diagonale von links-oben nach rechts-unten
    draw.polygon([
        x0, y0,
        x0 + white_diag_width, y0,
        x1, y1 - white_diag_width,
        x1, y1,
        x1 - white_diag_width, y1,
        x0, y0 + white_diag_width
    ], fill="white")
    # Diagonale von rechts-oben nach links-unten
    draw.polygon([
        x1, y0,
        x1 - white_diag_width, y0,
        x0, y1 - white_diag_width,
        x0, y1,
        x0 + white_diag_width, y1,
        x1, y0 + white_diag_width
    ], fill="white")

    # Rote Diagonalen (Irland)
    red_diag_width = 20
    # Diagonale von links-oben nach rechts-unten
    draw.polygon([
        x0, y0,
        x0 + red_diag_width, y0,
        x1, y1 - red_diag_width,
        x1, y1,
        x1 - red_diag_width, y1,
        x0, y0 + red_diag_width
    ], fill="#C8102E")
    # Diagonale von rechts-oben nach links-unten
    draw.polygon([
        x1, y0,
        x1 - red_diag_width, y0,
        x0, y1 - red_diag_width,
        x0, y1,
        x0 + red_diag_width, y1,
        x1, y0 + red_diag_width
    ], fill="#C8102E")

    # Weißes Kreuz (England - breiter)
    white_cross_width = 40
    draw.rectangle([cx - white_cross_width/2, y0, cx + white_cross_width/2, y1], fill="white")
    draw.rectangle([x0, cy - white_cross_width/2, x1, cy + white_cross_width/2], fill="white")

    # Rotes Kreuz (England - schmaler)
    red_cross_width = 24
    draw.rectangle([cx - red_cross_width/2, y0, cx + red_cross_width/2, y1], fill="#C8102E")
    draw.rectangle([x0, cy - red_cross_width/2, x1, cy + red_cross_width/2], fill="#C8102E")

def flag_fr(draw, x0, y0, x1, y1):
    w = (x1-x0)//3
    draw.rectangle([x0, y0, x0+w, y1], fill="#CE1126")
    draw.rectangle([x0+w, y0, x0+2*w, y1], fill="#ffe")
    draw.rectangle([x0+2*w, y0, x1, y1], fill="#002654")

def flag_it(draw, x0, y0, x1, y1):
    w = (x1-x0)//3
    draw.rectangle([x0, y0, x0+w, y1], fill="#009246")
    draw.rectangle([x0+w, y0, x0+2*w, y1], fill="#ffe")
    draw.rectangle([x0+2*w, y0, x1, y1], fill="#ce2b37")

def flag_es(draw, x0, y0, x1, y1):
    h = (y1-y0)
    draw.rectangle([x0, y0, x1, y0+h*0.25], fill="#ad1519")
    draw.rectangle([x0, y0+h*0.25, x1, y0+h*0.75], fill="#fabd00")
    draw.rectangle([x0, y0+h*0.75, x1, y1], fill="#ad1519")

if __name__ == "__main__":
    # Generiere alle Flaggen-Icons
    print(f"\nGeneriere Flaggen-Icons in {ICON_DIR}\n")

    icons = []
    icons.append(add_flag(flag_en, "dxfEN"))
    icons.append(add_flag(flag_fr, "dxfFR"))
    icons.append(add_flag(flag_it, "dxfIT"))
    icons.append(add_flag(flag_es, "dxfES"))

    print(f"\nFertig! {len(icons)} Icons wurden erstellt.")
