from typing import Counter


def analysis_to_str(data):
    """Converts a dictionary of analyzed file data to a formatted string for
    printing.

    Arguments:
        data (dict): The data to format.

    Returns:
        (str): A string representation of the data.
    """
    fmt = '{:32}, {:>15}, {};'
    sfmt = '\t{:28}, {:>15}, {};'
    lines = []
    for key, val in data.iteritems():
        if isinstance(val, list):
            lines.append('{}: {}'.format(key, len(val)))
            lines.extend(['\t' + i for i in val])

    for key, val in data.iteritems():
        if not isinstance(val, Counter) and not isinstance(val, list):
            lines.append(fmt.format(key, val, type(val).__name__))

    for key, val in data.iteritems():
        if isinstance(val, Counter):
            total = sum(val.values())
            lines.append(fmt.format(key, total, 'Counter'))

            # Get values in Counter. We sort the values by -count, then by key
            # name
            for k, v in ((k, val[k]) for k in sorted(val,
                                                     key=lambda x: (-val[x], x))):
                if isinstance(k, tuple):
                    lines.append(sfmt.format(' '.join(k), v, type(v).__name__))
                else:
                    lines.append(sfmt.format(k, v, type(v).__name__))

    return '\n'.join(lines)