import unreal


MAP_PATH = "/Game/Agrarian/Maps/L_GroundZeroTerrain_Test"
BOUNDARY_LABEL = "AGR_GroundZeroMapBoundary"
EXPECTED_BOUNDARY_ID = "ground_zero_mvp_tile"
EXPECTED_LOCATION = unreal.Vector(0.0, 0.0, 10000.0)
EXPECTED_EXTENT = unreal.Vector(50000.0, 50000.0, 25000.0)
EXPECTED_PADDING_CM = 250.0
EXPECTED_WARNING_DISTANCE_CM = 3000.0
TOLERANCE = 1.0


def get_actor_label(actor):
    try:
        return actor.get_actor_label()
    except Exception:
        return actor.get_name()


def nearly_equal(a, b, tolerance=TOLERANCE):
    return abs(float(a) - float(b)) <= tolerance


def vectors_nearly_equal(a, b, tolerance=TOLERANCE):
    return nearly_equal(a.x, b.x, tolerance) and nearly_equal(a.y, b.y, tolerance) and nearly_equal(a.z, b.z, tolerance)


def main():
    if not unreal.EditorLevelLibrary.load_level(MAP_PATH):
        raise RuntimeError(f"Could not load map: {MAP_PATH}")

    boundaries = [
        actor
        for actor in unreal.EditorLevelLibrary.get_all_level_actors()
        if isinstance(actor, unreal.AgrarianMapBoundaryVolume)
    ]
    labeled_boundaries = [actor for actor in boundaries if get_actor_label(actor) == BOUNDARY_LABEL]

    failures = []
    if len(labeled_boundaries) != 1:
        failures.append(f"expected one {BOUNDARY_LABEL}, found {len(labeled_boundaries)}")
    if len(boundaries) != 1:
        failures.append(f"expected one AgrarianMapBoundaryVolume actor, found {len(boundaries)}")

    if labeled_boundaries:
        boundary = labeled_boundaries[0]
        if str(boundary.get_editor_property("boundary_id")) != EXPECTED_BOUNDARY_ID:
            failures.append(f"boundary_id expected {EXPECTED_BOUNDARY_ID}, got {boundary.get_editor_property('boundary_id')}")
        if not bool(boundary.get_editor_property("clamp_players_at_boundary")):
            failures.append("clamp_players_at_boundary is disabled")
        if not nearly_equal(boundary.get_editor_property("boundary_padding_cm"), EXPECTED_PADDING_CM):
            failures.append("boundary_padding_cm mismatch")
        if not nearly_equal(boundary.get_editor_property("warning_distance_cm"), EXPECTED_WARNING_DISTANCE_CM):
            failures.append("warning_distance_cm mismatch")
        if not vectors_nearly_equal(boundary.get_actor_location(), EXPECTED_LOCATION):
            failures.append(f"boundary location expected {EXPECTED_LOCATION}, got {boundary.get_actor_location()}")

        extent = boundary.boundary_volume.get_unscaled_box_extent()
        if not vectors_nearly_equal(extent, EXPECTED_EXTENT):
            failures.append(f"boundary extent expected {EXPECTED_EXTENT}, got {extent}")

        inside_location = unreal.Vector(0.0, 0.0, 10000.0)
        warning_location = unreal.Vector(48200.0, 0.0, 10000.0)
        outside_location = unreal.Vector(52000.0, 0.0, 10000.0)
        clamped_location = boundary.clamp_location_to_boundary(outside_location)
        if boundary.is_location_outside_boundary(inside_location):
            failures.append("inside test location incorrectly detected outside boundary")
        if not boundary.is_location_inside_warning_zone(warning_location):
            failures.append("warning-zone test location did not trigger warning zone")
        if not boundary.is_location_outside_boundary(outside_location):
            failures.append("outside test location did not trigger outside boundary")
        if clamped_location.x > EXPECTED_EXTENT.x - EXPECTED_PADDING_CM + TOLERANCE:
            failures.append(f"clamped X exceeds padded east boundary: {clamped_location.x}")

    if failures:
        raise RuntimeError("Ground Zero map boundary verification failed: " + "; ".join(failures))

    unreal.log("Ground Zero map boundary verification complete.")


main()
