#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# License: GNU General Public License v2

# Author: thl-cmk[at]outlook[dot]com
# URL   : https://thl-cmk.hopto.org
# Date  : 2023-10-12
# File  : create_topology_utils.py

#
# options used
# -c --inventory-columns
# -d --data-source
# -k --keep-domain
# -l --lowercase
# -m --make-default
# -o --output-directory
# -p --path-in-inventory
# -s --seed-devices
# -t --time-format
# -u --uppercase
# -v --version

from argparse import (
    Namespace as arg_Namespace,
    ArgumentParser,
    RawTextHelpFormatter,
)


def parse_arguments(create_topology_version: str) -> arg_Namespace:
    script = '~/local/bin/network-topology/create_topology_data.py'
    sample_seeds = 'Core01 Core02'
    cdp_path = '"networking,cdp_cache"'
    cdp_columns = '"device_id,local_port,device_port"'
    cdp_label = 'inv_CDP'
    lldp_path = '"networking,lldp_cache"'
    lldp_columns = '"system_name,local_port_num,port_id"'
    lldp_label = 'inv_LLDP'

    parser = ArgumentParser(
        prog='create_topology_data.py',
        description='This script creates the topology data file needed for the Checkmk "network_visualization" '
                    'plugin by Andreas Boesl and schnetz.\n'
                    'For more information see https://forum.checkmk.com/t/network-visualization/41680 and '
                    'https://exchange.checkmk.com/p/network-visualization\n'
                    '\n'
                    'The inventory data could be created with my inventory plugins:\n'
                    'CDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache\n'
                    'LLDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache\n'
                    '\n'
                    f'\nVersion: {create_topology_version} | Written by: thl-cmk, for more information '
                    f'see: https://thl-cmk.hopto.org',
        formatter_class=RawTextHelpFormatter,
        epilog='Usage:\n'
               'for CDP (the default):\n'
               f'{script} -s {sample_seeds} -m\nor\n'
               f'{script} -s {sample_seeds} -m -p {cdp_path} -c {cdp_columns} -d {cdp_label}\n\n'
               'for LLDP:\n'
               f'{script} -s {sample_seeds} -m -p {lldp_path} -c {lldp_columns} -d {lldp_label}\n',
    )
    command_group = parser.add_mutually_exclusive_group()
    parser.add_argument(
        '-c', '--inventory-columns', type=str,
        help=f'Columns used from the inventory data. I.e. {cdp_columns}\n'
             'NOTE: the columns must be in the order: neighbour, local_port, neighbour_port',
    )
    parser.add_argument(
        '-d', '--data-source', type=str,
        help=f'The source from which the topology data originates. I.e. {cdp_label} '
             'for CDP data from the inventory. NOTE: right now this only an unused label.',
    )
    parser.add_argument(
        '-k', '--keep-domain', default=False, action='store_const', const=True,
        help='Do not remove the domain name from the neighbor name',
    )
    command_group.add_argument(
        '-l', '--lowercase', default=False, action='store_const', const=True,
        help='Change neighbour names to all lower case',
    )
    parser.add_argument(
        '-m', '--make-default', default=False, action='store_const', const=True,
        help='Set the created topology data as default',
    )
    parser.add_argument(
        '-o', '--output-directory', type=str,
        help='Directory name where to save the topology data. I.e.: my_topology. Default is the actual date/time '
             'in "--time-format" format.\n'
             'NOTE: the directory is a sub directory under "~/var/topology_data/"',
    )
    parser.add_argument(
        '-p', '--path-in-inventory', type=str,
        help=f'Checkmk inventory path to the topology data. I.e. {cdp_path}',
    )
    parser.add_argument(
        '-s', '--seed-devices', type=str, nargs='+',
        help=f'List of devices to start the topology discovery from. I.e. {sample_seeds}',
    )
    parser.add_argument(
        '-t', '--time-format', type=str,
        help='Format string to render the time. (default: %%Y-%%m-%%dT%%H:%%M:%%S.%%m)',
    )
    command_group.add_argument(
        '-u', '--uppercase', default=False, action='store_const', const=True,
        help='Change neighbour names to all upper case',
    )
    parser.add_argument(
        '-v', '--version', default=False, action='store_const', const=True,
        help='Print version of this script and exit',
    )
    return parser.parse_args()