<?xml version="1.0" ?>
<!--
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtScxml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
-->
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
       name="Pinball" datamodel="ecmascript">
    <datamodel>
        <data id="highScore" expr="0"/>
        <data id="score" expr="0"/>
    </datamodel>
    <parallel id="global">
        <parallel id="guiControl">
            <state id="cLight">
                <state id="cLightOn">
                    <transition event="turnOffC" target="cLightOff"/>
                </state>
                <state id="cLightOff">
                    <transition event="turnOnC" target="cLightOn"/>
                </state>
            </state>
            <state id="rLight">
                <state id="rLightOn">
                    <transition event="turnOffR" target="rLightOff"/>
                </state>
                <state id="rLightOff">
                    <transition event="turnOnR" target="rLightOn"/>
                </state>
            </state>
            <state id="aLight">
                <state id="aLightOn">
                    <transition event="turnOffA" target="aLightOff"/>
                </state>
                <state id="aLightOff">
                    <transition event="turnOnA" target="aLightOn"/>
                </state>
            </state>
            <state id="zLight">
                <state id="zLightOn">
                    <transition event="turnOffZ" target="zLightOff"/>
                </state>
                <state id="zLightOff">
                    <transition event="turnOnZ" target="zLightOn"/>
                </state>
            </state>
            <state id="yLight">
                <state id="yLightOn">
                    <transition event="turnOffY" target="yLightOff"/>
                </state>
                <state id="yLightOff">
                    <transition event="turnOnY" target="yLightOn"/>
                </state>
            </state>
            <state id="hurryLight">
                <state id="hurryLightOn">
                    <transition event="turnOffHurry" target="hurryLightOff"/>
                </state>
                <state id="hurryLightOff">
                    <transition event="turnOnHurry" target="hurryLightOn"/>
                </state>
            </state>
            <state id="jackpotLight">
                <state id="jackpotLightOn">
                    <transition event="turnOffJackpot" target="jackpotLightOff"/>
                </state>
                <state id="jackpotLightOff">
                    <transition event="turnOnJackpot" target="jackpotLightOn"/>
                </state>
            </state>
            <state id="gameOverLight">
                <state id="gameOverLightOn">
                    <transition event="turnOffGameOver" target="gameOverLightOff"/>
                </state>
                <state id="gameOverLightOff">
                    <transition event="turnOnGameOver" target="gameOverLightOn"/>
                </state>
            </state>
        </parallel>

        <parallel id="internalState">
            <parallel id="logicalState">
                <state id="letterState">
                    <parallel id="lettersState">
                        <state id="letter.C">
                            <state id="cLetterOff">
                                <transition event="cLetterTriggered" cond="In('onState')" target="cLetterOn"/>
                            </state>
                            <final id="cLetterOn"/>
                        </state>
                        <state id="letter.R">
                            <state id="rLetterOff">
                                <transition event="rLetterTriggered" cond="In('onState')" target="rLetterOn"/>
                            </state>
                            <final id="rLetterOn"/>
                        </state>
                        <state id="letter.A">
                            <state id="aLetterOff">
                                <transition event="aLetterTriggered" cond="In('onState')" target="aLetterOn"/>
                            </state>
                            <final id="aLetterOn"/>
                        </state>
                        <state id="letter.Z">
                            <state id="zLetterOff">
                                <transition event="zLetterTriggered" cond="In('onState')" target="zLetterOn"/>
                            </state>
                            <final id="zLetterOn"/>
                        </state>
                        <state id="letter.Y">
                            <state id="yLetterOff">
                                <transition event="yLetterTriggered" cond="In('onState')" target="yLetterOn"/>
                            </state>
                            <final id="yLetterOn"/>
                        </state>
                        <transition event="resetLetters" target="lettersState"/>
                    </parallel>
                </state>
                <state id="modeState">
                    <state id="offState">
                        <onentry>
                            <if cond="highScore &lt; score">
                                <assign location="highScore" expr="score"/>
                            </if>
                            <raise event="resetLetters"/>
                            <raise event="update"/>
                        </onentry>
                        <transition event="startTriggered" target="onState"/>
                    </state>
                    <parallel id="onState">
                        <onentry>
                            <assign location="score" expr="0"/>
                        </onentry>
                        <state id="hurryState">
                            <state id="hurryStateOff">
                                <onentry>
                                    <raise event="resetLetters"/>
                                    <raise event="update"/>
                                </onentry>
                                <transition event="goToHurryOn" target="hurryStateOn"/>
                            </state>
                            <state id="hurryStateOn">
                                <onentry>
                                    <send event="goToHurryOff" id="hurryId" delay="5s"/>
                                    <raise event="resetLetters"/>
                                    <raise event="update"/>
                                </onentry>
                                <transition event="goToHurryOff" target="hurryStateOff"/>
                                <onexit>
                                    <cancel sendid="hurryId"/>
                                </onexit>
                            </state>
                        </state>
                        <state id="jackpotState">
                            <state id="jackpotStateOff">
                                <onentry>
                                    <raise event="update"/>
                                </onentry>
                                <transition event="goForJackpot" target="jackpotStateOn"/>
                            </state>
                            <state id="jackpotStateOn">
                                <onentry>
                                    <raise event="update"/>
                                </onentry>
                            </state>
                        </state>
                        <transition event="ballOutTriggered" target="offState"/>
                    </parallel>
                </state>
            </parallel>

            <state id="workflow">
                <state id="lightImpulseGenerator">
                    <state id="lightImpulseOn"/>
                    <state id="lightImpulseOff"/>

                    <onentry>
                        <raise event="update"/>
                    </onentry>

                    <transition event="scheduleNewImpulse">
                        <cancel sendid="lightId"/>
                        <if cond="In('offState')">
                            <send event="lightImpulse" id="lightId" delay="1s"/>
                        <elseif cond="In('hurryStateOff')"/>
                            <send event="lightImpulse" id="lightId" delay="500ms"/>
                        <else/>
                            <send event="lightImpulse" id="lightId" delay="200ms"/>
                        </if>
                    </transition>

                    <transition event="update">
                        <raise event="scheduleNewImpulse"/>
                        <raise event="updateLights"/>
                    </transition>

                    <transition event="lightImpulse" cond="In('lightImpulseOn')" target="lightImpulseOff"/>
                    <transition event="lightImpulse" cond="In('lightImpulseOff')" target="lightImpulseOn"/>
                </state>

                <transition event="done.state.letter.*">
                    <if cond="In('hurryStateOff')">
                        <assign location="score" expr="score + 1000"/>
                    <elseif cond="In('hurryStateOn')"/>
                        <assign location="score" expr="score + 10000"/>
                    </if>
                    <raise event="updateLights"/>
                </transition>

                <transition event="done.state.lettersState">
                    <if cond="In('hurryStateOff')">
                        <assign location="score" expr="score + 100000"/>
                        <raise event="goToHurryOn"/>
                    <elseif cond="In('hurryStateOn')"/>
                        <assign location="score" expr="score + 1000000"/>
                        <raise event="goToHurryOff"/>
                        <raise event="goForJackpot"/>
                    </if>
                </transition>

                <transition event="updateLights">
                    <send event="updateScore">
                        <param name="highScore" expr="highScore"/>
                        <param name="score" expr="score"/>
                    </send>
                    <if cond="In('jackpotStateOn')">
                        <raise event="turnOnJackpot"/>
                    <else/>
                        <raise event="turnOffJackpot"/>
                    </if>

                    <if cond="In('lightImpulseOn')">
                        <if cond="In('offState')">
                            <raise event="turnOnLights"/>
                            <raise event="turnOnHurry"/>
                            <raise event="turnOnJackpot"/>
                            <raise event="turnOnGameOver"/>
                        <elseif cond="In('hurryStateOff')"/>
                            <raise event="updateLightsAccordingToLettersState"/>
                            <raise event="turnOffHurry"/>
                            <raise event="turnOffGameOver"/>
                        <else/>
                            <raise event="turnOnLights"/>
                            <raise event="turnOnHurry"/>
                            <raise event="turnOffGameOver"/>
                        </if>
                    <else/>
                        <if cond="In('offState')">
                            <raise event="turnOffLights"/>
                            <raise event="turnOffHurry"/>
                            <raise event="turnOffJackpot"/>
                        <elseif cond="In('hurryStateOff')"/>
                            <raise event="turnOffLights"/>
                        <else/>
                            <raise event="updateLightsAccordingToLettersState"/>
                        </if>
                        <raise event="turnOffHurry"/>
                        <raise event="turnOffGameOver"/>
                    </if>
                </transition>

                <transition event="updateLightsAccordingToLettersState">
                    <if cond="In('cLetterOn')">
                        <raise event="turnOnC"/>
                    <else/>
                        <raise event="turnOffC"/>
                    </if>
                    <if cond="In('rLetterOn')">
                        <raise event="turnOnR"/>
                    <else/>
                        <raise event="turnOffR"/>
                    </if>
                    <if cond="In('aLetterOn')">
                        <raise event="turnOnA"/>
                    <else/>
                        <raise event="turnOffA"/>
                    </if>
                    <if cond="In('zLetterOn')">
                        <raise event="turnOnZ"/>
                    <else/>
                        <raise event="turnOffZ"/>
                    </if>
                    <if cond="In('yLetterOn')">
                        <raise event="turnOnY"/>
                    <else/>
                        <raise event="turnOffY"/>
                    </if>
                </transition>

                <transition event="turnOnLights">
                    <raise event="turnOnC"/>
                    <raise event="turnOnR"/>
                    <raise event="turnOnA"/>
                    <raise event="turnOnZ"/>
                    <raise event="turnOnY"/>
                </transition>

                <transition event="turnOffLights">
                    <raise event="turnOffC"/>
                    <raise event="turnOffR"/>
                    <raise event="turnOffA"/>
                    <raise event="turnOffZ"/>
                    <raise event="turnOffY"/>
                </transition>
            </state>
        </parallel>
    </parallel>
</scxml>
