import FreeCAD as App
from model.sdf_geometry_model import SdfGeometryModel
 
from helper.is_solid import is_object_solid


class SdfGeometryUseCase:
    ShapePropertyCheck = ['Mass','MatrixOfInertia','Placement', ]
    PartPropertyCheck = ['Shape']
    def call(self, stlPaths:dict) -> list[SdfGeometryModel]:
        materialSolid = {}
        for el in App.ActiveDocument.Objects:
            if str(el) == '<App::MaterialObjectPython object>':   
                friction = el.Material.get('SlidingFriction')
                for i in el.References:
                    materialSolid[i[0].Label] = friction
        geometry = []
        try:
            for el in App.ActiveDocument.Objects:
                if is_object_solid(el):
                    for prop in self.PartPropertyCheck:
                        if prop in el:
                            App.Console.PrintMessage(el.Label + '   ' + 'Dont exists property: ' + prop)
                            return
                    for prop in self.ShapePropertyCheck:
                        if prop in el.Shape:
                            App.Console.PrintMessage(el.Label + '   ' + 'Dont exists property: ' + prop)
                            return
                    # com = el.Shape.CenterOfMass or el.Shape.CenterOfGravity
                    # if "Shape" in el:
                    #      App.Console.PrintMessage(el.Label + '   ' + 'Dont exists Shape')
                    #      return
                    # if "Mass" in el.Shape:
                        
                    mass = el.Shape.Mass
                    inertia = el.Shape.MatrixOfInertia
                    pos = el.Shape.Placement
                    inertia = el.Shape.MatrixOfInertia
                    name = el.Label
                    ixx = str(inertia.A11 / 1000000)
                    ixy = str(inertia.A12 / 1000000)
                    ixz = str(inertia.A13 / 1000000)
                    iyy = str(inertia.A22 / 1000000)
                    iyz = str(inertia.A23 / 1000000)
                    izz = str(inertia.A33 / 1000000)
                    massSDF = str(mass / 1000000)
                    posX = str(pos.Base[0] / 1000000)
                    posY = str(pos.Base[1] / 1000000)
                    posZ = str(pos.Base[2] / 1000000)
                    eulerX = str(pos.Rotation.toEuler()[0])
                    eulerY = str(pos.Rotation.toEuler()[1])
                    eulerZ = str(pos.Rotation.toEuler()[2])

                    geometry.append(
                        SdfGeometryModel(
                            stl=stlPaths.get(el.Label),
                            name=name, 
                            ixx=ixx,
                            ixz=ixz, 
                            ixy=ixy, 
                            iyy=iyy,
                            iyz=iyz,
                            izz=izz,
                            massSDF=massSDF,
                            posX=posX,
                            posY=posY,
                            posZ=posZ,
                            eulerX=eulerX, 
                            eulerY=eulerY, 
                            eulerZ=eulerZ,
                            friction=materialSolid.get(el.Label) or '',
                        )
                    )
        except Exception as e:
            print(200)
        return geometry

 
 


