#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention Management Console module:
#  Wizards
#
# SPDX-FileCopyrightText: 2012-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

import subprocess
import tempfile

from univention.lib.i18n import Translation
from univention.management.console.log import MODULE
from univention.management.console.modules import UMC_Error

_ = Translation("ucs-school-umc-wizards").translate


class SchoolImport(object):
    """Wrapper for the ucs-school-import script"""

    _SCRIPT_PATH = "/usr/share/ucs-school-import/scripts"
    SCHOOL_SCRIPT = "%s/create_ou" % _SCRIPT_PATH
    GROUP_SCRIPT = "%s/import_group" % _SCRIPT_PATH
    COMPUTER_SCRIPT = "%s/import_computer" % _SCRIPT_PATH
    MOVE_DC_SCRIPT = "%s/move_domaincontroller_to_ou" % _SCRIPT_PATH

    def _run_script(self, script, entry, run_with_string_argument=False):
        """Executes the script with given entry"""
        # Replace `True` with 1 and `False` with 0
        entry = [{True: 1, False: 0}.get(x, x) for x in entry]

        if run_with_string_argument:
            entry.insert(0, script)
            try:
                MODULE.process("Executing command: %s" % " ".join(entry))
                process = subprocess.Popen(entry, stdout=subprocess.PIPE)  # nosec
                stdout, stderr = process.communicate()
                stdout = stdout.decode("UTF-8", "replace")
                if stdout and process.returncode > 0:
                    MODULE.warn(stdout)
            except (OSError, ValueError) as err:
                MODULE.warn(str(err))
                raise UMC_Error(_("Execution of command failed"))
            else:
                return process.returncode, stdout
        else:
            # Separate columns by tabs
            entry = "\t".join(["%s" % column for column in entry])
            try:
                tmpfile = tempfile.NamedTemporaryFile()
                tmpfile.write(entry)
                tmpfile.flush()
                MODULE.process("Executing command: %s" % script)
                process = subprocess.Popen([script, tmpfile.name], stdout=subprocess.PIPE)  # nosec
                stdout, stderr = process.communicate()
                stdout = stdout.decode("UTF-8", "replace")
                if stdout:
                    info = "\n".join(
                        [line for line in stdout.splitlines() if not line.startswith("Processing line ")]
                    )
                    MODULE.process(info)
            except (OSError, ValueError) as err:
                MODULE.warn(str(err))
                raise UMC_Error(_("Execution of command failed"))
            else:
                return process.returncode, stdout
            finally:
                tmpfile.close()
        return None, None

    def import_group(self, school, name, description):
        """Creates a new class"""
        name = "%s-%s" % (school, name)
        entry = [
            "A",
            school,
            name,
            description,
        ]

        return_code, stdout = self._run_script(SchoolImport.GROUP_SCRIPT, entry)
        if return_code:
            raise OSError(_("Could not create class"))

    def import_computer(self, type_, name, mac, school, ip_address, subnet_mask, inventory_number):
        """Imports a new computer"""
        if subnet_mask:
            address = "%s/%s" % (ip_address, subnet_mask)
        else:
            address = ip_address

        entry = [
            type_,
            name,
            mac,
            school,
            address,
            inventory_number,
        ]

        return_code, stdout = self._run_script(SchoolImport.COMPUTER_SCRIPT, entry)
        if return_code:
            raise OSError(_("Could not create computer"))
