import csv
import random
import json
import sys

use_json = "-json" in sys.argv

try:
    with open("oesterreich.csv", "r", newline="", encoding="latin-1") as f:
        reader = csv.reader(f, delimiter=";")
        rows = list(reader)[1:]  # Header entfernen

    topo = []

    for row in rows:
        if row[0]:   # Gebirge
            topo.append(row[0])
        if row[2]:   # Landschaften
            topo.append(row[2])

    topo = list(set(topo))  # Duplikate entfernen

    result = []
    for i in range(17):
        result.append(random.sample(topo, 6))

    if use_json:
        with open("topo.json", "w", encoding="utf-8") as f:
            json.dump(result, f, ensure_ascii=False, indent=2)
    else:
        with open("topo.txt", "w", encoding="utf-8") as f:
            for row in result:
                f.write(", ".join(row) + "\n")

except Exception as e:
    print("Exception:", e)
