"""Tool to print menhir's configuration and state dict.

This can be used to inspect what the configuration a previous tool
produces, or the next tool sees.

It can output in python pprint, json or yaml formats.
"""
import argparse
import logging
import pprint

import json
import yaml

from menhir.tool import Tool
from menhir.tool_utils import OK, FAIL

log = logging.getLogger(__name__)


def tool():
    return Pprint()


class Pprint(Tool):

    def dir_info(tool, path, info):
        return {
            'project_recognised': False,
            'can_run': True,
        }

    def dependencies(tool, path):
        return []

    def arg_parser(tool, **kwargs):
        return parser(**kwargs)

    def execute_tool(tool, path, info, args):
        """Pprint the menhir config."""
        if args.format == 'pprint':
            pprint.pprint(info, width=80)
        elif args.format == 'yaml':
            print(yaml.dump(
                info,
                width=80,
                indent=2,
                default_flow_style=False,
            ))
        elif args.format == 'json':
            print(json.dumps(info, sort_keys=True, indent=4))
        else:
            log.error('Invalid output format: "%s', args.format)
            return FAIL
        return OK


def parser(**kwargs):
    """Configure arg parser for the tool options and arguments."""
    parser = argparse.ArgumentParser(
        description="Pretty print the menhir configuration.",
        **kwargs
    )
    parser.add_argument(
        "--format",
        default='yaml',
        choices=['json', 'pprint', 'yaml'],
        help="Output format.",
    )
    return parser
