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

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/moaccount/AccountBalanceScreen</name>
    <version>1.0</version>
    <description>
      A screen to show a current balance.
      
      This value is stored in the permanent location, in node
      'com/emarson/lemonade/examples/mobank/moaccount/AccountBalance'.
      Every time a new transfer is being done this node is updated.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>

  <path>

    <!-- ASSOCIATIONS --->
    
    <!-- association to MainScreen --->
    <action name="action for 'Back'">
      <type>general/bluelemon/code</type>
      <definition resultTo="goToMainScreen">
        <blue:call node="com/emarson/lemonade/examples/mobank/moaccount/MainScreen"
                         location="@com/emarson/lemonade/examples/mobank/moaccount"/>

      </definition>
    </action>
    
    <!-- INSTRUCTION --->

    <!-- Screen to show the account balance --->
    <action name="show the account balance screen">
      <type>general/bluelemon/code</type>
      <definition resultTo="screen">

        <!-- create the form instance --->
        <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="Account balance"/>
        </blue:call>
        
        <!-- get values of the two nodes from the permanent location --->
        <blue:get node="com/emarson/lemonade/examples/mobank/moaccount/IsBalanceSet"
                  location="permanent" toVariable="isBalanceSet"/>

        <blue:get node="com/emarson/lemonade/examples/mobank/moaccount/AccountBalance"
                  location="permanent" toVariable="balance"/>

                  
        <!-- if the first of them is set to TRUE, balance is set --->
        <blue:call manager="UtilsManager" method="=="
                   toVariable="isBalanceSet">

          <blue:argument type="variable" name="isBalanceSet"/>
          <blue:argument type="boolean" value="true"/>
        </blue:call>     
        
        <!-- if the balance is set, show its value --->
        <blue:if isTrue="isBalanceSet">
        
          <!-- convert the numeric value to the String --->
          <blue:call manager="UtilsManager" method="numberToString"
                     toVariable="balanceAsString">

            <blue:argument type="variable" name="balance"/>
          </blue:call>
          
          <!-- prepare a text to show --->
          <blue:call manager="UtilsManager" method="concat"
                     toVariable="balanceInfo">

            <blue:argument type="String" value="Your current balance is: "/>
            <blue:argument type="variable" name="balanceAsString"/>
            <blue:argument type="String" value=" EUR."/>
          </blue:call>
          
        </blue:if>
        
        <!-- otherwise just let user know why there is no balance value --->
        <blue:else>
        
          <blue:call manager="UtilsManager" method="this"
                     toVariable="balanceInfo">

            <blue:argument type="String" 
                value="You haven't done any transfers yet. Your account balance is unknown."/>

          </blue:call>  
                
        </blue:else>

        <!-- show the text on the form --->
        <blue:call object="form" method="appendString">
          <blue:argument type="variable" name="balanceInfo"/>
        </blue:call>
        
        <!--  prepare the back actions --->
        <blue:call manager="UtilsManager" method="getActionCommand"
                   toVariable="command">

          <blue:argument type="String" value="Back"/>
          <blue:argument type="byte" value="ActionCommand.BACK"/>
          <blue:argument type="byte" value="0"/>
          <blue:argument type="byteArr" contextKey="goToMainScreen"/>
        </blue:call>

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

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

      </definition>
    </action>

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

  </path>

</process>