#!/usr/bin/python
<%
var i, j,
        outerIf = 'if',
        innerIf = 'if',
        initialStateName;

for (i = 0; i < stateMachine.states.length; i += 1) {
    if (stateMachine.states[i].id === stateMachine.initialState) {
        initialStateName = stateMachine.states[i].name;
        break;
    }
}

%>
def main():
    final_states = []
<%for (i = 0; i < stateMachine.finalStates.length; i += 1) {%>    final_states.append('<%=stateMachine.finalStates[i]%>')
<%}%>
    current_state_id = '<%=stateMachine.initialState%>'
    current_state = '<%=initialStateName%>'

    current_input = ''
    while current_input != 'exit':
        print('Current state: {0} ({1})'.format(current_state, current_state_id))
        current_input = raw_input('Enter an event: ')

<%for (i = 0; i < stateMachine.states.length; i += 1) {
    var eventStr = '';
    if (stateMachine.states[i].transitions.length === 0) {
        continue;
    }
    innerIf = 'if';%>        <%=outerIf%> current_state_id == '<%=stateMachine.states[i].id%>':
<%for (j = 0; j < stateMachine.states[i].transitions.length; j += 1) {
    var transition = stateMachine.states[i].transitions[j];
    eventStr += ', ' + transition.event;
%>            <%=innerIf%> current_input == '<%=transition.event%>':
                print('Switching state to <%=transition.targetName%> (<%=transition.targetId%>)')
                current_state_id = '<%=transition.targetId%>'
                current_state = '<%=transition.targetName%>'
<%if (j === stateMachine.states[i].transitions.length - 1) {%>            else:
                print('Possible events for transition(s): <%=eventStr.substring(2)%>')<%}
innerIf = 'elif';}%>
<%outerIf = 'elif';}%>
        # Break the loop at a final state.
        if current_state_id in final_states:
            print('At a final state {0} ({1})'.format(current_state, current_state_id))
            break

    current_input = raw_input('Press enter to exit')


if __name__ == '__main__':
    main()