'''
Created on 10-sep.-2014

@author: Bruno
'''
#from bokeh.plotting import figure, output_file, show
from fsaclasses import State, Transition, After, Float, FSAModel, Event, RunTimeEvent, Events
from fsa_cbd_woven_classes import CBDState
from CBDMultipleOutput.models.HarmonicOscilator import CircleCBDDerivative
from woven_cbd_fsa_simulator import Controller
from Options import Options
from models.bouncingball import BouncingBallFSA
import os
from woven_cbd_fsa_lib import FSACBDLib

CBD_DELTA = 0.01

def buildModel():
    statea = State("a")
    stateb = State("b")
    statec = CBDState("c", CircleCBDDerivative("Harmonic Oscilator", CBD_DELTA))
    #statec = State("c")
    stated = State("d")
    statef = State("f", True)
    
    transitionab = Transition("a2b",statea,stateb)
    transitionab.trigger = After(Float(5.0))
    
    transitionbc = Transition("b2c",stateb,statec)
    transitionbc.trigger = After(Float(2.0))

    transitioncd = Transition("c2d",statec,stated)
    transitioncd.trigger = Event("c")

    transitiondf = Transition("d2f",stated,statef)
    transitiondf.trigger = After(Float(5.0))

    fsamodel = FSAModel(
                        [statea,stateb,statec,stated],
                        [transitionab,transitionbc,transitioncd,transitiondf]
                        )
    fsamodel.initialState = statea
    return fsamodel

if __name__ == '__main__':
    
    if os.path.exists(FSACBDLib.OUTPUT_EVENTS):
        os.remove(FSACBDLib.OUTPUT_EVENTS)
    if os.path.exists(FSACBDLib.OUTPUT_SIGNALS):
        os.remove(FSACBDLib.OUTPUT_SIGNALS)
    
    options = Options(delta = CBD_DELTA)
    options.setMaxIterations(10000);
    fsamodel = BouncingBallFSA()
    events = Events([RunTimeEvent('kick',13.0), RunTimeEvent('kick',23.0), RunTimeEvent('kick',37.0) ,RunTimeEvent('enough',60.0)])
    #events = Events([RunTimeEvent('enough',25.0)])
    controller = Controller(options, fsamodel, events, keep_running=False)
    controller.start()
    
    '''
    cbdModel = fsamodel.states[2].cbd;
    
    times = []
    values = []
    for timeValuePair in cbdModel.getSignal("neg"):
        times.append(timeValuePair.time)
        values.append(timeValuePair.value)
        
    output_file("./plot.html", title="Plot")
    p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
    p.line(times, values, legend="Something", line_width=1, line_color="red")
    show(p)
    '''