#!/usr/bin/env python

""" User registers commands with CommandGroups """

import os
import sys
from collections import OrderedDict

from knack import CLI
from knack.commands import CLICommandsLoader, CommandGroup
from knack.arguments import ArgumentsContext
from knack.help import CLIHelp

from knack.help_files import helps

cli_name = os.path.basename(__file__)

helps['abc'] = """
type: group
short-summary: Manage the alphabet of words.
"""

helps['abc list'] = """
type: command
short-summary: List the alphabet.
examples:
    - name: It's pretty straightforward.
      text: {cli_name} abc list
""".format(cli_name=cli_name)

helps['abc first'] = """
type: command
short-summary: List the first several letters in the alphabet.
examples:
    - name: Show the list of abc
      text: {cli_name} abc first --number 3
""".format(cli_name=cli_name)

helps['abc last'] = """
type: command
short-summary: List the last several letters in the alphabet.
examples:
    - name: Show the list of xyz
      text: {cli_name} abc last --number 3
""".format(cli_name=cli_name)

helps['ga'] = """
type: group
short-summary: A general available command group
"""

helps['pre'] = """
type: group
short-summary: A preview command group
"""

helps['exp'] = """
type: group
short-summary: An experimental command group
"""


def abc_show_command_handler():
    """
    Show a JSON mapping of letters to their ASCII values
    """
    import string
    lower = {}
    for ch in string.ascii_lowercase:
        lower[ch] = ord(ch)
    upper = {}
    for ch in string.ascii_uppercase:
        upper[ch] = ord(ch)
    return {"lowercase": lower, "uppercase": upper}


def abc_list_command_handler():
    import string
    return list(string.ascii_lowercase)


def abc_first_command_handler(number=5):
    import string
    return list(string.ascii_lowercase)[0:number]


def abc_last_command_handler(number=5):
    import string
    return list(string.ascii_lowercase)[-number:]


def range_command_handler(start=0, end=5):
    """
    Get a list of natural numbers from start to end
    :param start: the lower bound
    :param end: the higher bound
    :return:
    """
    return list(range(int(start), int(end) + 1))


def sample_json_handler():
    """
    Get a sample JSON string
    """
    # https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/list#examples
    result = {
        "id": "/subscriptions/291bba3f-e0a5-47bc-a099-3bdcb2a50a05",
        "subscriptionId": "291bba3f-e0a5-47bc-a099-3bdcb2a50a05",
        "tenantId": "31c75423-32d6-4322-88b7-c478bdde4858",
        "displayName": "Example Subscription",
        "state": "Enabled",
        "subscriptionPolicies": {
            "locationPlacementId": "Internal_2014-09-01",
            "quotaId": "Internal_2014-09-01",
            "spendingLimit": "Off"
        },
        "authorizationSource": "RoleBased",
        "managedByTenants": [
            {
                "tenantId": "8f70baf1-1f6e-46a2-a1ff-238dac1ebfb7"
            }
        ]
    }
    return result


def hello_command_handler(greetings=None):
    """
    Say "Hello World!" and my warm greetings
    :param greetings: My warm greetings
    """
    return ['Hello World!', greetings]


WELCOME_MESSAGE = r"""
   _____ _      _____ 
  / ____| |    |_   _|
 | |    | |      | |  
 | |    | |      | |  
 | |____| |____ _| |_ 
  \_____|______|_____|
                      
                      
Welcome to the cool new CLI!
"""


class MyCLIHelp(CLIHelp):

    def __init__(self, cli_ctx=None):
        super(MyCLIHelp, self).__init__(cli_ctx=cli_ctx,
                                        privacy_statement='My privacy statement.',
                                        welcome_message=WELCOME_MESSAGE)


class MyCommandsLoader(CLICommandsLoader):

    def load_command_table(self, args):
        with CommandGroup(self, '', '__main__#{}') as g:
            g.command('hello', 'hello_command_handler', confirmation=True)
            g.command('sample-json', 'sample_json_handler')
        with CommandGroup(self, 'abc', '__main__#{}') as g:
            g.command('list', 'abc_list_command_handler')
            g.command('show', 'abc_show_command_handler')
            g.command('get', 'abc_show_command_handler', deprecate_info=g.deprecate(redirect='show', hide='1.0.0'))
            g.command('first', 'abc_first_command_handler', is_preview=True)
            g.command('last', 'abc_last_command_handler', is_experimental=True)

        # A GA command group
        with CommandGroup(self, 'ga', '__main__#{}') as g:
            g.command('range', 'range_command_handler')

        # A preview command group
        with CommandGroup(self, 'pre', '__main__#{}', is_preview=True) as g:
            g.command('first', 'abc_first_command_handler', is_preview=True)
            g.command('range', 'range_command_handler')

        # An experimental command group
        with CommandGroup(self, 'exp', '__main__#{}', is_experimental=True) as g:
            g.command('range', 'range_command_handler')

        # A deprecated command group
        with CommandGroup(self, 'dep', '__main__#{}', deprecate_info=g.deprecate(redirect='ga', hide='1.0.0')) as g:
            g.command('range', 'range_command_handler')
        return super(MyCommandsLoader, self).load_command_table(args)

    def load_arguments(self, command):
        with ArgumentsContext(self, 'ga range') as ac:
            ac.argument('start', type=int, is_preview=True)
            ac.argument('end', type=int, is_experimental=True)
        super(MyCommandsLoader, self).load_arguments(command)


class MyCLI(CLI):

    def get_cli_version(self):
        return '0.1.0'


mycli = MyCLI(cli_name=cli_name,
              config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(cli_name))),
              config_env_var_prefix=cli_name,
              commands_loader_cls=MyCommandsLoader,
              help_cls=MyCLIHelp)
exit_code = mycli.invoke(sys.argv[1:])
sys.exit(exit_code)
