#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
#

"""Network agent action implementations"""

import argparse
import logging
from collections.abc import Iterable, Sequence
from typing import Any

from cliff import columns as cliff_columns
from openstack.network.v2 import agent as _agent
from osc_lib.cli import format_columns
from osc_lib import exceptions
from osc_lib import utils

from openstackclient import command
from openstackclient.common import pagination
from openstackclient.i18n import _

LOG = logging.getLogger(__name__)


class AliveColumn(cliff_columns.FormattableColumn[bool]):
    def human_readable(self) -> str:
        return ":-)" if self._value else "XXX"


class AdminStateColumn(cliff_columns.FormattableColumn[bool]):
    def human_readable(self) -> str:
        return 'UP' if self._value else 'DOWN'


_formatters = {
    'is_alive': AliveColumn,
    'alive': AliveColumn,
    'admin_state_up': AdminStateColumn,
    'is_admin_state_up': AdminStateColumn,
    'configurations': format_columns.DictColumn,
}


def _get_network_columns(
    item: _agent.Agent,
) -> tuple[tuple[str, ...], tuple[str, ...]]:
    column_data_mapping = {
        'admin_state_up': 'is_admin_state_up',
        'agent_type': 'agent_type',
        'alive': 'is_alive',
        'availability_zone': 'availability_zone',
        'binary': 'binary',
        'configuration': 'configuration',
        'created_at': 'created_at',
        'description': 'description',
        'ha_chassis_priority': 'ha_chassis_priority',
        'ha_state': 'ha_state',
        'host': 'host',
        'id': 'id',
        'last_heartbeat_at': 'last_heartbeat_at',
        'resources_synced': 'resources_synced',
        'started_at': 'started_at',
        'topic': 'topic',
    }
    return (
        tuple(column_data_mapping.keys()),
        tuple(column_data_mapping.values()),
    )


class AddNetworkToAgent(command.Command):
    _description = _("Add network to an agent")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--dhcp',
            action='store_true',
            help=_('Add network to a DHCP agent'),
        )
        parser.add_argument(
            'agent_id',
            metavar='<agent-id>',
            help=_('Agent to which a network is added (ID only)'),
        )
        parser.add_argument(
            'network',
            metavar='<network>',
            help=_('Network to be added to an agent (name or ID)'),
        )

        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        agent = client.get_agent(parsed_args.agent_id)
        network = client.find_network(
            parsed_args.network, ignore_missing=False
        )
        if parsed_args.dhcp:
            try:
                client.add_dhcp_agent_to_network(agent, network)
            except Exception:
                msg = f'Failed to add {network.name} to {agent.agent_type}'
                exceptions.CommandError(msg)


class AddRouterToAgent(command.Command):
    _description = _("Add router to an agent")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--l3', action='store_true', help=_('Add router to an L3 agent')
        )
        parser.add_argument(
            '--ha-chassis-priority',
            metavar='<ha-chassis-priority>',
            type=int,
            help=_(
                "HA Chassis priority, ranging from [0, 32767]. "
                "Only used with --l3 and for ML2/OVN L3 agents"
            ),
        )
        parser.add_argument(
            'agent_id',
            metavar='<agent-id>',
            help=_("Agent to which a router is added (ID only)"),
        )
        parser.add_argument(
            'router',
            metavar='<router>',
            help=_("Router to be added to an agent (name or ID)"),
        )

        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        agent = client.get_agent(parsed_args.agent_id)
        router = client.find_router(parsed_args.router, ignore_missing=False)
        if parsed_args.l3:
            client.add_router_to_agent(
                agent,
                router,
                ha_chassis_priority=parsed_args.ha_chassis_priority,
            )


class DeleteNetworkAgent(command.Command):
    _description = _("Delete network agent(s)")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            'network_agent',
            metavar="<network-agent>",
            nargs='+',
            help=(_("Network agent(s) to delete (ID only)")),
        )
        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        result = 0

        for agent in parsed_args.network_agent:
            try:
                client.delete_agent(agent, ignore_missing=False)
            except Exception as e:
                result += 1
                LOG.error(
                    _(
                        "Failed to delete network agent with "
                        "ID '%(agent)s': %(e)s"
                    ),
                    {'agent': agent, 'e': e},
                )

        if result > 0:
            total = len(parsed_args.network_agent)
            msg = _(
                "%(result)s of %(total)s network agents failed to delete."
            ) % {'result': result, 'total': total}
            raise exceptions.CommandError(msg)


class ListNetworkAgent(command.Lister):
    _description = _("List network agents")
    _supported_agents = {
        'bgp': 'BGP dynamic routing agent',
        'dhcp': 'DHCP agent',
        'open-vswitch': 'Open vSwitch agent',
        'linux-bridge': 'Linux bridge agent',
        'ofa': 'OFA driver agent',
        'l3': 'L3 agent',
        'loadbalancer': 'Loadbalancer agent',
        'metering': 'Metering agent',
        'metadata': 'Metadata agent',
        'macvtap': 'Macvtap agent',
        'nic': 'NIC Switch agent',
        'baremetal': 'Baremetal Node',
        'ovn-controller': 'OVN Controller agent',
        'ovn-controller-gateway': 'OVN Controller Gateway agent',
        'ovn-metadata': 'OVN Metadata agent',
        'ovn-agent': 'OVN Neutron agent',
    }

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        supported_agents = ','.join(self._supported_agents.keys())
        parser.add_argument(
            '--agent-type',
            metavar='<agent-type>',
            choices=list(self._supported_agents.keys()),
            help=_(
                "List only agents with the specified agent type. "
                "The supported agent types are: %(supported_agents)s."
            )
            % {'supported_agents': supported_agents},
        )
        parser.add_argument(
            '--host',
            metavar='<host>',
            help=_("List only agents running on the specified host"),
        )
        agent_type_group = parser.add_mutually_exclusive_group()
        agent_type_group.add_argument(
            '--network',
            metavar='<network>',
            help=_('List agents hosting the specified network (name or ID)'),
        )
        agent_type_group.add_argument(
            '--router',
            metavar='<router>',
            help=_('List agents hosting the specified router (name or ID)'),
        )
        parser.add_argument(
            '--long',
            action='store_true',
            default=False,
            help=_("List additional fields in output"),
        )
        pagination.add_marker_pagination_option_to_parser(parser)

        return parser

    def take_action(
        self, parsed_args: argparse.Namespace
    ) -> tuple[tuple[str, ...], Iterable[tuple[Any, ...]]]:
        client = self.app.client_manager.network
        columns: tuple[str, ...] = (
            'id',
            'agent_type',
            'host',
            'availability_zone',
            'is_alive',
            'is_admin_state_up',
            'binary',
        )
        column_headers: tuple[str, ...] = (
            'ID',
            'Agent Type',
            'Host',
            'Availability Zone',
            'Alive',
            'State',
            'Binary',
        )

        filters = {}
        if parsed_args.marker is not None:
            filters['marker'] = parsed_args.marker
        if parsed_args.limit is not None:
            filters['limit'] = parsed_args.limit
        if parsed_args.max_items is not None:
            filters['max_items'] = parsed_args.max_items

        data: list[_agent.Agent]
        if parsed_args.network is not None:
            network = client.find_network(
                parsed_args.network, ignore_missing=False
            )
            data = list(client.network_hosting_dhcp_agents(network))
        elif parsed_args.router is not None:
            if parsed_args.long:
                columns += ('ha_state',)
                column_headers += ('HA State',)
            router = client.find_router(
                parsed_args.router, ignore_missing=False
            )
            data = list(client.routers_hosting_l3_agents(router))
        else:
            if parsed_args.agent_type is not None:
                filters['agent_type'] = self._supported_agents[
                    parsed_args.agent_type
                ]
            if parsed_args.host is not None:
                filters['host'] = parsed_args.host

            data = list(client.agents(**filters))

        return (
            column_headers,
            (
                utils.get_item_properties(
                    s,
                    columns,
                    formatters=_formatters,
                )
                for s in data
            ),
        )


class RemoveNetworkFromAgent(command.Command):
    _description = _("Remove network from an agent.")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--dhcp',
            action='store_true',
            help=_('Remove network from DHCP agent'),
        )
        parser.add_argument(
            'agent_id',
            metavar='<agent-id>',
            help=_('Agent to which a network is removed (ID only)'),
        )
        parser.add_argument(
            'network',
            metavar='<network>',
            help=_('Network to be removed from an agent (name or ID)'),
        )
        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        agent = client.get_agent(parsed_args.agent_id)
        network = client.find_network(
            parsed_args.network, ignore_missing=False
        )
        if parsed_args.dhcp:
            try:
                client.remove_dhcp_agent_from_network(agent, network)
            except Exception:
                msg = f'Failed to remove {network.name} to {agent.agent_type}'
                exceptions.CommandError(msg)


class RemoveRouterFromAgent(command.Command):
    _description = _("Remove router from an agent")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--l3',
            action='store_true',
            help=_('Remove router from an L3 agent'),
        )
        parser.add_argument(
            'agent_id',
            metavar='<agent-id>',
            help=_("Agent from which router will be removed (ID only)"),
        )
        parser.add_argument(
            'router',
            metavar='<router>',
            help=_("Router to be removed from an agent (name or ID)"),
        )

        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        agent = client.get_agent(parsed_args.agent_id)
        router = client.find_router(parsed_args.router, ignore_missing=False)
        if parsed_args.l3:
            client.remove_router_from_agent(agent, router)


class SetNetworkAgentRouter(command.Command):
    _description = _("Set properties of a router associated to an agent")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--ha-chassis-priority',
            metavar='<ha-chassis-priority>',
            type=int,
            required=True,
            help=_(
                "HA Chassis priority, ranging from [0, 32767]. "
                "Only used with --l3 and for ML2/OVN L3 agents"
            ),
        )
        parser.add_argument(
            'agent_id',
            metavar='<agent-id>',
            help=_("L3 agent to modify (ID only)"),
        )
        parser.add_argument(
            'router',
            metavar='<router>',
            help=_("Router to update (name or ID)"),
        )

        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        agent = client.get_agent(parsed_args.agent_id)
        router = client.find_router(parsed_args.router, ignore_missing=False)
        client.update_router_in_agent(
            agent,
            router,
            ha_chassis_priority=parsed_args.ha_chassis_priority,
        )


class SetNetworkAgent(command.Command):
    _description = _("Set network agent properties")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            'network_agent',
            metavar="<network-agent>",
            help=(_("Network agent to modify (ID only)")),
        )
        parser.add_argument(
            '--description',
            metavar='<description>',
            help=_("Set network agent description"),
        )
        admin_group = parser.add_mutually_exclusive_group()
        admin_group.add_argument(
            '--enable', action='store_true', help=_("Enable network agent")
        )
        admin_group.add_argument(
            '--disable', action='store_true', help=_("Disable network agent")
        )
        return parser

    def take_action(self, parsed_args: argparse.Namespace) -> None:
        client = self.app.client_manager.network
        obj = client.get_agent(parsed_args.network_agent)
        attrs = {}
        if parsed_args.description is not None:
            attrs['description'] = parsed_args.description
        if parsed_args.enable:
            attrs['is_admin_state_up'] = True
            attrs['admin_state_up'] = True
        if parsed_args.disable:
            attrs['is_admin_state_up'] = False
            attrs['admin_state_up'] = False
        client.update_agent(obj, **attrs)


class ShowNetworkAgent(command.ShowOne):
    _description = _("Display network agent details")

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        parser.add_argument(
            'network_agent',
            metavar="<network-agent>",
            help=(_("Network agent to display (ID only)")),
        )
        return parser

    def take_action(
        self, parsed_args: argparse.Namespace
    ) -> tuple[Sequence[str], Iterable[Any]]:
        client = self.app.client_manager.network
        obj = client.get_agent(parsed_args.network_agent)
        display_columns, columns = _get_network_columns(obj)
        data = utils.get_item_properties(
            obj,
            columns,
            formatters=_formatters,
        )
        return display_columns, data
