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

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/moaccount/TransferSucceeded</name>
    <version>1.0</version>
    <description>
      This process is invoked when user has successfully done 
      the transfer.
      
      The Bluelemon instruction generated by this process:
       - updates the current balance,
       - adds extra row to the history dataset,
       - shows a screen informing the transfer has finished successfully.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>

  <arguments>
    <argument key="result" type="String"/>
    <argument key="currentBalance" type="Integer"/>
    <argument key="historyDatasetRow" type="String"/>
  </arguments>

  <path>
  
    <!-- LINKS --->
    
    <!-- link to go to the main scree --->
    <action name="go to the main screen">
      <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 --->

    <!-- Show the information the transfer has succeeded --->
    <action name="transfer result">
      <type>general/bluelemon/code</type>
      <definition resultTo="transferResult">

        <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Form"
                          toVariable="form"/>


        <blue:call object="form" method="initialize">
          <blue:argument type="String" value="Success"/>
        </blue:call>

        <blue:call object="form" method="appendString">
          <blue:argument type="String" contextKey="result"/>
        </blue:call>
        
        <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>

        <blue:call manager="GeneralManager" method="setCurrent">
          <blue:argument type="variable" name="form"/>
        </blue:call>

      </definition>
    </action>
    
    <!-- Change the balance --->
    <action name="balance change">
      <type>general/bluelemon/code</type>
      <definition resultTo="balanceChange">

        <!-- in case this is the first update, change the flag --->
        <blue:set node="com/emarson/lemonade/examples/mobank/moaccount/IsBalanceSet"
                  location="permanent">

          <blue:argument type="boolean" value="true"/>
        </blue:set>

        <!-- set the value of the balance in the permanent location --->
        <blue:set node="com/emarson/lemonade/examples/mobank/moaccount/AccountBalance"
                  location="permanent">

          <blue:argument type="int" contextKey="currentBalance"/>
        </blue:set>

      </definition>
    </action>
    
    <!-- Add one extra row at the beginning of the history dataset --->
    <action name="balance change">
      <type>general/bluelemon/code</type>
      <definition resultTo="history">

        <!-- get the history dataset --->
        <blue:get node="com/emarson/lemonade/examples/mobank/moaccount/HistoryData" 
                location="permanent" toVariable="historyDataset"/>

                
        <!-- append the new row --->
        <blue:call manager="UtilsManager" method="appendFirst"
                   toVariable="historyDataset">

          <blue:argument type="variable" name="historyDataset"/>
          <blue:argument type="byteArr" contextKey="historyDatasetRow"/>
        </blue:call>

        <!-- and save the changes --->
        <blue:set node="com/emarson/lemonade/examples/mobank/moaccount/HistoryData"
                  location="permanent">

          <blue:argument type="variable" name="historyDataset"/>
        </blue:set>

      </definition>
    </action>
    
    <!--  Return the merged result --->
    <finish name="done">
      <return>balanceChange + history + transferResult</return>
    </finish>

  </path>

</process>