#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 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.


def parse_connection_arguments(params, hostname, ipaddress):
    """Translation function for connection options

    datasource_programs.py contains a standard set of connection option elements
    using these components (in full or partially) allows you to use this function
    in order to convert the params to command line arguments

    Returns:
        list of command line arguments concerning connection options

    """
    connection_args = []
    if "connection" in params:
        connection_option = params["connection"]
        if connection_option == "host_name":
            connection_args.extend(["--address", hostname])
        elif connection_option == "ip_address":
            connection_args.extend(["--address", ipaddress])
        else:  # custom URL
            connection_args.extend(["--url-custom", connection_option[1]["url_address"]])

    for param in ("port", "protocol", "no-cert-check", "token"):
        if param in params:
            connection_args.extend([f"--{param}", f"{params[param]}"])

    if "auth_basic" in params:
        auth_info = params["auth_basic"]
        connection_args.extend(
            ["--username", f"{auth_info['username']}", "--password", f"{auth_info['password'][1]}"]
        )

    return connection_args
