#!/usr/bin/python3
# This file was automatically generated from drawio2cbd with the command:
#   /home/red/git/DrawioConvert/__main__.py StateEventLocation.xml -fav -F CBD -e Experiment -t 100 -E delta=0.1

from CBD.lib.plotting import SignalPlotBlock
from CBD.simulator import Simulator
from StateEventLocation import *
import matplotlib.pyplot as plt
import matplotlib.animation as animation

DELTA_T = 0.1

fig = plt.figure(figsize=(15, 5), dpi=100)
ax1 = fig.add_subplot(1, 1, 1)
ax2 = ax1.twinx()

cbd = Experiment("Experiment", ax1, ax2)

plotter1 = cbd.getBlockByName("valueplot")
plotter2 = cbd.getBlockByName("eventplot")

# opened = True
# def term(*_):
# 	global opened
# 	plt.draw()
# 	plt.pause(0.001)
# 	return not opened

# def animate(_, p):
# 	px = p.state["x"]
# 	py = p.state["y"]
# 	p.elem.set_data(px, py)
# 	try:
# 		p.elem.axes.set_xlim((min(px), max(px)))
# 		p.elem.axes.set_ylim((min(py), max(py)))
# 	except: pass
# 	return p.elem,


# Run the Simulation
sim = Simulator(cbd)
sim.setRealTime(True)
# sim.setTerminationCondition(term)

# def handle_close(evt):
# 	global opened
# 	opened = False
# fig.canvas.mpl_connect('close_event', handle_close)
# plt.ion()
# plt.show()

# ani1 = animation.FuncAnimation(fig, animate, fargs=(plotter1,), interval=200)
# ani2 = animation.FuncAnimation(fig, animate, fargs=(plotter2,), interval=200)

# plotter1.set_animation(fig)
# plotter2.set_animation(fig)
# plotter1.start_animation(fig)
sim.run(20.0)

# plt.ioff()
# plotter1.end_animation()
# plotter2.end_animation()


log = sim.getDurationLog()

fig2 = plt.figure()
ax2 = fig2.subplots()
ax2.bar(range(len(log)), log)
plt.show()
