# WARNING: This file/interface is likely to be removed after transition from Legacy Viewport is complete.
# Seee merge_requests/17255

__all__ = ["get_tests_to_run"]


# Rewrite a Legacy Viewport test to launch with Viewport backend only
def _get_viewport_next_test_config(test):
    # Check for flag to run test only on legacy Viewport
    if test.config.get("viewport_legacy_only", False):
        return None

    # Get the test dependencies
    test_dependencies = test.config.get("dependencies", tuple())

    # Check if legacy Viewport is in the test dependency list
    if "omni.kit.window.viewport" not in test_dependencies:
        return None

    # Deep copy the config dependencies
    test_dependencies = list(test_dependencies)

    # Re-write any 'omni.kit.window.viewport' dependency as 'omni.kit.viewport.window'
    for i in range(len(test_dependencies)):
        cur_dep = test_dependencies[i]
        if cur_dep == "omni.kit.window.viewport":
            test_dependencies[i] = "omni.kit.viewport.window"

    # Shallow copy of the config
    test_config = test.config.copy()
    # set the config name
    test_config["name"] = "viewport_next"
    # Replace the dependencies
    test_config["dependencies"] = test_dependencies

    # Add any additional args by deep copying the arg list
    test_args = list(test.config.get("args", tuple()))
    test_args.append("--/exts/omni.kit.viewport.window/startup/windowName=Viewport")
    # Replace the args
    test_config["args"] = test_args

    # TODO: Error if legacy Viewport somehow still inserting itself into the test run

    # Return the new config
    return test_config


def get_tests_to_run(test, ExtTest, run_context, is_parallel_run: bool, valid: bool):
    """For a test gather all unique test-runs that should be invoked"""

    # First run the additional tests, followed by the original / base-case

    # No need to run additional tests of the original is not valid
    if valid:
        # Currently the only usage of this method is to have legacy Viewport test run against new Viewport
        additional_tests = {
            'viewport_next': _get_viewport_next_test_config
        }
        for test_name, get_config in additional_tests.items():
            new_config = get_config(test)
            if new_config:
                yield ExtTest(
                    ext_id=test.ext_id,
                    ext_info=test.ext_info,
                    test_config=new_config,
                    test_id=f"{test.test_id}-{test_name}",
                    is_parallel_run=is_parallel_run,
                    run_context=run_context,
                    test_app=test.test_app,
                    valid=valid
                )

    # Run the original / base-case
    yield test
