import json

import FreeCAD as App


FREECAD_COMMIT = "0108fd4b4850cc46e625b60e53cea7a7bbe69f8d"


def snapshot(document, obj, requested):
    recompute_result = bool(document.recompute())
    return {
        "requested": requested,
        "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("PropertyFontSuccess")
    try:
        obj = document.addObject("TechDraw::DrawViewAnnotation", "FontProbe")
        phases = {"default": snapshot(document, obj, "osifont")}
        for phase, value in [("named-family", "DejaVu Sans"), ("unicode-family", "Noto Sans CJK SC"), ("empty-family", ""), ("restored", "osifont")]:
            obj.Font = value
            phases[phase] = snapshot(document, obj, value)
        return {
            "schemaVersion": 1,
            "status": "pass",
            "baselineId": "freecad-1.1.1-property-font-success",
            "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"), "group": obj.getGroupOfProperty("Font")},
            "phases": phases,
            "diagnostics": {"exception": None, "documentObjectCount": len(document.Objects)},
        }
    finally:
        App.closeDocument(document.Name)


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