#!/usr/bin/env python3
# Copyright (C) 2022 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# pylint: disable=undefined-variable
# type: ignore[name-defined]

from typing import Any, Mapping, Optional, Sequence, Tuple, Union


def _get_proxy_args_and_secrets(details):
    proxy_args = ["--proxy-host", details["proxy_host"]]

    if proxy_port := details.get("proxy_port"):
        proxy_args += ["--proxy-port", str(proxy_port)]

    if proxy_user := details.get("proxy_user"):
        proxy_args += ["--proxy-user", str(proxy_user)]

    if proxy_password := details.get("proxy_pasword"):
        proxy_args += ["--proxy-password", passwordstore_get_cmdline("%s", proxy_password)]

    return proxy_args


def agent_mobileiron_arguments(
    params: Mapping[str, Any], hostname: str, ipaddress: Optional[str]
) -> Sequence[Union[str, Tuple[str, str, str]]]:

    args = [
        elem
        for chunk in (
            ("-u", params["username"]) if "username" in params else (),
            ("-p", passwordstore_get_cmdline("%s", params["password"]))
            if "password" in params
            else (),
            ("--port", params["port"]) if "port" in params else (),
            ("--no-cert-check",) if params.get("no-cert-check") else (),
            ("--partition", ",".join(params["partition"])) if "partition" in params else (),
            ("--hostname", hostname),
        )
        for elem in chunk
    ]
    if "proxy_details" in params:
        args += _get_proxy_args_and_secrets(params["proxy_details"])
    return args


special_agent_info["mobileiron"] = agent_mobileiron_arguments
