<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" height="690" width="700" 
    xmlns:ftp="pl.maliboo.ftp.core.*">
    <mx:Script>
        <![CDATA[
            import pl.maliboo.ftp.events.FTPEvent;
            import mx.controls.treeClasses.TreeItemRenderer;
            import mx.core.IDataRenderer;
            import flash.filesystem.File;
            import pl.maliboo.ftp.Commands;
            import pl.maliboo.ftp.FTPCommand;
            import pl.maliboo.ftp.FTPFile;
            import pl.maliboo.ftp.events.FTPEvent;
            import pl.maliboo.ftp.core.FTPClient;
            import pl.maliboo.ftp.utils.ConsoleListener;
            
            private var list:ConsoleListener;
                        
            public function makeConnection (host:String, port:Number):void
            {
                if (list == null)
                    list = new ConsoleListener(client, output);
                client.addEventListener(FTPEvent.CONNECTED, handleConnected);
                client.connect(host, port);
            }
            
            public function handleConnected (evt:FTPEvent):void
            {
                client.addEventListener(FTPEvent.LOGGED, handleLogged);
                client.login(username.text, passwd.text);
            }
            
            public function handleLogged (evt:FTPEvent):void
            {
                client.addEventListener(FTPEvent.LISTING, handleListing);
                client.addEventListener(FTPEvent.CHANGE_DIR, handleCD);
                client.list();
            }
            
            public function handleCD (evt:FTPEvent):void
            {
                client.list();
            }
            
            public function handleListing(evt:FTPEvent):void
            {
                fileList.dataProvider = evt.listing;
            }
            
            /*
            public function makeKaszana (evt:FTPEvent):void
            {
                trace("Robie kaszane!")
                client.removeEventListener(evt.type, arguments.callee);
                client.sendCommand(new FTPCommand("NIMA!"));
            }
            */
            
            public function handleDGDoubleClick (evt:Event):void
            {
                var file:FTPFile = (evt.target as IDataRenderer).data as FTPFile;
                if (file.isDir)
                {
                    client.cwd(file.name);
                }
                else
                {
                    client.getFile(file.fullPath, (localDir.selectedItem as File).nativePath+file.name);
                }
            }
            
            public function handleFileSystemDC (evt:Event):void
            {
                var file:File = (evt.target.parent as IDataRenderer).data as File;
                if (!file.isDirectory)
                    client.putFile(file.nativePath, client.workingDirectory+"/"+file.name);
            }
            
        ]]>
    </mx:Script>
    <ftp:FTPClient id="client"/>
    <mx:VBox left="10" top="10" bottom="10" right="10">
        <mx:VBox>
        <mx:HBox width="100%">
            <mx:Label text="Host:"/>
            <mx:TextInput id="hostName" text=""/>
            <mx:Label text="Port:"/>
            <mx:TextInput width="60" id="hostPort" text="21"/>
            <mx:Button label="Połącz" id="connectButton" 
                    click="makeConnection(hostName.text, int(hostPort.text))"/>
        </mx:HBox>
        <mx:HBox left="10" top="10" bottom="10" right="10">
            <mx:Label text="Username"/>
            <mx:TextInput id="username" text=""/>
            <mx:Label text="Passwd"/>
            <mx:TextInput id="passwd" text="" displayAsPassword="true"/>
        </mx:HBox>    
        </mx:VBox>
        <mx:HBox width="676">
            <mx:VBox>
                <mx:TextInput id="path" text="{client.workingDirectory}" enter="client.cwd(path.text);" width="302"/>
                <mx:DataGrid id="fileList" doubleClick="handleDGDoubleClick(event)" doubleClickEnabled="true">
                    <mx:columns>
                        <mx:DataGridColumn headerText="Nazwa" dataField="name"/>
                        <mx:DataGridColumn headerText="Rozmiar" dataField="size"/>
                        <mx:DataGridColumn headerText="Katalog" dataField="isDir"/>
                    </mx:columns>
                </mx:DataGrid>
            </mx:VBox>
            <mx:VBox>
            <mx:TextInput text="{(localDir.selectedItem as File).nativePath}" width="342"/>
            <mx:FileSystemTree id="localDir" width="344" height="135" selectedIndex="{localDir.dataProvider.length - 1}" doubleClickEnabled="true" doubleClick="handleFileSystemDC(event)"/>
            </mx:VBox>
        </mx:HBox>
        <mx:TextArea id="output" width="570" height="390"/>
        <mx:HBox width="100%" height="23">
            <mx:TextInput width="483" id="commandLine"/>
            <mx:Button label="Wyślij" id="sendCommandButton"/>
        </mx:HBox>
    </mx:VBox>
</mx:ApolloApplication>