from usecases.export_assembly_them_all_usecase import ExportAssemblyThemAllUseCase
import FreeCAD
from usecases.export_usecase import EXPORT_TYPES, ExportUseCase
from usecases.get_sdf_geometry_usecase import SdfGeometryUseCase
from usecases.assembly_parse_usecase import AssemblyParseUseCase
from model.files_generator import FolderGenerator
from helper.file_system_repository import FileSystemRepository
import os


class RobossemblerFreeCadExportScenari:
    def call(path):
        directory = path

        __objs__ = FreeCAD.ActiveDocument.RootObjects
        directoryExport = directory + "/"
        FileSystemRepository.deletingOldAndCreatingNewFolder(
            directoryExport + FolderGenerator.ASSETS.value
        )
        FileSystemRepository.deletingOldAndCreatingNewFolder(
            directoryExport + FolderGenerator.SDF.value,
        )

        FileSystemRepository.deletingOldAndCreatingNewFolder(
            directoryExport
            + FolderGenerator.SDF.value
            + "/"
            + FolderGenerator.MESHES.value
        )
        FileSystemRepository.deletingOldAndCreatingNewFolder(
            directoryExport + FolderGenerator.ASSEMBlY.value
        )
        f = open(directory + "/step-structure.json", "w")
        f.write(AssemblyParseUseCase().toJson())
        f.close()
        RobossemblerFreeCadExportScenari.geometry(directory)
        ExportAssemblyThemAllUseCase().call(directoryExport)

        return True

    def geometry(outPutsPath: str):
        exportUseCase = ExportUseCase.call(outPutsPath, EXPORT_TYPES.OBJ)

        for el in SdfGeometryUseCase().call(exportUseCase):
            FileSystemRepository.writeFile(
                el.toJSON(),
                outPutsPath + FolderGenerator.ASSETS.value + "/",
                el.name + ".json",
            )
