import json
import hashlib
import os

import FreeCAD as App


FREECAD_COMMIT = "0108fd4b4850cc46e625b60e53cea7a7bbe69f8d"
CASES = [
    ("default", None),
    ("nominal", "500 mm/s^2"),
    ("lower-bound", "0 mm/s^2"),
    ("negative-boundary", "-500 mm/s^2"),
]


def value_snapshot(obj):
    value = getattr(obj, "Acceleration")
    return {
        "raw": str(value),
        "numeric": round(float(value.Value), 9),
        "unit": str(value.Unit),
    }


def shape_snapshot(obj):
    if not hasattr(obj, "Shape"):
        return {"applicable": False}
    shape = obj.Shape
    if shape.isNull():
        return {"applicable": True, "shapeNull": True, "shapeValid": None, "solids": 0, "faces": 0}
    return {
        "applicable": True,
        "shapeNull": False,
        "shapeValid": bool(shape.isValid()),
        "solids": len(shape.Solids),
        "faces": len(shape.Faces),
    }


def run():
    document = App.newDocument("PropertyAccelerationSuccess")
    try:
        obj = document.addObject("Robot::TrajectoryDressUpObject", "AccelerationProbe")
        document.recompute()
        cases = []
        for case_id, requested in CASES:
            if requested is not None:
                obj.Acceleration = requested
                document.recompute()
            cases.append({
                "id": case_id,
                "requested": requested,
                "value": value_snapshot(obj),
                "status": [str(item) for item in obj.getPropertyStatus("Acceleration")],
                "typeId": obj.getTypeIdOfProperty("Acceleration"),
                "group": obj.getGroupOfProperty("Acceleration"),
                "editorMode": [str(item) for item in obj.getEditorMode("Acceleration")],
                "shape": shape_snapshot(obj),
                "objectState": [str(item) for item in obj.State],
            })
        return {
            "schemaVersion": 1,
            "status": "pass",
            "baselineId": "freecad-1.1.1-property-acceleration-success",
            "freecadVersion": ".".join(str(value) for value in App.Version()[:3]),
            "gitCommit": FREECAD_COMMIT,
            "object": {"name": obj.Name, "typeId": obj.TypeId},
            "property": {"name": "Acceleration", "typeId": obj.getTypeIdOfProperty("Acceleration"), "group": obj.getGroupOfProperty("Acceleration")},
            "cases": cases,
            "diagnostics": {"exception": None, "documentObjectCount": len(document.Objects)},
        }
    finally:
        App.closeDocument(document.Name)


print("FREECAD_PROPERTY_ACCELERATION_SUCCESS_RESULT=" + json.dumps(run(), sort_keys=True))
