# Copyright (c) 2013 Mirantis Inc.
#
# 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.
import ast
import logging

from cliff import command
from cliff.formatters import table
from cliff import lister
from cliff import show

from blazarclient import exception
from blazarclient import utils

HEX_ELEM = '[0-9A-Fa-f]'
UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}',
                         HEX_ELEM + '{4}', HEX_ELEM + '{4}',
                         HEX_ELEM + '{12}'])


class OpenStackCommand(command.Command):
    """Base class for OpenStack commands."""

    api = None

    def run(self, parsed_args):
        if not self.api:
            return
        else:
            return super(OpenStackCommand, self).run(parsed_args)

    def get_data(self, parsed_args):
        pass

    def take_action(self, parsed_args):
        return self.get_data(parsed_args)


class TableFormatter(table.TableFormatter):
    """This class is used to keep consistency with prettytable 0.6."""

    def emit_list(self, column_names, data, stdout, parsed_args):
        if column_names:
            super(TableFormatter, self).emit_list(column_names, data, stdout,
                                                  parsed_args)
        else:
            stdout.write('\n')


class BlazarCommand(OpenStackCommand):

    """Base Blazar CLI command."""
    api = 'reservation'
    log = logging.getLogger(__name__ + '.BlazarCommand')
    values_specs = []
    json_indent = None
    resource = None
    allow_names = True
    name_key = None
    id_pattern = UUID_PATTERN

    def __init__(self, app, app_args):
        super(BlazarCommand, self).__init__(app, app_args)

        # NOTE(dbelova): This is no longer supported in cliff version 1.5.2
        # the same moment occurred in Neutron:
        # see https://bugs.launchpad.net/python-neutronclient/+bug/1265926

        # if hasattr(self, 'formatters'):
        #     self.formatters['table'] = TableFormatter()

    def get_client(self):
        # client_manager.reservation is used for osc_lib, and should be used
        # if it exists
        if hasattr(self.app, 'client_manager'):
            return self.app.client_manager.reservation
        else:
            return self.app.client

    def get_parser(self, prog_name):
        parser = super(BlazarCommand, self).get_parser(prog_name)
        return parser

    def format_output_data(self, data, parsed_args):
        # Do not format output data if the formatter is not table
        if parsed_args.formatter != 'table':
            return
        for k, v in data.items():
            if isinstance(v, str):
                try:
                    # Deserialize if possible into dict, lists, tuples...
                    v = ast.literal_eval(v)
                except SyntaxError:
                    # NOTE(sbauza): This is probably a datetime string, we need
                    #               to keep it unchanged.
                    pass
                except ValueError:
                    # NOTE(sbauza): This is not something AST can evaluate,
                    #               probably a string.
                    pass
            elif isinstance(v, list) or isinstance(v, dict):
                value = utils.dumps(v, indent=self.json_indent)
                data[k] = value
            elif v is None:
                data[k] = ''

    def add_known_arguments(self, parser):
        pass

    def args2body(self, parsed_args):
        return {}


class CreateCommand(BlazarCommand, show.ShowOne):
    """Create resource with passed args."""

    api = 'reservation'
    resource = None
    log = None

    def get_data(self, parsed_args):
        self.log.debug('get_data(%s)' % parsed_args)
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.create(**body)
        self.format_output_data(data, parsed_args)

        if data:
            print('Created a new %s:' % self.resource, file=self.app.stdout)
        else:
            data = {'': ''}
        return list(zip(*sorted(data.items())))


class UpdateCommand(BlazarCommand):
    """Update resource's information."""

    api = 'reservation'
    resource = None
    log = None

    def get_parser(self, prog_name):
        parser = super(UpdateCommand, self).get_parser(prog_name)
        if self.allow_names:
            help_str = 'ID or name of %s to update'
        else:
            help_str = 'ID of %s to update'
        parser.add_argument(
            'id', metavar=self.resource.upper(),
            help=help_str % self.resource
        )
        self.add_known_arguments(parser)
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        if self.allow_names:
            res_id = utils.find_resource_id_by_name_or_id(blazar_client,
                                                          self.resource,
                                                          parsed_args.id,
                                                          self.name_key,
                                                          self.id_pattern)
        else:
            res_id = parsed_args.id
        resource_manager = getattr(blazar_client, self.resource)
        resource_manager.update(res_id, **body)
        print('Updated %s: %s' % (self.resource, parsed_args.id),
              file=self.app.stdout)
        return


class DeleteCommand(BlazarCommand):
    """Delete a given resource."""

    api = 'reservation'
    resource = None
    log = None

    def get_parser(self, prog_name):
        parser = super(DeleteCommand, self).get_parser(prog_name)
        if self.allow_names:
            help_str = 'ID or name of %s to delete'
        else:
            help_str = 'ID of %s to delete'
        parser.add_argument(
            'id', metavar=self.resource.upper(),
            help=help_str % self.resource)
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        blazar_client = self.get_client()
        resource_manager = getattr(blazar_client, self.resource)
        if self.allow_names:
            res_id = utils.find_resource_id_by_name_or_id(blazar_client,
                                                          self.resource,
                                                          parsed_args.id,
                                                          self.name_key,
                                                          self.id_pattern)
        else:
            res_id = parsed_args.id
        resource_manager.delete(res_id)
        print('Deleted %s: %s' % (self.resource, parsed_args.id),
              file=self.app.stdout)
        return


class ListCommand(BlazarCommand, lister.Lister):
    """List resources that belong to a given tenant."""

    api = 'reservation'
    resource = None
    log = None
    _formatters = {}
    list_columns = []
    long_columns = []
    unknown_parts_flag = True
    _filters = []

    def args2body(self, parsed_args):
        params = {}
        if parsed_args.sort_by:
            if parsed_args.sort_by in self.list_columns:
                params['sort_by'] = parsed_args.sort_by
            elif self.long_columns and parsed_args.sort_by in self.long_columns:
                params['sort_by'] = parsed_args.sort_by
            else:
                msg = 'Invalid sort option %s' % parsed_args.sort_by
                raise exception.BlazarClientException(msg)
        return params

    def get_parser(self, prog_name):
        parser = super(ListCommand, self).get_parser(prog_name)
        if self.long_columns:
            parser.add_argument(
                '--long',
                action='store_true',
                help='Display detailed information for each item'
            )
        return parser

    def retrieve_list(self, parsed_args):
        """Retrieve a list of resources from Blazar server."""
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.list(**body)
        for f in self._filters:
            data = list(filter(f, data))
        return data

    def setup_columns(self, info, parsed_args):
        """
        Determines the list of columns that may be visible to the client. This may not
        be the columns that are actually visible to the client on the output of their
        command. The output columns are determined by a process in cliff.display which
        compares the parsed_args to the list output by this function.
        """
        # return empty list when info is empty
        # or sort all keys that are in all the networks
        columns = (
            len(info) > 0 and sorted({k for d in info for k in d.keys()}) or []
        )
        if parsed_args.columns:
            valid_parsed_columns = {col for col in parsed_args.columns if col in columns}
        else:
            valid_parsed_columns = set()

        default_columns = self.list_columns
        if self.long_columns and parsed_args.long:
            default_columns += self.long_columns
        if default_columns:
            columns = {
                col for col in default_columns if col in columns
            } | valid_parsed_columns
            # sort the columns based on list_columns
            sorting_map = {item: i for i, item in enumerate(default_columns)}
            # sort key is either index of item in list_columns, or placed at end of list
            columns = sorted(columns, key=lambda x: sorting_map.get(x, len(default_columns)))

        return (
            columns,
            (utils.get_item_properties(s, columns, formatters=self._formatters)
             for s in info)
        )

    def get_data(self, parsed_args):
        self.log.debug('get_data(%s)' % parsed_args)
        data = self.retrieve_list(parsed_args)
        return self.setup_columns(data, parsed_args)


class ListAllocationCommand(ListCommand, lister.Lister):
    """List allocations that belong to a given tenant."""

    def get_parser(self, prog_name):
        parser = super(ListAllocationCommand, self).get_parser(prog_name)
        parser.add_argument(
            '--lease-id', metavar="<lease_id>",
            help='Lease to filter allocations by',
        )
        parser.add_argument(
            '--username', metavar="<username>",
            help='Username to filter allocations by',
        )
        return parser

    def retrieve_list(self, parsed_args):
        """Retrieve a list of resources from Blazar server."""
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.list_allocations(**body)
        filters = []
        if parsed_args.lease_id:
            filters.append(lambda x: x['lease_id'] == parsed_args.lease_id)
        if parsed_args.username:
            filters.append(lambda x: x.get("extras", {}).get('user_name') == parsed_args.username)

        if filters:
            new_data = []
            for allocation in data:
                new_reservations = []
                for reservation in allocation["reservations"]:
                    if all(f(reservation) for f in filters):
                        new_reservations.append(reservation)
                if new_reservations:
                    new_data.append({
                        "resource_id": allocation["resource_id"],
                        "reservations": new_reservations,
                    })
            data = new_data
        return data


class ShowCommand(BlazarCommand, show.ShowOne):
    """Show information of a given resource."""

    api = 'reservation'
    resource = None
    log = None

    def get_parser(self, prog_name):
        parser = super(ShowCommand, self).get_parser(prog_name)
        return parser

    def get_data(self, parsed_args):
        self.log.debug('get_data(%s)' % parsed_args)
        body = self.args2body(parsed_args)
        blazar_client = self.get_client()

        if self.allow_names:
            res_id = utils.find_resource_id_by_name_or_id(blazar_client,
                                                          self.resource,
                                                          parsed_args.id,
                                                          self.name_key,
                                                          self.id_pattern)
        else:
            res_id = parsed_args.id

        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.get(res_id)
        if body.get('detail'):
            data.update(resource_manager.additional_details(res_id))
        self.format_output_data(data, parsed_args)
        return list(zip(*sorted(data.items())))


class ShowAllocationCommand(ShowCommand, show.ShowOne):
    """Show allocations for a given resource."""

    def get_parser(self, prog_name):
        parser = super(ShowAllocationCommand, self).get_parser(prog_name)
        if self.allow_names:
            help_str = 'ID or name of %s to show allocations'
        else:
            help_str = 'ID of %s to show allocations'
        parser.add_argument(
            'id', metavar=self.resource.upper(),
            help=help_str % self.resource)
        return parser

    def get_data(self, parsed_args):
        self.log.debug('get_data(%s)' % parsed_args)
        blazar_client = self.get_client()
        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.get_allocation(parsed_args.id)
        self.format_output_data(data, parsed_args)
        return list(zip(*sorted(data.items())))


class ReallocateCommand(BlazarCommand):
    """Reallocate host from current leases."""

    api = 'reservation'
    resource = None
    log = None

    def get_parser(self, prog_name):
        parser = super(ReallocateCommand, self).get_parser(prog_name)
        if self.allow_names:
            help_str = 'ID or name of %s to update'
        else:
            help_str = 'ID of %s to update'
        parser.add_argument(
            'id', metavar=self.resource.upper(),
            help=help_str % self.resource
        )
        self.add_known_arguments(parser)
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        if self.allow_names:
            res_id = utils.find_resource_id_by_name_or_id(blazar_client,
                                                          self.resource,
                                                          parsed_args.id,
                                                          self.name_key,
                                                          self.id_pattern)
        else:
            res_id = parsed_args.id
        resource_manager = getattr(blazar_client, self.resource)
        resource_manager.reallocate(res_id, body)
        print('Reallocated %s: %s' % (self.resource, parsed_args.id),
              file=self.app.stdout)
        return


class ShowPropertyCommand(BlazarCommand, show.ShowOne):
    """Show information of a given resource."""

    api = 'reservation'
    resource = None
    log = None

    def get_parser(self, prog_name):
        parser = super(ShowPropertyCommand, self).get_parser(prog_name)
        parser.add_argument('property_name', metavar='PROPERTY_NAME',
                            help='Name of property.')
        return parser

    def get_data(self, parsed_args):
        self.log.debug('get_data(%s)' % parsed_args)
        blazar_client = self.get_client()
        resource_manager = getattr(blazar_client, self.resource)
        data = resource_manager.get_property(parsed_args.property_name)
        return list(zip(*sorted(data.items())))


class UpdatePropertyCommand(BlazarCommand):
    api = 'reservation'
    resource = None
    log = None

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        blazar_client = self.get_client()
        body = self.args2body(parsed_args)
        resource_manager = getattr(blazar_client, self.resource)
        resource_manager.set_property(**body)
        print(
            'Updated %s property: %s' % (
                self.resource, parsed_args.property_name),
            file=self.app.stdout)
        return

    def get_parser(self, prog_name):
        parser = super(UpdatePropertyCommand, self).get_parser(prog_name)
        parser.add_argument(
            'property_name', metavar='PROPERTY_NAME',
            help='Name of property to patch.'
        )
        parser.add_argument(
            '--private',
            action='store_true',
            default=False,
            help='Set property to private.'
        )
        parser.add_argument(
            '--public',
            action='store_true',
            default=False,
            help='Set property to public.'
        )
        parser.add_argument(
            '--unique',
            action='store_true',
            default=False,
            help='Set capability as unique.'
        )
        return parser

    def args2body(self, parsed_args):
        return dict(
            property_name=parsed_args.property_name,
            is_unique=(parsed_args.unique is True),
            private=(parsed_args.private is True))
