#! /usr/bin/env python
# tovid-stats

"""Print statistical summaries of videos encoded with tovid, from data stored
in ~/.tovid/stats.tovid.
"""

import os
import sys
from libtovid import stats
from libtovid.util import pretty_dict
from libtovid.render.drawing import Drawing, display
from libtovid.render.layer import Scatterplot

USAGE = \
"""This script gathers statistics from your ~/.tovid/stats.tovid"
file, and can display summary information or average values."

Usage:
    tovid-stats COMMANDS

COMMANDS may be any of:

    -count FIELD
        Count total occurrences of distinct values in FIELD
    -average FIELD_1 [by FIELD_2]
        Average FIELD_1, optionally sorted by FIELD_2
    -list FIELD_1 by FIELD_2
        Show all values of FIELD_1, organized by FIELD_2
    -plot FIELD_1 by FIELD_2
        Like -list, but show a scatterplot image of the data.
    -match FIELD VALUE
        Only display records where FIELD is VALUE. This option can
        be used to filter the results of subsequent options.
    -show 'FIELD [FIELD]...'
        Format and display the given fields for all records

FIELDs may be any of the following:

    format, tvsys, final_size, avg_bitrate, encoding_time, quant, kbpm

and many others. See 'man tovid-stats' for a full listing, and examples.
"""

STATFILE = os.path.expanduser("~/.tovid/stats.tovid")

def get_statlist(statfile):
    """Return a Statlist, created from the given tovid stats file.
    """
    # Open and read tovid stat file
    statfile = os.path.abspath(statfile)
    tempfile = "%s.csv" % statfile
    # Only use lines beginning with " (and not header line)
    filter_cmd = 'grep --color=never \'^ *\"[^T]\' > "%s" "%s"' % \
        (tempfile, statfile)
    os.system(filter_cmd)
    statlist = stats.Statlist(filename=tempfile)
    os.remove(tempfile)
    return statlist

if __name__ == '__main__':
    args = sys.argv[1:]
    if len(args) == 0:
        print(USAGE)
        sys.exit(0)

    # Get stats from the default tovid stat file
    statlist = get_statlist(STATFILE)

    # Parse command-line
    while args:
        arg = args.pop(0)

        if arg == '-count':
            field = args.pop(0)
            counts = statlist.count_unique(field)
            print("Total occurrences of each distinct value of '%s':" % field)
            print(pretty_dict(counts))
            print("Total of %s distinct values" % len(counts))

        elif arg == '-average':
            avg_field = args.pop(0)
            if args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                print("Average %s, sorted by %s:" % (avg_field, by_field))
                print(pretty_dict(statlist.average_by(avg_field, by_field)))
            else:
                print("Average %s:" % avg_field)
                print(statlist.average(avg_field))

        # Useful -list arguments:
        #    -list avg_bitrate by tgt_bitrate
        #    -list peak_bitrate by tgt_bitrate

        elif arg == '-list':
            list_field = args.pop(0)
            if args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                print("List of %ss by by %s:" % (list_field, by_field))
                print(pretty_dict(statlist.list_by(list_field, by_field, True)))
            else:
                print("Please use -list with a 'by' clause.")

        elif arg == '-plot':
            list_field = args.pop(0)
            if list_field not in stats.int_fields:
                print("The -plot option requires a numerical field.")
            elif args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                if by_field not in stats.FIELDS:
                    print("Invalid field name: %s" % by_field)
                    sys.exit(1)

                print("Generating a scatterplot of %ss by %s" % \
                      (list_field, by_field))
                xy_values = statlist.list_by(list_field, by_field, True)

                scatterplot = Scatterplot(xy_values, 640, 480,
                                          by_field, list_field)
                drawing = Drawing(800, 600)
                drawing.rectangle(0, 0, 800, 600)
                drawing.fill('white')
                drawing.translate(80, 60)
                scatterplot.draw(drawing, 1)
                print("Showing scatterplot. Press 'q' to close.")
                display(drawing, 800, 600)

        elif arg == '-match':
            field = args.pop(0)
            value = args.pop(0)
            if field in stats.int_fields:
                value = int(value)
            print("Finding statistics records where %s is %s" % (field, value))
            matches = statlist.get_matching(field, value)
            # Change the statlist to contain only the matched records
            statlist = stats.Statlist(records=matches)
            #for match in matches:
            #    print("Matching record:")
            #    print(pretty_dict(match))
            if matches == []:
                print("No matches found.")
            else:
                print("Found %s records where %s is %s." % \
                      (len(matches), field, value))

        elif arg == '-show':
            fields = args.pop(0)
            statlist.show('all', fields.split(' '))

def required_MB(format, tvsys, vbitrate, quant, seconds=1):
# Not working yet
    """Return the approximate number of Megabytes required to encode
    a video to the given format and tvsys, at the given video bitrate
    (in kbps) and quantization, for the given number of seconds. By default,
    returns MB per second of video.
    """
    statlist = get_statlist(STATFILE)

    # Determine final output size per second
    for record in statlist.records:
        if record['format'] == format and record['tvsys'] == tvsys and \
           record['tgt_bitrate'] == vbitrate:
            matches.append(record)

