from typing import List, Dict


def isUnique(array, element):
    for i in array:
        if i == element:
            return False

    return True


class GetCollisionAtPrimitiveUseCase(object):
    """Get collisions between primitives

    Args:
        object: cad-model

    Returns:
        dict: collision matrix
    """

    # Получение колизий примитивов
    def call(self, freeCadMetaModels, detailSquares) -> Dict[str, List[str]]:
        import FreeCAD as App

        matrix: Dict[str, List[str]] = {}
        for model in freeCadMetaModels:
            activePart = App.ActiveDocument.getObjectsByLabel(model.label)[0]
            for key in detailSquares:
                if model.label != key:
                    for renderPrimitive in detailSquares[key]:
                        primitivePart = App.ActiveDocument.getObjectsByLabel(
                            renderPrimitive.cadLabel
                        )[0]
                        collisionResult: int = int(
                            activePart.Shape.distToShape(primitivePart.Shape)[0]
                        )
                        if collisionResult == 0:
                            if matrix.get(model.label) == None:
                                matrix[model.label] = [renderPrimitive.label]
                            else:
                                if isUnique(matrix[model.label], renderPrimitive.label):
                                    matrix[model.label].append(renderPrimitive.label)
        return matrix
