#! /usr/bin/env python

import argparse

from commands import settings
from commands.utils import messages
from commands.utils.parse_actions import multi_set


def main():

    default_format = settings.get('git-settings.list.format', default='compact')

    file_parser = argparse.ArgumentParser(add_help=False)
    file_group = file_parser.add_mutually_exclusive_group()

    # --local
    file_group.add_argument(
        '--local',
        help='limit to the local config file',
        action='store_const',
        const='local',
        dest='config'
    )

    # --global
    file_group.add_argument(
        '--global',
        help='limit to the global config file',
        action='store_const',
        const='global',
        dest='config'
    )

    # --system
    file_group.add_argument(
        '--system',
        help='limit to the system config file',
        action='store_const',
        const='system',
        dest='config'
    )

    # --file <file>
    file_group.add_argument(
        '--file',
        help='limit to a specific config file',
        action=multi_set(config='file_'),
        dest='file_',
        metavar='FILE'
    )

    parser = argparse.ArgumentParser(
        prog='git settings',
        description=settings.__doc__,
        epilog='for more detail, use: git help settings'
    )
    parser.add_argument('-v', '--version', action='version', help='show version and exit', version='git-settings 0.7.0')
    subparsers = parser.add_subparsers(help='sub-command help', dest='subcommand')

    # --------------------------------------------
    # get sub-command
    # --------------------------------------------
    get_parser = subparsers.add_parser(
        'get',
        help='retrieve a configuration value',
        description='retrieve a configuration value',
        parents=[file_parser]
    )
    get_parser.add_argument(
        'key',
        help='a configuration key',
        metavar='KEY'
    )
    get_parser.add_argument(
        '-d',
        '--default',
        help='a default value to return when no setting exists'
    )
    get_parser.set_defaults(func=settings.get)

    # --------------------------------------------
    # list sub-command
    # --------------------------------------------
    list_parser = subparsers.add_parser(
        'list',
        help='list configuration settings respecting override precedence',
        description='list configuration settings respecting override precedence',
        parents=[file_parser]
    )
    list_parser.add_argument(
        'section',
        help='optionally, limit to a specific section',
        metavar='SECTION',
        nargs='?'
    )
    list_group = list_parser.add_mutually_exclusive_group()
    list_group.add_argument(
        '-f',
        '--format',
        help='print using a specific format (choices: compact, pretty)',
        choices=('compact', 'pretty'),
        default=default_format,
        dest='format_'
    )
    list_group.add_argument(
        '-p',
        '--pretty',
        help='use pretty format',
        action='store_const',
        const='pretty',
        dest='format_'
    )
    list_group.add_argument('-c', '--count', help='print the count of configurations', action='store_true')
    list_group.add_argument('-k', '--keys', help='list the keys for a section', action='store_const', const='keys', dest='limit_to')
    list_group.add_argument('-s', '--sections', help='list only section names', action='store_const', const='sections', dest='limit_to')
    list_parser.set_defaults(func=settings.list_)

    # --------------------------------------------
    # destroy sub-command
    # --------------------------------------------
    destroy_parser = subparsers.add_parser(
        'destroy',
        help='destroy a section from the local, global, and system config files',
        description='destroy a section from the local, global, and system config files'
    )
    destroy_parser.add_argument('section', help='a section', metavar='SECTION')
    destroy_parser.add_argument(
        '-d',
        '--dry-run',
        help='print which configurations would be destroyed rather than removing them',
        action='store_true'
    )
    destroy_parser.set_defaults(func=settings.destroy)

    args = vars(parser.parse_args())

    subcommand = args.pop('subcommand')
    if subcommand == 'list' and not args['section'] and args['limit_to'] and args['limit_to'] == 'keys':
        messages.error('argument -k/--keys: not allowed without positional argument section')

    func = args.pop('func')
    result = func(**args)
    if result:
        print result


if __name__ == '__main__':
    main()
