package
{
    import flash.events.MouseEvent;
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.controls.Button;
    import mx.controls.List;
    import mx.controls.Text;
    import mx.core.Application;
    import mx.events.ListEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    public class ListApp extends Application
    {
        [Bindable]
        public var employeeList:ArrayCollection = new ArrayCollection();
        public var employeesService:HTTPService;
        public var textMessage:Text;
        public var mylist:List;
        public var mybutton:Button;

        public static const employeesServiceUrl:String =
            "http://localhost:18080/employees.xml";

        public function appComplete():void
        {
            _initButtonControl();
            _initListControl();
            _initEmployeesService();
            trace(this + " initialized!");
        }

        private function _initButtonControl():void
        {
            trace(this + "._initButtonControl()");
            mybutton.addEventListener(MouseEvent.CLICK, requestEmployees);
        }

        private function _initListControl():void
        {
            trace(this + "._initListControl()");
            mylist.addEventListener(ListEvent.ITEM_CLICK, showMessage);
            trace(this + ".mylist.dataProvider = " + mylist.dataProvider);
        }

        private function _initEmployeesService():void
        {
            trace(this + "._initEmployeesService()");
            employeesService = new HTTPService();
            employeesService.url = employeesServiceUrl;
            employeesService.method = "GET";
            employeesService.resultFormat = "object";
            employeesService.addEventListener("result", resultHandler);
            employeesService.addEventListener("fault", faultHandler);
        }

        public function requestEmployees(event:Event):void
        {
            trace(this + ".requestEmployees(" + event + ")");
            employeesService.send();
        }

        public function resultHandler(event:ResultEvent):void
        {
            trace(this + ".resultHandler(" + event + ")");
            trace(this + " event.result = " + event.result);
            trace(this + " event.headers = " + event.headers);
            trace(this + " event.statusCode = " + event.statusCode);
            trace(this + " event.result.employees = " + event.result.employees);
            employeeList = event.result.employees.employee as ArrayCollection;
            // mylist.dataProvider = employeeList;
            trace(this + ".employeeList = " + employeeList);
        }

        public function faultHandler(event:FaultEvent):void {
            var faultstring:String = event.fault.faultString;
            Alert.show(faultstring);
        }

        public function showMessage(event:Event):void
        {
            textMessage.text = "You selected: " +
                event.currentTarget.selectedItem.firstName + ' ' +
                event.currentTarget.selectedItem.lastName;
        }
    }
}
