import FreeCAD as App
from model.robosembler_assets import Asset
from model.sdf_geometry_model import SdfGeometryModel

from helper.is_solid import is_object_solid


class GeometryUseCase:
    ShapePropertyCheck = [
        "Mass",
        "MatrixOfInertia",
        "Placement",
    ]
    PartPropertyCheck = ["Shape"]

    def call(self, stlPaths: dict) -> list[Asset]:
        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):
                    mass = el.Shape.Mass
                    inertia = el.Shape.MatrixOfInertia
                    pos = el.Shape.Placement
                    inertia = el.Shape.MatrixOfInertia
                    name = el.Label
                    delimiter = 1000000
                    ixx = str(inertia.A11 / delimiter)
                    ixy = str(inertia.A12 / delimiter)
                    ixz = str(inertia.A13 / delimiter)
                    iyy = str(inertia.A22 / delimiter)
                    iyz = str(inertia.A23 / delimiter)
                    izz = str(inertia.A33 / delimiter)
                    massSDF = str(mass / delimiter)
                    posX = str(pos.Base[0] / delimiter)
                    posY = str(pos.Base[1] / delimiter)
                    posZ = str(pos.Base[2] / delimiter)
                    eulerX = str(pos.Rotation.toEuler()[0])
                    eulerY = str(pos.Rotation.toEuler()[1])
                    eulerZ = str(pos.Rotation.toEuler()[2])
                    centerMassX = str(el.Shape.CenterOfMass[0])
                    centerMassY = str(el.Shape.CenterOfMass[1])
                    centerMassZ = str(el.Shape.CenterOfMass[2])
                    geometry.append(
                        Asset(
                            meshPath=stlPaths.get(el.Label),
                            name=name,
                            ixx=ixx,
                            ixz=ixz,
                            ixy=ixy,
                            iyy=iyy,
                            iyz=iyz,
                            izz=izz,
                            mass=massSDF,
                            posX=posX,
                            posY=posY,
                            posZ=posZ,
                            eulerX=eulerX,
                            eulerY=eulerY,
                            eulerZ=eulerZ,
                            friction=materialSolid.get(el.Label) or "",
                            centerMassX=centerMassX,
                            centerMassY=centerMassY,
                            centerMassZ=centerMassZ,
                        )
                    )
        except Exception as e:
            print(e)
        return geometry
