"""Build north-up, byte-aligned airport rasters from a retained public-source packet."""
import argparse
import base64
import csv
import datetime as dt
import hashlib
import json
import math
import re
from pathlib import Path

RADIUS_M = 6371008.8
M_PER_PX = 30
FIELDS = ("KORD", "KPWK", "KDPA", "EGLL")
RUNWAY_ID = re.compile(r"(?:0[1-9]|[12][0-9]|3[0-6])[LCR]?/(?:0[1-9]|[12][0-9]|3[0-6])[LCR]?")


def project(origin, point):
    lon, lat = point
    lon0, lat0 = origin
    x = RADIUS_M * math.cos(math.radians(lat0)) * math.radians(lon - lon0)
    y = RADIUS_M * math.radians(lat - lat0)
    if not math.isfinite(x + y) or math.hypot(x, y) > 15000:
        raise ValueError("surface point outside airport envelope")
    return (round(x, 2), round(y, 2))


def assemble_rings(members):
    """Join exact matching OSM endpoints, including reversed member ways."""
    remaining = [list(part) for part in members]
    rings = []
    while remaining:
        ring = remaining.pop()
        while ring[0] != ring[-1]:
            for i, segment in enumerate(remaining):
                if ring[-1] == segment[0]:
                    ring.extend(segment[1:])
                elif ring[-1] == segment[-1]:
                    ring.extend(reversed(segment[:-1]))
                else:
                    continue
                remaining.pop(i)
                break
            else:
                raise ValueError("incomplete OSM multipolygon")
        if len(ring) < 4:
            raise ValueError("degenerate OSM ring")
        rings.append(ring)
    return rings


def osm_points(way, origin):
    return [project(origin, (p["lon"], p["lat"])) for p in way["geometry"]]


def osm_shapes(data, origin, icao):
    polygons, lines, boundary, runway_ways, thresholds = [], [], [], [], []
    categories = {"apron": 1, "terminal": 1, "taxiway": 2, "taxilane": 2}
    for item in data["elements"]:
        tags = item.get("tags", {})
        kind = tags.get("aeroway")
        if kind == "runway" and item["type"] == "way":
            points = osm_points(item, origin)
            if tags.get("runway") == "displaced_threshold":
                thresholds.append((points[0], points[-1]))
            elif RUNWAY_ID.fullmatch(tags.get("ref", "").replace("-", "/")) and points[0] != points[-1]:
                runway_ways.append((tags, points))
            continue
        if kind not in categories and kind != "aerodrome":
            continue
        if kind == "aerodrome" and tags.get("icao") != icao:
            continue
        if item["type"] == "relation":
            groups = {"inner": [], "outer": []}
            for member in item["members"]:
                if member.get("role") in groups and member["type"] == "way":
                    groups[member["role"]].append(osm_points(member, origin))
            if not groups["outer"]:
                raise ValueError("OSM relation has no exterior")
            rings = assemble_rings(groups["outer"]) + assemble_rings(groups["inner"])
        elif item["type"] == "way":
            points = osm_points(item, origin)
            if points[0] != points[-1] or (kind in ("taxilane", "taxiway") and tags.get("area") != "yes"):
                if kind in ("taxilane", "taxiway"):
                    lines.append((2, points, 0))
                continue
            rings = [points]
        else:
            continue
        if kind == "aerodrome":
            boundary.extend(rings)
        else:
            polygons.append((categories[kind], rings))
    if not boundary:
        raise ValueError(f"{icao} missing OSM aerodrome boundary")
    runways = []
    for tags, way in runway_ways:
        ends = [way[0], way[-1]]
        for idx, point in enumerate(ends):
            adjacent = [b if math.dist(point, a) <= 2 else a for a, b in thresholds
                        if min(math.dist(point, a), math.dist(point, b)) <= 2]
            if len(adjacent) > 1:
                raise ValueError("ambiguous displaced runway threshold")
            if adjacent:
                ends[idx] = adjacent[0]
        runway_id = tags["ref"].replace("-", "/")
        heading = math.radians(int(runway_id[:2]) * 10)
        direction = ((ends[1][0] - ends[0][0]), (ends[1][1] - ends[0][1]))
        if direction[0] * math.sin(heading) + direction[1] * math.cos(heading) < 0:
            ends.reverse()
        runways.append({"id": runway_id, "xy_m": ends, "width_m": float(tags["width"])})
    return polygons, lines, boundary, runways


def read_json(packet, name):
    return json.loads((packet / name).read_text())


def nasr_runways(packet, base, origin):
    ends = {(r["RWY_ID"], r["RWY_END_ID"]): r for r in read_json(packet, "APT_RWY_END-selected.json")
            if r["SITE_NO"] == base["SITE_NO"]}
    runways = []
    for row in read_json(packet, "APT_RWY-selected.json"):
        runway = row["RWY_ID"]
        if row["SITE_NO"] != base["SITE_NO"] or not RUNWAY_ID.fullmatch(runway):
            continue
        points = []
        for end_id in runway.split("/"):
            end = ends[(runway, end_id)]
            if end["EFF_DATE"] != base["EFF_DATE"] or row["EFF_DATE"] != base["EFF_DATE"]:
                raise ValueError("runway source cycle mismatch")
            points.append(project(origin, (float(end["LONG_DECIMAL"]), float(end["LAT_DECIMAL"]))))
        runways.append({"id": runway, "xy_m": points, "width_m": round(float(row["RWY_WIDTH"]) * 0.3048, 2)})
    return runways


def check_osm_runway_ends(packet, icao, origin, runways):
    surveyed = {r["le_ident"] + "/" + r["he_ident"]: r
                for r in read_json(packet, "ourairports-selected-runways.json")
                if r["airport_ident"] == icao and r["closed"] == "0"}
    if set(surveyed) != {r["id"] for r in runways}:
        raise ValueError(f"{icao} runway identifiers disagree with the independent runway record")
    for runway in runways:
        row = surveyed[runway["id"]]
        for side, point in zip(("le", "he"), runway["xy_m"]):
            reference = project(origin, (float(row[side + "_longitude_deg"]), float(row[side + "_latitude_deg"])))
            if math.dist(reference, point) > M_PER_PX:
                raise ValueError(f"{icao} {runway['id']} physical endpoint exceeds one pixel from reference")

def faa_surfaces(packet, icao, origin):
    polygons, open_runways = [], set()
    for layer, color, status_key in (("Apron", 1, "APRON_OPER"), ("Building", 1, None),
                                     ("Taxiway", 2, "TWY_OPER"), ("Runway", 3, "RWY_OPER")):
        for feature in read_json(packet, f"faa-AM_{layer}-selected.geojson")["features"]:
            props = feature["properties"]
            if props["ICAO_ID"] != icao or (status_key and str(props.get(status_key)) != "2"):
                continue
            if layer == "Runway":
                open_runways.add(props["RWY_ID"])
            geometry = feature["geometry"]
            if geometry["type"] not in ("Polygon", "MultiPolygon"):
                raise ValueError(f"unexpected {layer} geometry")
            groups = [geometry["coordinates"]] if geometry["type"] == "Polygon" else geometry["coordinates"]
            for group in groups:
                polygons.append((color, [[project(origin, p[:2]) for p in ring] for ring in group]))
    return polygons, open_runways


def paint_area(pixels, width, height, rings, color):
    """Even-odd scanlines at pixel centers. Holes expose earlier layers."""
    y_min = max(0, math.ceil(min(y for ring in rings for _, y in ring) - .5))
    y_max = min(height, math.ceil(max(y for ring in rings for _, y in ring) - .5))
    for row in range(y_min, y_max):
        scan = row + .5
        hits = []
        for ring in rings:
            for (ax, ay), (bx, by) in zip(ring, ring[1:]):
                if (ay <= scan < by) or (by <= scan < ay):
                    hits.append(ax + (scan - ay) * (bx - ax) / (by - ay))
        hits.sort()
        if len(hits) % 2:
            raise ValueError("open polygon in raster input")
        for left, right in zip(hits[::2], hits[1::2]):
            start, end = max(0, math.ceil(left - .5)), min(width, math.ceil(right - .5))
            if end > start:
                pixels[row*width+start:row*width+end] = bytes([color]) * (end - start)


def paint_line(pixels, width, height, points, thickness, color):
    # Keep a surveyed centreline legible when its width is below one display
    # pixel. Only the original metric runway width is used for position matching.
    radius = max(.72, thickness / 2)
    for (ax, ay), (bx, by) in zip(points, points[1:]):
        dx, dy = bx-ax, by-ay
        denom = dx*dx + dy*dy
        for y in range(max(0, math.floor(min(ay, by)-radius)), min(height, math.ceil(max(ay, by)+radius))):
            for x in range(max(0, math.floor(min(ax, bx)-radius)), min(width, math.ceil(max(ax, bx)+radius))):
                t = max(0, min(1, ((x+.5-ax)*dx+(y+.5-ay)*dy)/denom)) if denom else 0
                if (x+.5-ax-t*dx)**2 + (y+.5-ay-t*dy)**2 <= radius*radius:
                    pixels[y*width+x] = color


def boundary_bits(pixels, width, height):
    """One bit at each pixel center, with independent padded rows."""
    stride = (width + 7) // 8
    packed = bytearray(stride * height)
    for y in range(height):
        for x in range(width):
            if pixels[y * width + x]:
                packed[y * stride + x // 8] |= 1 << (7 - x % 8)
    return base64.b64encode(packed).decode("ascii")


def faa_publication(packet, valid_until):
    publication = {}
    files = []
    stamp = r"([0-9]{4}Z [0-9]{1,2} [A-Za-z]{3} [0-9]{4})"
    for layer in ("runway", "taxiway"):
        filename = f"faa-am-{layer}-item.json"
        files.append(filename)
        item = read_json(packet, filename)
        match = re.search("Current Effective Date: " + stamp + " to " + stamp, item["description"])
        if not match:
            raise ValueError(f"missing published effective interval on the FAA {layer} item")
        start, end = [dt.datetime.strptime(value, "%H%MZ %d %b %Y").replace(tzinfo=dt.timezone.utc)
                      for value in match.groups()]
        if not start.timestamp() < valid_until <= end.timestamp():
            raise ValueError(f"FAA {layer} publication expires before the NASR review deadline")
        publication[layer] = {"item_id": item["id"], "effective_utc": start.isoformat(),
                              "expires_utc": end.isoformat()}
    edits = {}
    for layer in ("Runway", "Taxiway", "Apron", "Building"):
        filename = f"faa-AM_{layer}-metadata.json"
        files.append(filename)
        ts = read_json(packet, filename)["editingInfo"]["dataLastEditDate"] / 1000
        reviewed = dt.datetime.fromtimestamp(ts, tz=dt.timezone.utc)
        if not valid_until - 56 * 86400 <= ts < valid_until:
            raise ValueError(f"FAA {layer} layer requires source-date review")
        edits[layer] = reviewed.isoformat()
    publication["layer_edit_utc"] = edits
    return publication, files


def pack_pixels(pixels):
    return bytes((a<<6)|(b<<4)|(c<<2)|d for a,b,c,d in zip(*[iter(pixels)]*4))


def build_airport(packet, icao, base, qa, valid_until):
    origin = ([float(base["LONG_DECIMAL"]), float(base["LAT_DECIMAL"])] if base
              else [float(qa["longitude_deg"]), float(qa["latitude_deg"])])
    runways = nasr_runways(packet, base, origin) if base else []
    files = ["APT_BASE-selected.json", "APT_RWY-selected.json", "APT_RWY_END-selected.json"] if base else ["ourairports-airports-selected.csv"]
    publication = {}
    if icao in ("KORD", "KDPA"):
        publication, metadata_files = faa_publication(packet, valid_until)
        files.extend(metadata_files)
        polygons, open_runways = faa_surfaces(packet, icao, origin)
        runways = [r for r in runways if r["id"] in open_runways]
        lines, bound = [], []
        sources = ["NASR", "Airport Mapping"]
        files.extend(f"faa-AM_{layer}-selected.geojson" for layer in ("Apron","Building","Taxiway","Runway"))
    else:
        name = f"osm-{icao}.json"
        files.append(name)
        osm = read_json(packet,name)
        polygons, lines, bound, osm_runways = osm_shapes(osm, origin, icao)
        if not base:
            runways = osm_runways
            check_osm_runway_ends(packet, icao, origin, runways)
            files.append("ourairports-selected-runways.json")
        sources = (["NASR"] if base else ["OurAirports identity"]) + ["OpenStreetMap contributors (ODbL)"]
        lines.extend((3, r["xy_m"], r["width_m"]) for r in runways)
    if not runways or not polygons:
        raise ValueError(f"{icao} missing active airport surfaces or runway pairs")
    points = ([pt for ring in bound for pt in ring] if bound else
              [pt for _, rings in polygons for ring in rings for pt in ring])
    west = (math.floor(min(x for x,_ in points)/M_PER_PX) - 1) * M_PER_PX
    north = (math.ceil(max(y for _,y in points)/M_PER_PX) + 1) * M_PER_PX
    width = max(64, 4 * math.ceil((1 + (max(x for x,_ in points)-west)/M_PER_PX) / 4))
    height = max(18, math.ceil(1 + (north-min(y for _,y in points))/M_PER_PX))
    if width > 1024 or height > 1024:
        raise ValueError("airport atlas exceeds supported extent")
    def screen(points):
        return [((x-west)/M_PER_PX, (north-y)/M_PER_PX) for x,y in points]
    pixels = bytearray(width*height)
    for priority in (1,2,3):
        for color,rings in polygons:
            if color == priority:
                paint_area(pixels, width, height, [screen(r) for r in rings], color)
        for color, way, thickness in lines:
            if color == priority:
                paint_line(pixels, width, height, screen(way), thickness/M_PER_PX, color)
    if bound:
        mask = bytearray(width*height)
        paint_area(mask,width,height,[screen(r) for r in bound],1)
        for i,inside in enumerate(mask):
            if not inside: pixels[i] = 0
    hashes = {f:hashlib.sha256((packet/f).read_bytes()).hexdigest() for f in files}
    asset = {"origin":origin,"west_m":west,"north_m":north,"m_per_px":M_PER_PX,
             "width":width,"height":height,"bitmap":base64.b64encode(pack_pixels(pixels)).decode(),
             "runways":sorted(runways,key=lambda r:r["id"]),"valid_until_unix":valid_until,
             "sources":sources,"input_sha256":hashes}
    if base:
        asset["nasr_effective_utc"] = base["EFF_DATE"].replace("/", "-")
    if publication:
        asset["faa_airport_mapping"] = publication
        asset["faa_disclaimer"] = "https://adds-faa.opendata.arcgis.com/pages/adds-data-disclaimer"
    if bound:
        asset["boundary"] = boundary_bits(mask, width, height)
        asset["osm_license"] = "ODbL-1.0"
        asset["osm_attribution"] = "© OpenStreetMap contributors, https://www.openstreetmap.org/copyright"
        asset["osm_timestamp"] = osm["osm3s"]["timestamp_osm_base"]
        captured = dt.datetime.fromisoformat(asset["osm_timestamp"].replace("Z","+00:00")).timestamp()
        if not valid_until-28*86400 <= captured < valid_until:
            raise ValueError(f"{icao} mapping snapshot falls outside the review cycle")
    asset["version"] = hashlib.sha256(json.dumps(asset,sort_keys=True).encode()).hexdigest()[:8]
    return asset


def build(packet):
    bases = {r["ICAO_ID"]:r for r in read_json(packet, "APT_BASE-selected.json")}
    cycles = {r["EFF_DATE"] for r in bases.values()}
    if len(cycles) != 1:
        raise ValueError("multiple NASR cycles in packet")
    cycle = dt.datetime.strptime(cycles.pop(), "%Y/%m/%d").replace(tzinfo=dt.timezone.utc)
    end = int((cycle+dt.timedelta(days=28)).timestamp())
    with (packet/"ourairports-airports-selected.csv").open() as stream:
        qa = {r["ident"]: r for r in csv.DictReader(stream)}
    return {"schema":1,"effective_utc":cycle.date().isoformat(),
            "airports":{icao:build_airport(packet,icao,bases.get(icao),qa[icao],end) for icao in FIELDS}}


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("packet", type=Path)
    parser.add_argument("output",type=Path)
    args = parser.parse_args()
    content = build(args.packet)
    args.output.write_text(json.dumps(content, sort_keys=True, indent=2)+"\n")
