from model.robosembler_assets import RobossemblerAssets
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 GeometryUseCase
from usecases.assembly_parse_usecase import AssemblyParseUseCase
from model.files_generator import FolderGenerator
from helper.file_system_repository import FileSystemRepository
import os
import json


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

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

        RobossemblerFreeCadExportScenario.geometry(directory)
        ExportAssemblyThemAllUseCase().call(directoryExport)         
        return True

    def geometry(outPutsPath: str):
        exportUseCase = ExportUseCase.call(outPutsPath, EXPORT_TYPES.OBJ)
        # for el in GeometryUseCase().call(exportUseCase):
        #     FileSystemRepository.writeFile(el.toJSON(),   outPutsPath + '/' + FolderGenerator.ASSETS.value + '/', el.name + '.json',)

        assets = []
        for el in GeometryUseCase().call(exportUseCase):
            assets.append(el)
        robossemblerAssets = RobossemblerAssets(assets=assets, instances=[]).toJson()
        FileSystemRepository.writeFile(
            robossemblerAssets,
            outPutsPath,
            "robossembler_assets.json",
        )
