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

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/moaccount/Validate</name>
    <version>1.0</version>
    <description>
      This is the action invoked when user is trying to continue
      after filling the TransferScreen text fields required to do the transfer: 
      ammount of money to send and a target account number.
      
      This action:
       - reads the values of the text fields,
       - if any of them is empty, shows the warning alert and returns,
       - otherwise forwards the processing to the DownloadingScreen
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>

  <path>
    
    <!-- INSTRUCTION --->
    
    <action name="read data, verify and continue">
      <type>general/bluelemon/code</type>
      <definition resultTo="result">

        <!-- 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="ammountField"
                   castTo="com/emarson/lemonade/bluelemon/midp1/TextField">

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

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

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

                   
        <!-- let's make sure both values are not empty --->
        <blue:call manager="UtilsManager" method="length"
                   toVariable="ammountStringLength">

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

          <blue:argument type="variable" name="targetAccountString"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="=="
                   toVariable="ammountStringIsEmpty">

          <blue:argument type="variable" name="ammountStringLength"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="=="
                   toVariable="targetAccountStringIsEmpty">

          <blue:argument type="variable" name="targetAccountStringLength"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="OR"
                   toVariable="anyStringIsEmpty">

          <blue:argument type="variable" name="ammountStringIsEmpty"/>
          <blue:argument type="variable" name="targetAccountStringIsEmpty"/>
        </blue:call>
        
        <!-- if any of them is empty, show the warning alert --->
        <blue:if isTrue="anyStringIsEmpty">
        
          <!-- 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="Incorrect data"/>
            <blue:argument type="String" value="All fields must be filled in."/>
            <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>
        
        <!-- if everything is OK, convert the ammount to String --->
        <blue:call manager="UtilsManager" method="stringToNumber"
                   toVariable="ammount">

          <blue:argument type="variable" name="ammountString"/>
          <blue:argument type="byte" value="4"/>
        </blue:call>
        
        <!-- invoke the downloading screen --->
        <blue:call node="com/emarson/lemonade/examples/mobank/moaccount/DownloadingScreen" 
            location="@com/emarson/lemonade/examples/mobank/moaccount">

            <blue:argument type="variable" name="ammount"/>
            <blue:argument type="variable" name="targetAccountString"/>
        </blue:call>

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

  </path>

</process>