import os
import ezdxf
from ezdxf.math import Vec2

# Hilfsfunktion für Umgebungsvariablen mit Fallback

def check_environment_var(env_str: str, fallback: str) -> str:
    out_path = os.environ.get(env_str)
    if out_path:
        return out_path
    print(f"[WARN] Umgebungsvariable {env_str} ist nicht gesetzt oder leer. Verwende '{fallback}'.")
    return fallback

# Verzeichnisse/Dateien über Umgebungsvariablen oder Fallback
INPUT_DIR = os.path.join(check_environment_var("PROJECT_WORK", "."), "converted_dxfs")
OUTPUT_FILE = os.path.join(check_environment_var("PROJECT_WORK", "."), "block_library.dxf")

doc = ezdxf.new()
msp = doc.modelspace()

def get_bbox(entities):
    min_x, min_y = float('inf'), float('inf')
    max_x, max_y = float('-inf'), float('-inf')
    for e in entities:
        try:
            box = e.bbox()
            if box:
                (x1, y1), (x2, y2) = box.extmin, box.extmax
                min_x, min_y = min(min_x, x1), min(min_y, y1)
                max_x, max_y = max(max_x, x2), max(max_y, y2)
        except Exception:
            continue
    if min_x == float('inf'):
        return None
    return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2)

x_offset = 0

for filename in os.listdir(INPUT_DIR):
    if not filename.lower().endswith(".dxf"):
        continue

    filepath = os.path.join(INPUT_DIR, filename)
    name = os.path.splitext(filename)[0]

    try:
        src_doc = ezdxf.readfile(filepath)
        src_msp = src_doc.modelspace()
        entities = list(src_msp)
    except Exception as e:
        print(f"Fehler beim Lesen von {filename}: {e}")
        continue

    center = get_bbox(entities)
    if center is None:
        print(f"Keine gültige Geometrie in {filename}")
        continue

    if name in doc.blocks:
        doc.blocks.delete_block(name)

    blk = doc.blocks.new(name=name, base_point=center)

    for e in entities:
        try:
            blk.add_entity(e.copy())
        except Exception as err:
            print(f"Fehler in {filename}: {err}")

    # Optional Vorschau
    msp.add_blockref(name, insert=(x_offset, 0))
    x_offset += 200  # Abstand zwischen Blöcken

print(f"Bibliotheks-DXF gespeichert: {OUTPUT_FILE}")
doc.saveas(OUTPUT_FILE)
