import FreeCAD as App
def is_object_solid(obj):
    """If obj is solid return True"""
    if not isinstance(obj, App.DocumentObject):
        return False
    if hasattr(obj, 'Group'):
        return False
    
    if not hasattr(obj, 'Shape'):
        return False
    # if not hasattr(obj.Shape, 'Mass'):
    #     return False
    if not hasattr(obj.Shape, 'Solids'):
        return False

    if len(obj.Shape.Solids) == 0:
        return False

    return True


class AssemblyParseUseCase:
    _parts = []

    _asm = []

    def getAsm(self):
        return self._asm

    def __init__(self) -> None:
        if (self._asm.__len__() == 0):
            self.initParse()
        pass

    def initParse(self):
        for el in App.ActiveDocument.Objects:
            if (is_object_solid(el)):
                self._asm.append(el.Label)

    def toJson(self):
        return str(self._asm).replace('\'', "\"")

    def getSubPartsLink(self, group):
        groupLink = {}
        for el in group:
            if (is_object_solid(el)):
                if str(el.Shape).find('Solid') != -1:
                    if groupLink.get(el.Label) == None:
                        groupLink[el.Label] = []
                    for i in el.Group:
                        if str(i).find('Pad') != -1:
                            groupLink[el.Label].append(i)
        if groupLink.__len__() == 0:
            return None
        return groupLink

    def getLinkedProperty(self):
        return self._asm
