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

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/pin/VerifyPinSaveAndRemove</name>
    <version>1.0</version>
    <description>
      This action is invoked after user specified a PIN number.
      It does the following things:
       - reads the values of PIN and PIN confirmation text fields,
       - if any of them is shorter than 4 digits or if they don't match,
         shows the error alert and returns,
       - stores the value of the PIN in the permanent location, in
         'com/emarson/lemonade/examples/mobank/pin/PinValue'
       - updates the STARTUP_INSTRUCTION to the one being part
         of the package (NewStartupInstruction)
       - removes the whole 'pin' package
       - removes from the application menu label to run 'pin' package
       - forwards user to the mobank main screen
       - shows an information alert confirming the PIN has been saved.
       The last three of those are achieved by invoking RemovePackage node.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>
  
  <path>
  
    <!-- ASSOCIATIONS --->

    <!-- association to MainScreen in mobank package --->
    <action name="go to the main screen">
      <type>general/bluelemon/code</type>
      <definition resultTo="cancelAction">
        <blue:call node="com/emarson/lemonade/examples/mobank/MainScreen"
                         location="@com/emarson/lemonade/examples/mobank"/>

      </definition>
    </action>
    
    
    <!-- INSTRUCTION --->
    
    <!-- 
      This action reads both the PINs, make sure they match and 
      are 4 digits - and stores it in the permanent location.
    --->

    <action name="verify pin numbers and save">
      <type>general/bluelemon/code</type>
      <definition resultTo="verifyAndSavePIN">

        <!-- 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"/>

        
        <!-- same with the other item --->
        <blue:call object="form" method="getItem"
                   toVariable="pinConfirmationField"
                   castTo="com/emarson/lemonade/bluelemon/midp1/TextField">

          <blue:argument type="byte" value="1"/>
        </blue:call>
        <blue:call object="pinConfirmationField" method="getString"
                   toVariable="pinConfirmation"/>

                   
        <!-- both values must have exactly 4 digits --->
        <blue:call manager="UtilsManager" method="length"
                   toVariable="pinLength">

          <blue:argument type="variable" name="pin"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="length"
                   toVariable="pinConfirmationLength">

          <blue:argument type="variable" name="pinConfirmation"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="lesserThan"
                   toVariable="pinIsNot4DigitsLong">

          <blue:argument type="variable" name="pinLength"/>
          <blue:argument type="byte" value="4"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="lesserThan"
                   toVariable="pinConfirmationIsNot4DigitsLong">

          <blue:argument type="variable" name="pinConfirmationLength"/>
          <blue:argument type="byte" value="4"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="OR"
                   toVariable="anyStringIsNot4DigitsLong">

          <blue:argument type="variable" name="pinIsNot4DigitsLong"/>
          <blue:argument type="variable" name="pinConfirmationIsNot4DigitsLong"/>
        </blue:call>
        
        <!-- if any of them is empty, show the warning alert --->
        <blue:if isTrue="anyStringIsNot4DigitsLong">
        
          <!-- 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 a warning alert
           --->

          <blue:call object="alert" method="initialize">
            <blue:argument type="String" value="Wrong PIN"/>
            <blue:argument type="String" value="PIN must be 4 digits long."/>
            <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:return/>
          
        </blue:if>
        
        <!-- both pin numbers must match --->
        <blue:call manager="UtilsManager" method="equalValues"
                   toVariable="pinsMatch">

          <blue:argument type="variable" name="pin"/>
          <blue:argument type="variable" name="pinConfirmation"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="NOT"
                   toVariable="pinsDontMatch">

          <blue:argument type="variable" name="pinsMatch"/>
        </blue:call>
        
        <!-- if PINs dont match, show the error message --->
        <blue:if isTrue="pinsDontMatch">
        
          <!-- 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 a warning alert
           --->

          <blue:call object="alert" method="initialize">
            <blue:argument type="String" value="Wrong PIN"/>
            <blue:argument type="String" value="The numbers you entered don't match."/>
            <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:return/>
          
        </blue:if>
        
        <!-- OK, the PIN is correct - save it --->
        <blue:set node="com/emarson/lemonade/examples/mobank/pin/PinValue"
                  location="permanent">

          <blue:argument type="variable" name="pin"/>
        </blue:set>
        
      </definition>
    </action>
    
    <!-- 
      Read the content of the NewStartupInstruction node and save it
      as STARTUP_INSTRUCTION in the permanent location
    --->

    <action name="update startup instruction">
      <type>general/bluelemon/code</type>
      <definition resultTo="updateStartupInstruction">

        <!-- get the new startup instruction --->
        <blue:get node="com/emarson/lemonade/examples/mobank/pin/NewStartupInstruction"
                  location="@com/emarson/lemonade/examples/mobank/pin" 
                  toVariable="newStartupInstruction"/>

        
        <!-- save it as STARTUP_INSTRUCTION --->
        <blue:set node="STARTUP_INSTRUCTION" location="permanent">
          <blue:argument type="variable" name="newStartupInstruction"/>
        </blue:set>
        
        <!-- set mistakes counter to zero --->
        <blue:set node="com/emarson/lemonade/examples/mobank/pin/MistakesCounter"
                  location="permanent">

          <blue:argument type="byte" value="0"/>
        </blue:set>
        
      </definition>
    </action>
    
    <!-- 
      By calling RemovePackage node all the NIP package will be 
      cleaned from the device and user will be forwarded to the main
      menu of the moBank application.
    --->

    <action name="remove package">
      <type>general/bluelemon/code</type>
      <definition resultTo="removePackage">

        <!-- call the RemovePackage node --->
        <blue:call node="com/emarson/lemonade/examples/mobank/pin/RemovePackage"
                   location="@com/emarson/lemonade/examples/mobank/pin"/>

        
      </definition>
    </action>
    
    <!-- At the end we want user to know everything went well. --->
    <action name="show confirmation alert">
      <type>general/bluelemon/code</type>
      <definition resultTo="showConfirmationAlert">

        <!-- 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 info alert,
         don't allow it to disappear
        --->

         <blue:call object="alert" method="initialize">
           <blue:argument type="String" value="Success"/>
           <blue:argument type="String" value="PIN has been set successfully."/>
           <blue:argument type="byte" value="AlertType.CONFIRMATION"/>
           <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>
          
      </definition>
    </action>


    <finish name="done">
      <return>verifyAndSavePIN + updateStartupInstruction + 
          removePackage + showConfirmationAlert</return>
    </finish>

  </path>

</process>