import json

import FreeCAD as App


FREECAD_COMMIT = "0108fd4b4850cc46e625b60e53cea7a7bbe69f8d"


def snapshot(document, obj):
    recompute_result = bool(document.recompute())
    return {
        "value": str(obj.Font),
        "propertyTypeId": obj.getTypeIdOfProperty("Font"),
        "propertyStatus": [str(item) for item in obj.getPropertyStatus("Font")],
        "editorMode": [str(item) for item in obj.getEditorMode("Font")],
        "objectState": [str(item) for item in obj.State],
        "statusString": str(obj.getStatusString()),
        "recomputeResult": recompute_result,
        "hasShapeProperty": "Shape" in obj.PropertiesList,
        "objectSet": [{"name": candidate.Name, "typeId": candidate.TypeId} for candidate in document.Objects],
    }


def run():
    document = App.newDocument("PropertyFontMutation")
    try:
        obj = document.addObject("TechDraw::DrawViewAnnotation", "FontProbe")
        document.recompute()
        before = snapshot(document, obj)
        obj.Font = "Liberation Sans"
        edited = snapshot(document, obj)
        obj.Font = "osifont"
        restored = snapshot(document, obj)
        return {
            "schemaVersion": 1,
            "status": "pass",
            "baselineId": "freecad-1.1.1-property-font-mutation",
            "freecadVersion": ".".join(str(value) for value in App.Version()[:3]),
            "gitCommit": FREECAD_COMMIT,
            "propertyType": "App::PropertyFont",
            "object": {"name": obj.Name, "typeId": obj.TypeId},
            "property": {"name": "Font", "typeId": obj.getTypeIdOfProperty("Font")},
            "requested": {"before": "osifont", "edited": "Liberation Sans", "restored": "osifont"},
            "phases": {"before": before, "edited": edited, "restored": restored},
            "classification": {"valueChanged": before["value"] != edited["value"], "restoreExact": restored["value"] == before["value"] and restored["propertyTypeId"] == before["propertyTypeId"] and restored["propertyStatus"] == before["propertyStatus"] and restored["editorMode"] == before["editorMode"] and restored["objectState"] == before["objectState"] and restored["statusString"] == before["statusString"] and restored["hasShapeProperty"] == before["hasShapeProperty"] and restored["objectSet"] == before["objectSet"], "objectSetStable": before["objectSet"] == edited["objectSet"] == restored["objectSet"], "geometryApplicability": "not-applicable-no-shape-property", "unknownSemanticDrift": False},
        }
    finally:
        App.closeDocument(document.Name)


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