#! /usr/bin/env python

import argparse

from commands import settings, state
from utils.parse_actions import optional_list


def main():

    # status defaults
    default_show_status = settings.get('git-state.status.show', default=True, as_type=settings.as_bool)

    # log defaults
    show_log = settings.get('git-state.log.show', default=True, as_type=settings.as_bool)
    if show_log:
        default_log_count = settings.get('git-state.log.count', default=10, as_type=int)
    else:
        default_log_count = 0

    # reflog defaults
    show_reflog = settings.get('git-state.reflog.show', default=True, as_type=settings.as_bool)
    if show_reflog:
        default_reflog_count = settings.get('git-state.reflog.count', default=5, as_type=int)
    else:
        default_reflog_count = 0

    # branches defaults
    default_show_branches = settings.get('git-state.branches.show', default=True, as_type=settings.as_bool)

    # stashes defaults
    default_show_stashes = settings.get('git-state.stashes.show', default=True, as_type=settings.as_bool)

    # general defaults
    default_show_empty = settings.get('git-state.show-empty', default=False, as_type=settings.as_bool)
    default_format = settings.get('git-state.format', default='compact')
    default_show_color = settings.get('color.ui', default='auto')
    default_clear = settings.get('git-state.clear', default=True, as_type=settings.as_bool)

    parser = argparse.ArgumentParser(
        prog='git state',
        version='git-state 0.4.0',
        description=state.__doc__,
        epilog='for more detail, use: git help state'
    )

    # status
    status_group = parser.add_mutually_exclusive_group()
    status_group.add_argument(
        '-s',
        '--status',
        help='show the status section',
        action='store_true',
        dest='show_status',
        default=default_show_status
    )
    status_group.add_argument(
        '-S',
        '--no-status',
        help="don't show the status section",
        action='store_false',
        dest='show_status'
    )

    # log
    log_group = parser.add_mutually_exclusive_group()
    log_group.add_argument(
        '-l',
        '--log',
        help='limit the commits in the log section',
        type=int,
        dest='log_count',
        metavar='<count>',
        default=default_log_count
    )
    log_group.add_argument(
        '-L',
        '--no-log',
        help="don't show the log section",
        action='store_const',
        const=0,
        dest='log_count'
    )
    log_group.add_argument(
        '--full-log',
        help='show the full log',
        action='store_const',
        const=-1,
        dest='log_count'
    )

    # reflog
    reflog_group = parser.add_mutually_exclusive_group()
    reflog_group.add_argument(
        '-r',
        '--reflog',
        help='limit the reflog section',
        type=int,
        dest='reflog_count',
        metavar='<count>',
        default=default_reflog_count
    )
    reflog_group.add_argument(
        '-R',
        '--no-reflog',
        help="don't show the reflog section",
        action='store_const',
        const=0,
        dest='reflog_count'
    )
    reflog_group.add_argument(
        '--full-reflog',
        help='show the full reflog',
        action='store_const',
        const=-1,
        dest='reflog_count'
    )

    # branches
    branches_group = parser.add_mutually_exclusive_group()
    branches_group.add_argument(
        '-b',
        '--branches',
        help='show the branches section',
        action='store_true',
        dest='show_branches',
        default=default_show_branches
    )
    branches_group.add_argument(
        '-B',
        '--no-branches',
        help="don't show the branches section",
        action='store_false',
        dest='show_branches'
    )

    # stashes
    stashes_group = parser.add_mutually_exclusive_group()
    stashes_group.add_argument(
        '-t',
        '--stashes',
        help='show the stashes section',
        action='store_true',
        dest='show_stashes',
        default=default_show_stashes
    )
    stashes_group.add_argument(
        '-T',
        '--no-stashes',
        help="don't show the stashes section",
        action='store_false',
        dest='show_stashes'
    )

    # color
    color_group = parser.add_mutually_exclusive_group()
    color_group.add_argument(
        '-c',
        '--color',
        help='always color output',
        const='always',
        dest='show_color',
        nargs='?',
        choices=('always', 'never', 'auto'),
        default=default_show_color
    )
    color_group.add_argument(
        '-C',
        '--no-color',
        help='never color output',
        action='store_const',
        const='never',
        dest='show_color'
    )

    # format
    format_group = parser.add_mutually_exclusive_group()
    format_group.add_argument(
        '-f',
        '--format',
        help='format the each section (choices: compact, pretty)',
        choices=('pretty', 'compact'),
        metavar='<format>',
        dest='format',
        default=default_format
    )
    format_group.add_argument(
        '-p',
        '--pretty',
        help='show in pretty format',
        action='store_const',
        const='pretty',
        dest='format'
    )

    # show empty
    show_empty_group = parser.add_mutually_exclusive_group()
    show_empty_group.add_argument(
        '-e',
        '--show-empty',
        help='show empty sections',
        action='store_true',
        dest='show_empty',
        default=default_show_empty
    )
    show_empty_group.add_argument(
        '-E',
        '--no-show-empty',
        help="don't show empty sections (does not apply to status)",
        action='store_false',
        dest='show_empty'
    )

    # screen clearing
    clear_group = parser.add_mutually_exclusive_group()
    clear_group.add_argument(
        '--clear',
        help='clear the screen before printing',
        action='store_true',
        dest='clear',
        default=default_clear
    )
    clear_group.add_argument(
        '--no-clear',
        help='do not clear the screen before printing',
        action='store_false',
        dest='clear'
    )

    # ignore sections
    parser.add_argument(
        '--ignore-extensions',
        help='a list of extensions to ignore',
        nargs='*',
        action=optional_list(settings.list(
            'git-state.extensions',
            config=None,
            count=False,
            keys=True,
            format=None
        ).splitlines()),
        default=[]
    )

    parser.add_argument(
        '-o',
        '--order',
        help='custom section order',
        nargs='+',
        default=argparse.SUPPRESS
    )

    state.state(**vars(parser.parse_args()))

if __name__ == '__main__':
    main()
