###########################################################################
# overlay-example.py
#
# Monterey Bay Aquarium Research Institute   2019
# All rights reserved.
#
# Scaffold for reading lcm log files from mesobot project and displaying
# stereo images with data overlay.
#
#
# Created:  Dec 2019      Paul Roberts
#
###########################################################################


import time
import sys
import lcm
from managers import OverlayManager

"""
loop_log_file - Read and display events from lcm log file
"""
def loop_log_file(filepath, event_manager=None, publish_lcm=False):

    log = lcm.EventLog(filepath, "r")
    lc = lcm.LCM()

    try:
        for event in log:
            if event_manager is not None:
                event_manager.add_event(event)
            if publish_lcm:
                lc.publish(event.channel, event.data)

    except KeyboardInterrupt:
        pass


"""
Main entry point when called from the command line
"""
if __name__=="__main__":

    print("LCM Log Overlay Example:")

    if len(sys.argv) < 2:
        print("Please input log file path as first argument")
        exit()

    # Create the manager to handle log file events and render images. In this
    # case it's just a simple image display with overlay
    aux_channels = ['EXT_TARGET_1']
    mgr = OverlayManager('DSPL_LEFT', 'DSPL_RIGHT', aux_channels=aux_channels, fps=10, log_rate=1)
    #TODO: # add input args for these paramers and handler using argparse (or similar)

    loop_log_file(sys.argv[1], mgr, publish_lcm=True)


