

from typing import List
import FreeCAD as App
import Part
from model.join_mesh_model import JoinMeshModel
from model.mesh_part_model import MeshPartModel
from helper.fs import FS
from helper.is_solid import is_object_solid
from model.simple_copy_part_model import SimpleCopyPartModel
from model.files_generator import FolderGenerator
from usecases.assembly_parse_usecase import AssemblyParseUseCase
import importOBJ
import os
import json


class ExportAssemblyThemAllUseCase:

    def call(self, path):
        assembly = AssemblyParseUseCase().getAsm()
        asmStructure = {}
        inc = 0
        for el in assembly:
            if (inc != 0):
                asmStructure[inc] = {
                    "child": el,
                    "parents": assembly[0:inc]
                }
            inc += 1
        objectsFreeCad = App.ActiveDocument.Objects
        asmSolids = {}
        for k, v in asmStructure.items():
            assemblyParentList = v['parents']
            assemblyChild = v['child']
            for el in assemblyParentList:
                for solid in objectsFreeCad:
                    if (el == solid.Label):
                        if (asmSolids.get(k) is None):

                            asmSolids[k] = {'parents': [], 'child': list(
                                filter(lambda x: x.Label == assemblyChild, objectsFreeCad))[0]}

                        asmSolids[k]['parents'].append(solid)

        inc = 0
        for k, v in asmSolids.items():
            geometry = {"0": [], "1": []}
            if (k != 0):
                App.activeDocument().addObject("Part::Compound", "Compound")

                copyLinks = list(
                    map(lambda el:  SimpleCopyPartModel(el), v['parents']))

                if copyLinks != None:
                    App.activeDocument().Compound.Links = list(
                        map(lambda el: el.getPart(), copyLinks))

                    object = App.activeDocument().getObject('Compound')
                    boundBox = object.Shape.BoundBox
                    geometry['0'].append(boundBox.XMax)
                    geometry['0'].append(boundBox.YMax)
                    geometry['0'].append(boundBox.ZMax)

                    os.makedirs(
                        path + FolderGenerator.ASSEMBlY.value + '/' + '0000' + str(k))
                    boundBoxChild = v['child'].Shape.BoundBox
                    geometry['1'].append(boundBoxChild.XMax)
                    geometry['1'].append(boundBoxChild.YMax)
                    geometry['1'].append(boundBoxChild.ZMax)
                    meshParents = []
                    
                    for el in v['parents']:
                        meshParents.append(MeshPartModel(el))
                    joinMesh = JoinMeshModel(meshParents)
                    for el in meshParents:
                        el.remove()
                    importOBJ.export(joinMesh.mesh, path + FolderGenerator.ASSEMBlY.value +
                                     '/' + '0000' + str(k) + '/' + str(1) + '.obj')
                    joinMesh.remove()
                    importOBJ.export(v['child'], path + FolderGenerator.ASSEMBlY.value +
                                     '/' + '0000' + str(k) + '/' + str(0) + '.obj')
                    FS.writeFile(json.dumps(geometry), path + FolderGenerator.ASSEMBlY.value +
                                 '/' + '0000' + str(k) + '/', 'translation.json')

                    App.ActiveDocument.removeObject("Compound")
                    for el in copyLinks:
                        el.remove()
                    App.activeDocument().recompute()
            inc += 1
            
 
