#!/usr/bin/env python3

"""Basic console interface for running pre-rendered functions.

Supports either pre-rendering a function from a workspace or loading a pickled pre-render from 
the disk.
"""

import pickle
import sys
import time

from blc.workspace import Workspace, PreRenderable
from blc.audio import DefaultAudioPlayer 
from blc.output import LightingOutput

__version__ = "0.0.1"

class DummyOutput(LightingOutput):
    """Output that prints changed values to the console."""
    def set_values(self, values):
        for c,v in values:
            univ, addr = c
            if self.values[addr] != v:
                print("U%1d C%3d to %3d" % (univ, addr, v))
                self.values[addr] = v

    def __init__(self):
        self.values = {c: 0 for c in range(1,513)}

minnx = 10
aminnx = minnx/1000

print("PRCons v%s" % __version__)
print()
if len(sys.argv) == 3:
    w = Workspace.load(sys.argv[1])
    f = w.functions[int(sys.argv[2])]

    if not issubclass(type(f), PreRenderable):
        raise ValueError("Function must be PreRenderable!")

    print("Rendering...")
    render = f.render_all(minnx=minnx)
elif len(sys.argv) == 2:
    print("Loading from %s..." % sys.argv[1])
    with open(sys.argv[1], "rb") as f:
        render = pickle.load(f)
else:
    print("Usage: %s <workspace> <function id>" % sys.argv[0])
    print("       %s <pickled render>" % sys.argv[0])
    sys.exit(1)

input("Ready, press enter to start: ")
print("Starting show")
output = DummyOutput()
for n, (values, acues) in enumerate(render, 1):
    values = list(values)
    acues = list(acues)
    print("Step", n)
    start = time.monotonic()
    while values or acues:
        t = 1000*(time.monotonic() - start)
        while values and values[0][0] < t:
            output.set_values(values.pop(0)[1])
        while acues and acues[0][0] < t:
            fname = acues.pop(0)[2]
            print("Playing", fname)
            DefaultAudioPlayer(fname).play()
        time.sleep(aminnx)
    print("Done step", n)
    input("Press enter to continue: ")