# Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES, ETH Zurich, and University of Toronto
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#

"""
This script merges the python.analysis.extraPaths from the "_isaac_sim/.vscode/settings.json" file into
the ".vscode/settings.json" file.

This is necessary because Isaac Sim 2022.2.1 does not add the necessary python packages to the python path
when the "setup_python_env.sh" is run as part of the vs-code launch configuration.
"""

import re
import os
import pathlib


ORBIT_DIR = pathlib.Path(__file__).parents[2]
"""Path to the orbit directory."""
ISAACSIM_DIR = os.path.join(ORBIT_DIR, "_isaac_sim")
"""Path to the isaac-sim directory."""


def main():
    # isaac-sim settings
    isaacsim_vscode_filename = os.path.join(ISAACSIM_DIR, ".vscode", "settings.json")
    # make sure the isaac-sim settings file exists
    if not os.path.exists(isaacsim_vscode_filename):
        raise FileNotFoundError(f"Could not find the isaac-sim settings file: {isaacsim_vscode_filename}")
    # read the path names from the isaac-sim settings file
    with open(isaacsim_vscode_filename) as f:
        vscode_settings = f.read()

    # extract the path names
    # search for the python.analysis.extraPaths section and extract the contents
    settings = re.search(r"\"python.analysis.extraPaths\": \[.*?\]", vscode_settings, flags=re.MULTILINE | re.DOTALL)
    settings = settings.group(0)
    settings = settings.split('"python.analysis.extraPaths": [')[-1]
    settings = settings.split("]")[0]
    # change the path names to be relative to the orbit directory
    path_names = settings.split(",")
    path_names = [path_name.strip().strip('"') for path_name in path_names]
    path_names = ['"${workspaceFolder}/_isaac_sim/' + path_name + '"' for path_name in path_names if len(path_name) > 0]
    # combine them into a single string
    path_names = ",\n\t\t".expandtabs(4).join(path_names)

    # orbit template settings
    orbit_vscode_template_filename = os.path.join(ORBIT_DIR, ".vscode", "tools", "settings.template.json")
    # make sure the orbit template settings file exists
    if not os.path.exists(orbit_vscode_template_filename):
        raise FileNotFoundError(f"Could not find the orbit template settings file: {orbit_vscode_template_filename}")
    # read the orbit template settings file
    with open(orbit_vscode_template_filename) as f:
        orbit_template_settings = f.read()
    # replace the path names in the orbit settings file with the path names from the isaac-sim settings file
    orbit_settings = re.sub(
        r"\"python.analysis.extraPaths\": \[.*?\]",
        '"python.analysis.extraPaths": [\n\t\t'.expandtabs(4) + path_names + "\n\t]".expandtabs(4),
        orbit_template_settings,
        flags=re.DOTALL,
    )
    # add template notice to the top of the file
    header_message = (
        "// This file is a template and is automatically generated by the setup_vscode.py script.\n"
        "// Do not edit this file directly.\n"
        "// \n"
        f"// Generated from: {orbit_vscode_template_filename}\n"
    )
    orbit_settings = header_message + orbit_settings

    # write the orbit settings file
    orbit_vscode_filename = os.path.join(ORBIT_DIR, ".vscode", "settings.json")
    with open(orbit_vscode_filename, "w") as f:
        f.write(orbit_settings)


if __name__ == "__main__":
    main()
