<?xml version="1.0" encoding="UTF-8"?>
<process xmlns:blue="http://www.e-marson.com/bluelemon">

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/pin/NewStartupInstruction</name>
    <version>1.0</version>
    <description>
      The instruction generated by this process will 
      be used as a new STARTUP_INSTRUCTION if a user decides
      to have a PIN number set for the application.
      
      It asks a user to specify a 4-digits PIN number.
      If the PIN is OK, user is forwarded to the moBank MainScreen.
      User has three chances to make a mistake. After the third
      wrong PIN the whole STARTUP_INSTRUCTION is changed to call
      AuthenticationScreen in mobank package and user
      has to log in specifying the userId and password.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>
  
  <path>
    
    <!-- ASSOCIATIONS --->

    <!-- association to exit from MIDlet --->
    <subprocess name="action for 'Exit'">
      <source>com/emarson/lemonade/templates/ExitFromApplication</source>
      <resultTo>exitAction</resultTo>
    </subprocess>
    
    <!-- association to AuthenticationScreen in mobank package --->
    <action name="go to the authentication screen">
      <type>general/bluelemon/code</type>
      <definition resultTo="goToAuthenticationScreen">
        <blue:call node="com/emarson/lemonade/examples/mobank/AuthenticationScreen"
                         location="@com/emarson/lemonade/examples/mobank"/>

      </definition>
    </action>
    
    
    <!-- INSTRUCTION --->
    
    <!-- 
      Let's create the action that will test if the PIN number specified
      by the user is legal and either forward to the main screen
      or increment number of errors and show the warning.
    --->

    <action name="action for OK">
      <type>general/bluelemon/code</type>
      <definition resultTo="okAction">

        <!-- get the instance of the screen which is currently being shown --->
        <blue:call manager="GeneralManager" method="getCurrent"
                   toVariable="form"
                   castTo="com/emarson/lemonade/bluelemon/midp1/Form"/>


        <!-- get its first item... --->
        <blue:call object="form" method="getItem"
                   toVariable="pinField"
                   castTo="com/emarson/lemonade/bluelemon/midp1/TextField">

          <blue:argument type="byte" value="0"/>
        </blue:call>
        <!-- ...and read its value --->
        <blue:call object="pinField" method="getString"
                   toVariable="pin"/>

        
        <!-- get the value of the PIN from the permanent location --->
        <blue:get node="com/emarson/lemonade/examples/mobank/pin/PinValue"
                  location="permanent" toVariable="correctPin"/>

        
        <!-- those two should match --->
        <blue:call manager="UtilsManager" method="equalValues"
                   toVariable="pinsMatch">

          <blue:argument type="variable" name="pin"/>
          <blue:argument type="variable" name="correctPin"/>
        </blue:call>
        
        <!-- if they do... --->
        <blue:if isTrue="pinsMatch">
        
          <!-- ...in case of any mistake earlier, remove mistakes counter --->
          <blue:set node="com/emarson/lemonade/examples/mobank/pin/MistakesCounter"
                    location="permanent">

            <blue:argument type="byte" value="0"/>
          </blue:set>
          
          <!-- and forward to the main screen --->
          <blue:call node="com/emarson/lemonade/examples/mobank/MainScreen"
                         location="@com/emarson/lemonade/examples/mobank"/>

        
        </blue:if>
        
        <!-- otherwise... --->
        <blue:else>
        
          <!-- get the mistakes counter to know which mistake it is --->
          <blue:get node="com/emarson/lemonade/examples/mobank/pin/MistakesCounter"
                    location="permanent" toVariable="mistakesCounter"/>

                   
          <!-- check if we have two mistakes already --->
          <blue:call manager="UtilsManager" method="=="
                     toVariable="thisIsTheThirdMistake">

            <blue:argument type="variable" name="mistakesCounter"/>
            <blue:argument type="byte" value="2"/>
          </blue:call>
                    
          <!-- if this is the 3rd mistake...  --->
          <blue:if isTrue="thisIsTheThirdMistake">
          
            <!-- update STARTUP_INSTRUCTION --->
            <blue:set node="STARTUP_INSTRUCTION" location="permanent">
              <blue:argument type="byteArr" contextKey="goToAuthenticationScreen"/>
            </blue:set>
            
            <!-- remove PIN --->
            <blue:call manager="ResourcesManager" method="remove">
              <blue:argument type="node" location="permanent"
                name="com/emarson/lemonade/examples/mobank/pin/PinValue"/>

            </blue:call>
            
            <!-- remove MistakesCounter --->
            <blue:call manager="ResourcesManager" method="remove">
              <blue:argument type="node" location="permanent"
                name="com/emarson/lemonade/examples/mobank/pin/MistakesCounter"/>

            </blue:call>
            
            <!-- forward user to the log in screen --->
            <blue:call node="com/emarson/lemonade/examples/mobank/AuthenticationScreen"
                         location="@com/emarson/lemonade/examples/mobank"/>

            
            <!-- and show him the error message --->
            <!-- create the instance of the alert --->
              <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Alert"
                              toVariable="alert"/>

    
            <!-- 
             set its title and text, mark this is an error alert,
             don't allow it to disappear
            --->

             <blue:call object="alert" method="initialize">
               <blue:argument type="String" value="PIN error"/>
               <blue:argument type="String" 
                   value="Three wrong PIN numbers. Please log in."/>

               <blue:argument type="byte" value="AlertType.ERROR"/>
               <blue:argument type="short" value="Alert.FOREVER"/>
             </blue:call>
    
             <!-- show it to the screen and return --->
             <blue:call manager="GeneralManager" method="setCurrent">
               <blue:argument type="variable" name="alert"/>
             </blue:call>
          
          </blue:if>
          
          <!-- otherwise... --->
          <blue:else>
          
            <!-- increment mistakes counter --->
            <blue:call manager="UtilsManager" method="++">
              <blue:argument type="variable" name="mistakesCounter"/>
            </blue:call>
            
            <!-- save it --->
            <blue:set node="com/emarson/lemonade/examples/mobank/pin/MistakesCounter"
                    location="permanent">

              <blue:argument type="variable" name="mistakesCounter"/>
            </blue:set>
                        
            <!-- and show the warning message --->
            <!-- create the instance of the alert --->
              <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Alert"
                              toVariable="alert"/>

    
            <!-- 
             set its title and text, mark this is an warning alert,
             don't allow it to disappear
            --->

             <blue:call object="alert" method="initialize">
               <blue:argument type="String" value="Wrong PIN"/>
               <blue:argument type="String" 
                   value="This PIN number is incorrect."/>

               <blue:argument type="byte" value="AlertType.WARNING"/>
               <blue:argument type="short" value="1500"/>
             </blue:call>
    
             <!-- show it to the screen and return --->
             <blue:call manager="GeneralManager" method="setCurrent">
               <blue:argument type="variable" name="alert"/>
             </blue:call>
          
          </blue:else>

        </blue:else>

      </definition>
    </action>
      
    <!-- Prepare the form to specify the PIN number --->
    <action name="screen to specify a PIN number">
      <type>general/bluelemon/code</type>
      <definition resultTo="result">

        <!-- create the first text field --->
        <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/TextField"
                          toVariable="pinField"/>


        <!-- set the title, limit the size to 4, mars as numeric password --->
        <blue:call object="pinField" method="initialize">
          <blue:argument type="String" value="Your PIN:"/>
          <blue:argument type="String" value=""/>
          <blue:argument type="byte" value="4"/>
          <blue:argument type="int" value="TextField.NUMERICORPASSWORD"/>
        </blue:call>

        <!-- create the form... --->
        <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Form"
                          toVariable="form"/>


        <!-- ...set its title...  --->
        <blue:call object="form" method="initialize">
          <blue:argument type="String" value="moBank PIN"/>
        </blue:call>

        <!-- ... and put the text fields there. --->
        <blue:call object="form" method="appendItem">
          <blue:argument type="variable" name="pinField"/>
        </blue:call>

        <!-- add the action to exit from the application --->
        <blue:call manager="UtilsManager" method="getActionCommand"
                   toVariable="command">

          <blue:argument type="String" value="Exit"/>
          <blue:argument type="byte" value="ActionCommand.EXIT"/>
          <blue:argument type="byte" value="0"/>
          <blue:argument type="byteArr" contextKey="exitAction"/>
        </blue:call>

        <blue:call object="form" method="addCommand">
          <blue:argument type="variable" name="command"/>
        </blue:call>

        <!-- add the action to continue --->
        <blue:call manager="UtilsManager" method="getActionCommand"
                   toVariable="command">

          <blue:argument type="String" value="OK"/>
          <blue:argument type="byte" value="ActionCommand.OK"/>
          <blue:argument type="byte" value="0"/>
          <blue:argument type="byteArr" contextKey="okAction"/>
        </blue:call>

        <blue:call object="form" method="addCommand">
          <blue:argument type="variable" name="command"/>
        </blue:call>

        <!-- show the form on the device's screen --->
        <blue:call manager="GeneralManager" method="setCurrent">
          <blue:argument type="variable" name="form"/>
        </blue:call>

      </definition>
    </action>

    <finish name="done">
      <return>result</return>
    </finish>

  </path>

</process>