<?xml version="1.0" encoding="UTF-8"?>
<flow
    xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow 
                        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
    parent="common-transitions"
    start-state="chooseToSearch">

    <input
        name="itemId"
        value="flowScope.itemId"
        type="string" />

    <input
        name="itemType"
        value="flowScope.itemType"
        type="gov.va.med.pharmacy.peps.common.vo.EntityType" />

    <decision-state id="chooseToSearch">
        <if
            test="flowScope.itemId == null and flowScope.itemType == null"
            then="search"
            else="retrieve" />
    </decision-state>

    <subflow-state
        id="search"
        subflow="searchItems">
        <transition
            on="end"
            to="chooseToLoop">
            <set
                name="flowScope.itemId"
                value="currentEvent.attributes.itemId"
                type="string" />
            <set
                name="flowScope.itemType"
                value="currentEvent.attributes.itemType"
                type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        </transition>
    </subflow-state>

    <decision-state id="chooseToLoop">
        <on-entry>
            <!-- chooseToLoop only called when leaving search. First reset the selections, if any set previously. -->
           
            <set
                name="conversationScope.tableSelections"
                value="null" />
            <set
                name="conversationScope.tableSelectionsIndex"
                value="0"
                type="int" />
            <set
                name="conversationScope.isFirstItem"
                value="true"
                type="boolean" />
            <set
                name="conversationScope.isMultiSelect"
                value="true"
                type="boolean" />                
                

            <evaluate expression="springWebFlowActionAdapter">
                <attribute
                    name="actionName"
                    value="peps" />
                <attribute
                    name="actionMethod"
                    value="input" />
            </evaluate>
        </on-entry>
        <if
            test="conversationScope.tableSelections != null and !conversationScope.tableSelections.isEmpty()"
            then="nextItem"
            else="retrieve" />
    </decision-state>

    <action-state id="nextItem">
        <evaluate expression="springWebFlowActionAdapter">
            <attribute
                name="actionName"
                value="managedItem" />
            <attribute
                name="actionMethod"
                value="parseUniqueId" />
        </evaluate>
        <transition
            on="success"
            to="retrieve" />
        <transition
            on="error"
            to="search" />
    </action-state>

    <action-state id="retrieve">
        <evaluate expression="springWebFlowActionAdapter">
            <attribute
                name="actionName"
                value="managedItem" />
            <attribute
                name="actionMethod"
                value="retrieve" />
        </evaluate>
        <transition
            on="success"
            to="editItem" />
    </action-state>

    <subflow-state
        id="editItem"
        subflow="editItem">
        <input
            name="itemId"
            value="flowScope.itemId"
            type="string" />
        <input
            name="itemType"
            value="flowScope.itemType"
            type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        <input
            name="item"
            value="flowScope.item"
            type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
        <input
            name="oldItem"
            value="flowScope.oldItem"
            type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
        <transition
            on="end"
            to="chooseEnd">
            <set
                name="flowScope.item"
                value="currentEvent.attributes.item"
                type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
            <set
                name="flowScope.itemId"
                value="currentEvent.attributes.itemId != null ? currentEvent.attributes.itemId : currentEvent.attributes.item.id"
                type="string" />
            <set
                name="flowScope.itemType"
                value="currentEvent.attributes.itemType"
                type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        </transition>
        <transition
            on="searchEnd"
            to="search" />
        <transition
            on="retrieveEnd"
            to="retrieve" />
        <transition
            on="cancel"
            to="cancel" />            
    </subflow-state>

    <decision-state id="chooseEnd">
        <if
            test="conversationScope.tableSelections != null and !conversationScope.tableSelections.isEmpty() and conversationScope.tableSelections.size() > conversationScope.tableSelectionsIndex"
            then="nextItem" />
        <if
            test="flowScope.itemType.isDomainType()"
            then="startDomainFlow" />
        <if
            test="flowScope.subFlow"
            then="end"
            else="restartFlow" />
    </decision-state>

    <end-state id="end">
        <output
            name="itemId"
            value="flowScope.itemId"
            type="string" />
        <output
            name="itemType"
            value="flowScope.itemType"
            type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        <output
            name="item"
            value="flowScope.item"
            type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
    </end-state>

    <end-state
        id="restartFlow"
        view="flowRedirect:${flowScope.flowId}">
        <output
            name="itemId"
            value="flowScope.itemId"
            type="string" />
        <output
            name="itemType"
            value="flowScope.itemType"
            type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
    </end-state>

    <end-state
        id="startDomainFlow"
        view="flowRedirect:manageDomain" />
        
    <end-state id="cancel" />

</flow>