# Path: foos/process.py
# def call_and_log(*args, **kwargs):
#     """Run process, gather output and write to log"""
#     p = subprocess.Popen(*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
#     stdout, stderr = p.communicate()
#     if len(stdout) > 0:
#         logger.info(stdout.decode("utf-8").strip())
#     if len(stderr) > 0:
#         logger.error(stderr.decode("utf-8").strip())
#     if p.returncode != 0:
#         logger.error("{} returned {}".format(p.args, p.returncode))
# 
# Path: foos/platform.py
# def is_pi():
#     return pi3d.PLATFORM == pi3d.PLATFORM_PI


import foos.config as config
import os
import time
from foos.process import call_and_log
from foos.platform import is_pi


class Plugin:
    def __init__(self, bus):
        self.bus = bus
        bus.subscribe_map({'replay_request': lambda d: self.replay('long', 'manual', {}),
                           'score_goal': lambda d: self.replay('short', 'goal', d)},
                          thread=True)

    def replay(self, replay_type, trigger, extra={}):
        extra['type'] = trigger

        call_and_log(["video/generate-replay.sh", config.replay_path,
              str(config.ignore_recent_chunks),
              str(config.long_chunks), str(config.short_chunks)])
        self.bus.notify('replay_start', extra)
        if is_pi():

# Path: foos/process.py
# def call_and_log(*args, **kwargs):
#     """Run process, gather output and write to log"""
#     p = subprocess.Popen(*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
#     stdout, stderr = p.communicate()
#     if len(stdout) > 0:
#         logger.info(stdout.decode("utf-8").strip())
#     if len(stderr) > 0:
#         logger.error(stderr.decode("utf-8").strip())
#     if p.returncode != 0:
#         logger.error("{} returned {}".format(p.args, p.returncode))
# 
# Path: foos/platform.py
# def is_pi():
#     return pi3d.PLATFORM == pi3d.PLATFORM_PI


# Path: plugins/replay.py
import foos.config as config
import os
import time
from foos.process import call_and_log
from foos.platform import is_pi



class Plugin:
    def __init__(self, bus):
        self.bus = bus
        bus.subscribe_map({'replay_request': lambda d: self.replay('long', 'manual', {}),
                           'score_goal': lambda d: self.replay('short', 'goal', d)},
                          thread=True)

    def replay(self, replay_type, trigger, extra={}):
        extra['type'] = trigger

        call_and_log(["video/generate-replay.sh", config.replay_path,
              str(config.ignore_recent_chunks),
              str(config.long_chunks), str(config.short_chunks)])
        self.bus.notify('replay_start', extra)
        if is_pi():

            call_and_log(["video/replay.sh", os.path.join(config.replay_path, "replay_{}.h264".format(replay_type)), str(config.replay_fps)])