#! /usr/bin/env python
"""Rectangle compression workflow: geometry, partition, mesh

Requires the following ``SConscript(..., exports={})``

* ``env`` - The SCons construction environment with the following required keys

  * ``unconditional_build`` - Boolean flag to force building of conditionally ignored targets

* ``envCubit`` - The Cubit Python SCons construction environment with the following required keys

  * ``python`` - String path for the Cubit Python intepreter

* ``element_type`` - The Cubit 4 node quadrilateral element type
* ``solver`` - The target solver to use when writing a mesh file
"""

import pathlib

import waves

# Inherit the parent construction environment
Import("env", "envCubit", "element_type", "solver")

# Simulation variables
build_directory = pathlib.Path(Dir(".").abspath)
workflow_name = build_directory.name

# Collect the target nodes to build a concise alias for all targets
workflow = []

# Rectangle 2D
# Geometry
workflow.extend(
    envCubit.PythonScript(
        target=["rectangle_geometry.cub"],
        source=["#/modsim_package/cubit/rectangle_geometry.py"],
        subcommand_options="",
    )
)

# Partition
workflow.extend(
    envCubit.PythonScript(
        target=["rectangle_partition.cub"],
        source=["#/modsim_package/cubit/rectangle_partition.py", "rectangle_geometry.cub"],
        subcommand_options="",
    )
)

# Mesh
if solver.lower() == "abaqus":
    mesh_extension = "inp"
elif solver.lower() in ["sierra", "adagio"]:
    mesh_extension = "g"
else:
    raise RuntimeError(f"Unknown solver '{solver}'")
workflow.extend(
    envCubit.PythonScript(
        target=[f"rectangle_mesh.{mesh_extension}", "rectangle_mesh.cub"],
        source=["#/modsim_package/cubit/rectangle_mesh.py", "rectangle_partition.cub"],
        subcommand_options="--element-type ${element_type} --solver ${solver}",
        element_type=element_type,
        solver=solver,
    )
)


# Cube 3D
# Geometry
workflow.extend(
    envCubit.PythonScript(
        target=["cube_geometry.cub"],
        source=["#/modsim_package/cubit/cube_geometry.py"],
        subcommand_options="",
    )
)

# Partition
workflow.extend(
    envCubit.PythonScript(
        target=["cube_partition.cub"],
        source=["#/modsim_package/cubit/cube_partition.py", "cube_geometry.cub"],
        subcommand_options="",
    )
)

# Mesh
if solver.lower() == "abaqus":
    mesh_extension = "inp"
elif solver.lower() in ["sierra", "adagio"]:
    mesh_extension = "g"
else:
    raise RuntimeError(f"Unknown solver '{solver}'")
workflow.extend(
    envCubit.PythonScript(
        target=[f"cube_mesh.{mesh_extension}", "cube_mesh.cub"],
        source=["#/modsim_package/cubit/cube_mesh.py", "cube_partition.cub"],
        subcommand_options="--element-type ${element_type} --solver ${solver}",
        element_type=element_type,
        solver=solver,
    )
)

# Collector alias based on parent directory name
envCubit.Alias(f"{workflow_name}_cubit", workflow)

if not env["unconditional_build"] and not envCubit["python"]:
    print(
        "The Cubit Python interpreter was not found in the construction environment. "
        f"Ignoring '{workflow_name}' target(s)"
    )
    Ignore([".", workflow_name], workflow)
