#!/usr/bin/freecadcmd

import os
import shutil
import subprocess
import FreeCAD as App  # type: ignore
import Mesh  # type: ignore

# Define the project root directory
project_root = os.path.abspath(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)
)

# Define the directories for CAD files and 3MF export files
printed_cad_dir = os.path.join(project_root, "cad", "printed")
printed_3mf_dir = os.path.join(project_root, "3mf")

# Get the short hash of the current Git commit
git_short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()

# Remove the existing 3MF directory if it exists and create a new one
if os.path.exists(printed_3mf_dir):
    shutil.rmtree(printed_3mf_dir, ignore_errors=True)

os.makedirs(printed_3mf_dir, exist_ok=True)

# List to keep track of files that failed to export
failed_files = []

# Traverse the printed CAD directory and its subdirectories
for root, dirs, files in os.walk(printed_cad_dir):
    for file in files:
        if file.endswith(".FCStd"):
            # Absolute path of the CAD file
            file_path = os.path.join(root, file)

            App.Console.PrintMessage(f"\nProcessing {file_path}\n")

            # Open the FreeCAD document
            doc = App.openDocument(file_path)
            App.setActiveDocument(doc.Name)
            App.ActiveDocument.recompute()

            # Iterate over all objects in the document
            for obj in doc.Objects:
                # Check if the object is Std_Part
                if obj.TypeId == "App::Part":
                    # Construct the output path for the 3MF file using the label
                    output_path = os.path.join(
                        printed_3mf_dir,
                        os.path.relpath(root, printed_cad_dir),
                        f"{obj.Label}_{git_short_hash}.3mf",
                    )
                    # Create the output directory if it doesn't already exist
                    os.makedirs(os.path.dirname(output_path), exist_ok=True)

                    try:
                        # Export the part to a 3MF file
                        Mesh.export([obj], output_path)
                        App.Console.PrintMessage(
                            f"Exported {obj.Label} to {output_path}\n"
                        )
                    except Exception as e:
                        App.Console.PrintError(f"Error exporting {obj.Label}: {e}\n")
                        failed_files.append(file_path)

            # Close the FreeCAD document
            App.closeDocument(doc.Name)

# Print the result of the export process
if failed_files:
    App.Console.PrintError("\nThe following files failed to export:\n")
    for file in failed_files:
        App.Console.PrintError(f" - {file}\n")
else:
    App.Console.PrintMessage("\nAll files have been exported successfully.\n")