/* Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)

Date:   Wed Aug 03 16:57:46 2016

Model author: Raphael Mannadiar
Model name:   Traffic_Light_JavaScript_Version
*/


// package "Traffic_Light_JavaScript_Version"
var Traffic_Light_JavaScript_Version = {};
(function() {

var MainApp = function(controller) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    
    // call user defined constructor
    MainApp.prototype.user_defined_constructor.call(this);
};
MainApp.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        MainApp.prototype[prop] = proto[prop];
    }
})();

MainApp.prototype.user_defined_constructor = function() {
    this.canvas	= ui.append_canvas(ui.window,100,310,{'background':'#eee'});
    var police_button = ui.append_button(ui.window, 'Police interrupt');
    var quit_button	= ui.append_button(ui.window, 'Quit');
    ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'police_interrupt_clicked');
    ui.bind_event(quit_button.element, 	 ui.EVENTS.MOUSE_CLICK, this.controller, 'quit_clicked');
};

MainApp.prototype.user_defined_destructor = function() {
};


// builds Statechart structure
MainApp.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /initializing
    this.states["/initializing"] = new State(1, this);
    
    // state /creating
    this.states["/creating"] = new State(2, this);
    
    // state /initialized
    this.states["/initialized"] = new State(3, this);
    
    // add children
    this.states[""].addChild(this.states["/initializing"]);
    this.states[""].addChild(this.states["/creating"]);
    this.states[""].addChild(this.states["/initialized"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/initializing"];
    
    // transition /initializing
    var _initializing_0 = new Transition(this, this.states["/initializing"], [this.states["/creating"]]);
    _initializing_0.setAction(this._initializing_0_exec);
    this.states["/initializing"].addTransition(_initializing_0);
    
    // transition /creating
    var _creating_0 = new Transition(this, this.states["/creating"], [this.states["/initialized"]]);
    _creating_0.setAction(this._creating_0_exec);
    _creating_0.trigger = new Event("instance_created", null);
    this.states["/creating"].addTransition(_creating_0);
};

MainApp.prototype._initializing_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("create_instance", null, [this, "trafficlight", "TrafficLight", this.canvas]));
};

MainApp.prototype._creating_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
};

MainApp.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/initializing"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'MainApp' to package 'Traffic_Light_JavaScript_Version'
Traffic_Light_JavaScript_Version.MainApp = MainApp;

var TrafficLight = function(controller, canvas) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    
    // call user defined constructor
    TrafficLight.prototype.user_defined_constructor.call(this, canvas);
};
TrafficLight.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        TrafficLight.prototype[prop] = proto[prop];
    }
})();

TrafficLight.prototype.user_defined_constructor = function(canvas) {
    var size 	= 100;
    var offset 	= size+5;
    this.RED 	= 0;
    this.YELLOW = 1;
    this.GREEN 	= 2;
    this.colors	= ['#f00','#ff0','#0f0']
    this.lights = [canvas.add_rectangle(size/2, size/2, 		 	 size, size, {'fill':'#000'}),
    					canvas.add_rectangle(size/2, size/2+offset,	 size, size, {'fill':'#000'}),
    					canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
};

TrafficLight.prototype.user_defined_destructor = function() {
};


// user defined method
TrafficLight.prototype.clear = function() {
    this.lights[this.RED].set_color('#000');
    this.lights[this.YELLOW].set_color('#000');
    this.lights[this.GREEN].set_color('#000');
};


// user defined method
TrafficLight.prototype.setGreen = function() {
    this.clear();
    this.lights[this.GREEN].set_color(this.colors[this.GREEN]);
};


// user defined method
TrafficLight.prototype.setYellow = function() {
    this.clear();
    this.lights[this.YELLOW].set_color(this.colors[this.YELLOW]);
};


// user defined method
TrafficLight.prototype.setRed = function() {
    this.clear();
    this.lights[this.RED].set_color(this.colors[this.RED]);
};


// builds Statechart structure
TrafficLight.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /on
    this.states["/on"] = new State(1, this);
    
    // state /on/normal
    this.states["/on/normal"] = new State(2, this);
    
    // state /on/normal/red
    this.states["/on/normal/red"] = new State(3, this);
    this.states["/on/normal/red"].setEnter(this._on_normal_red_enter);
    this.states["/on/normal/red"].setExit(this._on_normal_red_exit);
    
    // state /on/normal/green
    this.states["/on/normal/green"] = new State(4, this);
    this.states["/on/normal/green"].setEnter(this._on_normal_green_enter);
    this.states["/on/normal/green"].setExit(this._on_normal_green_exit);
    
    // state /on/normal/yellow
    this.states["/on/normal/yellow"] = new State(5, this);
    this.states["/on/normal/yellow"].setEnter(this._on_normal_yellow_enter);
    this.states["/on/normal/yellow"].setExit(this._on_normal_yellow_exit);
    
    // state /on/normal/history
    this.states["/on/normal/history"] = new ShallowHistoryState(6, this);
    
    // state /on/interrupted
    this.states["/on/interrupted"] = new State(7, this);
    
    // state /on/interrupted/yellow
    this.states["/on/interrupted/yellow"] = new State(8, this);
    this.states["/on/interrupted/yellow"].setEnter(this._on_interrupted_yellow_enter);
    this.states["/on/interrupted/yellow"].setExit(this._on_interrupted_yellow_exit);
    
    // state /on/interrupted/black
    this.states["/on/interrupted/black"] = new State(9, this);
    this.states["/on/interrupted/black"].setEnter(this._on_interrupted_black_enter);
    this.states["/on/interrupted/black"].setExit(this._on_interrupted_black_exit);
    
    // state /off
    this.states["/off"] = new State(10, this);
    this.states["/off"].setEnter(this._off_enter);
    
    // add children
    this.states[""].addChild(this.states["/on"]);
    this.states[""].addChild(this.states["/off"]);
    this.states["/on"].addChild(this.states["/on/normal"]);
    this.states["/on"].addChild(this.states["/on/interrupted"]);
    this.states["/on/normal"].addChild(this.states["/on/normal/red"]);
    this.states["/on/normal"].addChild(this.states["/on/normal/green"]);
    this.states["/on/normal"].addChild(this.states["/on/normal/yellow"]);
    this.states["/on/normal"].addChild(this.states["/on/normal/history"]);
    this.states["/on/interrupted"].addChild(this.states["/on/interrupted/yellow"]);
    this.states["/on/interrupted"].addChild(this.states["/on/interrupted/black"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/on"];
    this.states["/on"].default_state = this.states["/on/normal"];
    this.states["/on/normal"].default_state = this.states["/on/normal/red"];
    this.states["/on/interrupted"].default_state = this.states["/on/interrupted/yellow"];
    
    // transition /on/normal/red
    var _on_normal_red_0 = new Transition(this, this.states["/on/normal/red"], [this.states["/on/normal/green"]]);
    _on_normal_red_0.trigger = new Event("_0after");
    this.states["/on/normal/red"].addTransition(_on_normal_red_0);
    
    // transition /on/normal/green
    var _on_normal_green_0 = new Transition(this, this.states["/on/normal/green"], [this.states["/on/normal/yellow"]]);
    _on_normal_green_0.trigger = new Event("_1after");
    this.states["/on/normal/green"].addTransition(_on_normal_green_0);
    
    // transition /on/normal/yellow
    var _on_normal_yellow_0 = new Transition(this, this.states["/on/normal/yellow"], [this.states["/on/normal/red"]]);
    _on_normal_yellow_0.trigger = new Event("_2after");
    this.states["/on/normal/yellow"].addTransition(_on_normal_yellow_0);
    
    // transition /on/interrupted/yellow
    var _on_interrupted_yellow_0 = new Transition(this, this.states["/on/interrupted/yellow"], [this.states["/on/interrupted/black"]]);
    _on_interrupted_yellow_0.trigger = new Event("_3after");
    this.states["/on/interrupted/yellow"].addTransition(_on_interrupted_yellow_0);
    
    // transition /on/interrupted/black
    var _on_interrupted_black_0 = new Transition(this, this.states["/on/interrupted/black"], [this.states["/on/interrupted/yellow"]]);
    _on_interrupted_black_0.trigger = new Event("_4after");
    this.states["/on/interrupted/black"].addTransition(_on_interrupted_black_0);
    
    // transition /on
    var _on_0 = new Transition(this, this.states["/on"], [this.states["/off"]]);
    _on_0.trigger = new Event("quit_clicked", "ui");
    this.states["/on"].addTransition(_on_0);
    
    // transition /on/normal
    var _on_normal_0 = new Transition(this, this.states["/on/normal"], [this.states["/on/interrupted"]]);
    _on_normal_0.trigger = new Event("police_interrupt_clicked", "ui");
    this.states["/on/normal"].addTransition(_on_normal_0);
    
    // transition /on/interrupted
    var _on_interrupted_0 = new Transition(this, this.states["/on/interrupted"], [this.states["/on/normal/history"]]);
    _on_interrupted_0.trigger = new Event("police_interrupt_clicked", "ui");
    this.states["/on/interrupted"].addTransition(_on_interrupted_0);
};

TrafficLight.prototype._on_normal_red_enter = function() {
    this.addTimer(0, 3);
    this.setRed();
};

TrafficLight.prototype._on_normal_red_exit = function() {
    this.removeTimer(0);
};

TrafficLight.prototype._on_normal_green_enter = function() {
    this.addTimer(1, 2);
    this.setGreen();
};

TrafficLight.prototype._on_normal_green_exit = function() {
    this.removeTimer(1);
};

TrafficLight.prototype._on_normal_yellow_enter = function() {
    this.addTimer(2, 1);
    this.setYellow();
};

TrafficLight.prototype._on_normal_yellow_exit = function() {
    this.removeTimer(2);
};

TrafficLight.prototype._on_interrupted_yellow_enter = function() {
    this.addTimer(3, .5);
    this.setYellow();
};

TrafficLight.prototype._on_interrupted_yellow_exit = function() {
    this.removeTimer(3);
};

TrafficLight.prototype._on_interrupted_black_enter = function() {
    this.addTimer(4, .5);
    this.clear();
};

TrafficLight.prototype._on_interrupted_black_exit = function() {
    this.removeTimer(4);
};

TrafficLight.prototype._off_enter = function() {
    this.clear();
};

TrafficLight.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/on"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'TrafficLight' to package 'Traffic_Light_JavaScript_Version'
Traffic_Light_JavaScript_Version.TrafficLight = TrafficLight;

var ObjectManager = function(controller) {
    ObjectManagerBase.call(this, controller);
};
ObjectManager.prototype = new Object();
(function() {
    var proto = new ObjectManagerBase();
    for (prop in proto) {
        ObjectManager.prototype[prop] = proto[prop];
    }
})();

ObjectManager.prototype.instantiate = function(class_name, construct_params) {
    if (class_name === "MainApp") {
        var instance = new MainApp(this.controller);
        instance.associations = new Object();
        instance.associations["trafficlight"] = new Association("TrafficLight", 0, -1);
    } else if (class_name === "TrafficLight") {
        var instance = new TrafficLight(this.controller, construct_params[0]);
        instance.associations = new Object();
    }
    return instance;
};

// add symbol 'ObjectManager' to package 'Traffic_Light_JavaScript_Version'
Traffic_Light_JavaScript_Version.ObjectManager = ObjectManager;

var Controller = function(event_loop_callbacks, finished_callback) {
    if (finished_callback === undefined) finished_callback = null;
    EventLoopControllerBase.call(this, new ObjectManager(this), event_loop_callbacks, finished_callback);
    this.addInputPort("ui");
    this.object_manager.createInstance("MainApp", new Array());
};
Controller.prototype = new Object();
(function() {
    var proto = new EventLoopControllerBase();
    for (prop in proto) {
        Controller.prototype[prop] = proto[prop];
    }
})();

// add symbol 'Controller' to package 'Traffic_Light_JavaScript_Version'
Traffic_Light_JavaScript_Version.Controller = Controller;
})();