"""
CLI tool for managing connections between nodes of a Pluto instance.
Supports connecting and disconnecting nodes.
"""

# ----------------------------------------------------------------------------------------------------------------------

from argparse import ArgumentParser
from logging import Formatter, Logger, StreamHandler, getLogger
from os import environ
from sys import argv
from sys import exit as sys_exit

from ..client.client import NewHorizonClient  # pylint: disable=relative-beyond-top-level

# ----------------------------------------------------------------------------------------------------------------------


def cli():
    """Entry point for the plyto_admin_connect CLI tool."""
    #
    # -----------------------------------------------------
    #
    logger: Logger = getLogger()
    log_level: str = environ.get("PLYTO_LOG_LEVEL", "INFO")
    logger.setLevel(log_level)
    logging_handler: StreamHandler = StreamHandler()
    logging_handler.setFormatter(Formatter())
    logger.addHandler(logging_handler)
    #
    # -----------------------------------------------------
    #
    parser: ArgumentParser = ArgumentParser()
    parser.add_argument(
        "-c",
        "--connect",
        action="store_true",
    )
    parser.add_argument(
        "-d",
        "--disconnect",
        action="store_true",
    )
    parser.add_argument(
        "-s",
        "--source",
        type=str,
        default=None,
    )
    parser.add_argument(
        "-t",
        "--target",
        type=str,
        default=None,
    )
    parser.add_argument(
        "-i",
        "--ip-address",
        type=str,
        default=None,
    )
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=None,
    )
    args = parser.parse_args(argv[1 : len(argv)])
    #
    # -----------------------------------------------------
    #
    client: NewHorizonClient
    if args.connect and args.disconnect:
        logger.error("Only one operation at a time is allowed.")
        sys_exit(-1)
    elif args.connect:
        client = NewHorizonClient(logger.getChild(NewHorizonClient.__name__)).connect(
            address=args.ip_address,
            port=args.port,
        )
        client.manager().connect_nodes(
            src=args.source,
            dest=args.target,
        )
        client.close()
    elif args.disconnect:
        client = NewHorizonClient(logger.getChild(NewHorizonClient.__name__)).connect(
            address=args.ip_address,
            port=args.port,
        )
        client.manager().disconnect_nodes(
            src=args.source,
            dest=args.target,
        )
        client.close()
    else:
        logger.error("Nothing to do...")
        sys_exit(-1)
    sys_exit(0)


if __name__ == "__main__":
    cli()

# ----------------------------------------------------------------------------------------------------------------------
