<?xml version="1.0" encoding="utf-8"?>
<EventMap xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="http://mate.asfusion.com/">
    
    <mx:Script>
        <![CDATA[
            import com.cafetownsend.model.constants.*;
            import mx.events.*;
            import com.cafetownsend.events.*;
            import com.cafetownsend.model.managers.*;
            import com.cafetownsend.ui.views.*;
            import com.cafetownsend.ui.presenters.*;
        ]]>
    </mx:Script>
    
    <!-- FlexEvent.PREINITIALIZE __________________________________________________________________ -->
    
    <EventHandlers type="{FlexEvent.PREINITIALIZE}">
        <ObjectBuilder generator="{ LoginPresentationModel }" constructorArguments="{ scope.dispatcher }" />
        <ObjectBuilder generator="{ EmployeeDetailPresentationModel }" constructorArguments="{ scope.dispatcher }" />
        <ObjectBuilder generator="{ EmployeeListPresentationModel }" constructorArguments="{ scope.dispatcher }" />
    </EventHandlers>
    
    <!-- FlexEvent.APPLICATION_COMPLETE ___________________________________________________________ -->
    
    <EventHandlers type="{FlexEvent.APPLICATION_COMPLETE}">
        
        <HTTPServiceInvoker instance="{employeesService}">
            <resultHandlers>
                <MethodInvoker generator="{EmployeeParser}" 
                    method="loadEmployeesFromXML" arguments="{resultObject}" />
                    
                <MethodInvoker generator="{EmployeeManager}" method="saveEmpoyeeList" arguments="{lastReturn}" />    
            </resultHandlers>
        </HTTPServiceInvoker>

    </EventHandlers>
    
    <!-- LoginEvent.LOGIN ________________________________________________________________________ -->
    
    <!-- Loging in, normally this would send a server request, but we are making it simple here  -->
    <EventHandlers type="{LoginEvent.LOGIN}">
        <MethodInvoker generator="{ AuthorizationManager }" method="login" arguments="{[event.username, event.password]}" />
        <MethodInvoker generator="{ NavigationManager }" method="updateAfterLogin" arguments="{ lastReturn }"/>
    </EventHandlers>
    
    <!-- EmployeeEvent.SELECT _____________________________________________________________________ -->
    
    <EventHandlers type="{EmployeeEvent.SELECT}">
        <MethodInvoker generator="{EmployeeManager}" method="selectEmployee" arguments="{event.employee}"/>
        <MethodInvoker generator="{ NavigationManager }" method="updatePath" arguments="{ Navigation.EMPLOYEE_DETAIL }"/>
    </EventHandlers>

    <!-- EmployeeEvent.DELETE ____________________________________________________________________ -->
    
    <EventHandlers type="{EmployeeEvent.DELETE}">
        <MethodInvoker generator="{EmployeeManager}" method="deleteEmployee" arguments="{event.employee}"/>
        <!-- assume everything was ok, make employee list show up -->
        <MethodInvoker generator="{ NavigationManager }" method="updatePath" arguments="{ Navigation.EMPLOYEE_LIST }"/>
    </EventHandlers>
    
    <!-- EmployeeEvent.SAVE _______________________________________________________________________ -->
    
    <EventHandlers type="{EmployeeEvent.SAVE}">
        <MethodInvoker generator="{EmployeeManager}" method="saveEmployee" arguments="{event.employee}"/>
        <!-- assume everything was ok, make employee list show up -->
        <MethodInvoker generator="{ NavigationManager }" method="updatePath" arguments="{ Navigation.EMPLOYEE_LIST }"/>
    </EventHandlers>
    
    <!-- EmployeeEvent.CANCEL_EDIT __________________________________________________________________ -->
    
    <EventHandlers type="{EmployeeEvent.CANCEL_EDIT}">
        <!-- assume everything was ok, make employee list show up -->
        <MethodInvoker generator="{ NavigationManager }" method="updatePath" arguments="{ Navigation.EMPLOYEE_LIST }"/>
    </EventHandlers>
    
    <!-- EmployeeEvent.ADD _________________________________________________________________________ -->
    
    <EventHandlers type="{EmployeeEvent.ADD}">
        <EventAnnouncer generator="{EmployeeEvent}" type="{EmployeeEvent.SELECT}">
            <Properties employee="{event.employee}"/>
        </EventAnnouncer>
    </EventHandlers>
    
    <!-- LoginEvent.LOGOUT _________________________________________________________________________ -->
    <EventHandlers type="{LoginEvent.LOGOUT}">
        <MethodInvoker generator="{ AuthorizationManager }" method="logout"/>
        <MethodInvoker generator="{ NavigationManager }" method="updatePath" arguments="{ Navigation.LOGIN }"/>
    </EventHandlers>
    
    
    
    <!-- ======================================================================================== -->
    <!-- Injectors -->
    <!-- _________________________________________________________________________________________ -->
    
    <!-- MainUI __________________________________________________________________ -->
    
    <Injectors target="{ MainUI }" >
        <PropertyInjector targetKey="model" source="{ MainUIPresentationModel }"/>
    </Injectors>
    
    <!-- MainUI __________________________________________________________________ -->
    
    <Injectors target="{ MainUIPresentationModel }" >
        <PropertyInjector targetKey="navigationPath" source="{ NavigationManager }" sourceKey="navigationPath"/>
    </Injectors>
    
    
    <!-- EmployeeDetail __________________________________________________________________ -->
    
    <Injectors target="{EmployeeDetail}" >
        <PropertyInjector targetKey="model" source="{ EmployeeDetailPresentationModel} " />
    </Injectors>
    
    
    <!-- EmployeeDetailPresentationModel _______________________________________________________ -->
    
    <Injectors target="{ EmployeeDetailPresentationModel }" >
        <PropertyInjector targetKey="selectedEmployee" source="{ EmployeeManager }" sourceKey="employee" />
    </Injectors>
    
    <!-- EmployeeList View __________________________________________________________________ -->
    
    <Injectors target="{ EmployeeList }">
        <PropertyInjector targetKey="model" source="{ EmployeeListPresentationModel }" />
    </Injectors>
    
    
    <!-- EmployeeListPresentationModel  __________________________________________________________________ -->
    
    <Injectors target="{ EmployeeListPresentationModel }">
        <PropertyInjector targetKey="employees" source="{EmployeeManager}" sourceKey="employeeList" />
    </Injectors>
    
    <!-- Login __________________________________________________________________ -->
    
    <Injectors target="{ Login }" >
        <PropertyInjector targetKey="model" source="{ LoginPresentationModel }" />
    </Injectors>
    
    <!-- LoginPresentationModel __________________________________________________ -->
    
    <Injectors target="{ LoginPresentationModel }" >
        <PropertyInjector targetKey="loginStatus" source="{ AuthorizationManager }" sourceKey="status" />
    </Injectors>
    
    <!-- ======================================================================================== -->
    <!-- Services   Note: you can have an external file if you have more services -->
    <!-- _________________________________________________________________________________________ -->
    
    <mx:HTTPService id="employeesService" url="assets/data/Employees.xml" resultFormat="e4x" />
    
</EventMap>