# Copyright 2016 - Nokia Corporation
#
# 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.

from cliff import command
from cliff import lister
from cliff import show

from vitrageclient.common import utils


# noinspection PyAbstractClass
class TemplateValidate(show.ShowOne):

    def get_parser(self, prog_name):
        parser = super(TemplateValidate, self).get_parser(prog_name)
        parser.add_argument('--path',
                            required=True,
                            help='full path for template file or templates dir'
                            )
        return parser

    @property
    def formatter_default(self):
        return 'json'

    def take_action(self, parsed_args):

        result = utils.get_client(self).template.validate(
            path=parsed_args.path)

        return self.dict2columns(result)


class TemplateList(lister.Lister):
    """Template list"""

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

    def take_action(self, parsed_args):
        templates = utils.get_client(self).template.list()
        # TODO(ikinory): add type to the table.
        return utils.list2cols_with_rename(
            (
                ('UUID', 'uuid'),
                ('Name', 'name'),
                ('Status', 'status'),
                ('Status details', 'status details'),
                ('Date', 'date'),
            ), templates)


class TemplateShow(show.ShowOne):
    """Template show"""

    def get_parser(self, prog_name):
        parser = super(TemplateShow, self).get_parser(prog_name)
        parser.add_argument('uuid', help='Template UUID')
        return parser

    @property
    def formatter_default(self):
        return 'json'

    def take_action(self, parsed_args):
        uuid = parsed_args.uuid
        template = utils.get_client(self).template.show(uuid=uuid)
        return self.dict2columns(template)


class TemplateAdd(lister.Lister):
    """Template add

    support 3 types of templates:
    standard, definition, equivalence
    """

    def get_parser(self, prog_name):
        parser = super(TemplateAdd, self).get_parser(prog_name)
        parser.add_argument('--path',
                            required=True,
                            help='full path for template file or templates dir'
                            )
        parser.add_argument('--type',
                            choices=['standard', 'definition', 'equivalence'],
                            help='Template type. Valid types:'
                                 '[standard, definition, equivalence]')
        return parser

    def take_action(self, parsed_args):
        path = parsed_args.path
        template_type = parsed_args.type
        templates = utils.get_client(self).template.add(
            path=path, template_type=template_type)
        return utils.list2cols_with_rename(
            (
                ('UUID', 'uuid'),
                ('Name', 'name'),
                ('Status', 'status'),
                ('Status details', 'status details'),
                ('Date', 'date'),
                ('Type', 'type'),
            ), templates)


class TemplateDelete(command.Command):
    """Template delete"""

    def get_parser(self, prog_name):
        parser = super(TemplateDelete, self).get_parser(prog_name)
        parser.add_argument('uuid', help='ID of a template')
        return parser

    @property
    def formatter_default(self):
        return 'json'

    def take_action(self, parsed_args):
        uuid = parsed_args.uuid
        utils.get_client(self).template.delete(uuid=uuid)

@@ -1621,56 +1621,8 @@
 t()%0A
-        # TODO(ikinory): add type to the table.%0A
     
@@ -1861,32 +1861,66 @@
 Date', 'date'),%0A
+                ('Type', 'type'),%0A
             ), t

add template type to template list cli