from repository.file_system_repository import FileSystemRepository
from usecases.intersection_computed_use_case import (
    IntersectionComputedUseCase,
)

import json


class IntersectionGeometryUseCase:
    """A class that checks bodies in an assembly for interference and returns the result of the check to a file"""

    def call(contacts, path):
        import FreeCAD as App

        intersection_geometry = {"status": True, "recalculations": None}
        for el in contacts:
            child = App.ActiveDocument.getObjectsByLabel(el.get("child"))[0]
            parent = App.ActiveDocument.getObjectsByLabel(el.get("parent"))[0]
            area = IntersectionComputedUseCase.call([child, parent])
            if area != 0.0:
                if intersection_geometry.get("recalculations") == None:
                    intersection_geometry["status"] = False
                    intersection_geometry["recalculations"] = []
                intersection_geometry["recalculations"].append(
                    {"area": area, "connect": el.get("child") + " " + el.get("parent")}
                )
        FileSystemRepository.writeFile(
            json.dumps(intersection_geometry, ensure_ascii=False, indent=4),
            path,
            "intersection_geometry.json",
        )
