# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Serializes an Environment into a JSON file."""

import ntpath
import os
import posixpath
import re


class GNIVisitor:
    """Serializes portions of an Environment into a gni file.

    Example gni file:

    declare_args() {
      pw_env_setup_CIPD_ARM = "//<ENVIRONMENT_DIR>/cipd/packages/arm"
      pw_env_setup_CIPD_BAZEL = "//<ENVIRONMENT_DIR>/cipd/packages/bazel"
      pw_env_setup_CIPD_DEFAULT = "//<ENVIRONMENT_DIR>/cipd/packages/default"
      pw_env_setup_CIPD_LUCI = "//<ENVIRONMENT_DIR>/cipd/packages/luci"
      pw_env_setup_CIPD_PIGWEED = "//<ENVIRONMENT_DIR>/cipd/packages/pigweed"
      pw_env_setup_CIPD_PYTHON = "//<ENVIRONMENT_DIR>/cipd/packages/python"
      pw_env_setup_VIRTUAL_ENV = "//<ENVIRONMENT_DIR>/pigweed-venv"
    }
    """

    def __init__(self, project_root, gni_file, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._project_root = project_root
        self._gni_file = gni_file
        self._variables = {}  # Dict of variables to set.

    def _gn_variables(self, env):
        self._variables.clear()
        env.accept(self)
        variables = dict(self._variables)
        self._variables.clear()
        return variables

    def serialize(self, env, outs):
        """Write a gni file based on the given environment.

        Args:
            env (environment.Environment): Environment variables to use.
            outs (file): GNI file to write.
        """

        print(
            """
# This file is automatically generated by Pigweed's environment setup. Do not
# edit it manually or check it in.

# Relative paths are interpreted with respect to this file, which helps
# determine the correct path even if the source root changes.
            """.strip(),
            file=outs,
        )

        print('declare_args() {', file=outs)
        for name, value in sorted(self._gn_variables(env).items()):
            print('  {} = {}'.format(name, value), file=outs)
        print('}', file=outs)

    def _abspath_to_gn_path(self, path):
        gn_dir = os.path.join(
            self._project_root, os.path.dirname(self._gni_file)
        )
        # Use relative paths within the project root:
        if path.startswith(self._project_root + os.sep):
            gn_path = os.path.relpath(path, start=gn_dir)
        else:
            gn_path = path
        if os.name == 'nt':
            # GN paths are posix-style, so convert to posix. This
            # find-and-replace is a little crude, but python 2.7 doesn't support
            # pathlib.
            gn_path = gn_path.replace(ntpath.sep, posixpath.sep)
        return 'get_path_info("{}", "abspath")'.format(gn_path)

    def visit_set(self, set):  # pylint: disable=redefined-builtin
        match = re.search(r'PW_(.*)_CIPD_INSTALL_DIR', set.name)
        name = None

        if match:
            name = 'pw_env_setup_CIPD_{}'.format(match.group(1))
        if set.name == 'VIRTUAL_ENV':
            name = 'pw_env_setup_VIRTUAL_ENV'
        if set.name == 'PW_PACKAGE_ROOT':
            name = 'pw_env_setup_PACKAGE_ROOT'

        if name:
            self._variables[name] = self._abspath_to_gn_path(set.value)

    def visit_clear(self, clear):
        pass

    def visit_remove(self, remove):
        pass

    def visit_prepend(self, prepend):
        pass

    def visit_append(self, append):
        pass

    def visit_echo(self, echo):
        pass

    def visit_comment(self, comment):
        pass

    def visit_command(self, command):
        pass

    def visit_doctor(self, doctor):
        pass

    def visit_blank_line(self, blank_line):
        pass

    def visit_function(self, function):
        pass

    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
        pass
