//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Net;
using Rebex.Net;

namespace Rebex.Samples
{
    /// <summary>
    /// This sample retrieves the list of files from a FTP server.
    /// It demonstrates the usage of FtpList class.
    /// </summary>
    class FileList
    {
        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            if (args.Length != 1)
            {
                // display syntax if wrong number of parameters or unsupported operation
                Console.WriteLine("FtpFileList - retrieves file list from a FTP server");
                Console.WriteLine("Syntax: FtpFileList [username:[password]@]hostname/remotepath");
                Console.WriteLine("Example: FtpFileList demo:password@test.rebex.net/pub/example/");
                return;
            }

            // get username and password from URI
            Uri uri = new Uri("ftp://" + args[0]);

            string username;
            string password = null;
            if (uri.UserInfo.Length == 0)
            {
                Console.Write("Username: ");
                username = Console.ReadLine();
            }
            else
            {
                string[] userInfo = uri.UserInfo.Split(':');
                username = userInfo[0];
                if (userInfo.Length > 1)
                    password = userInfo[1];
            }

            if (password == null)
            {
                Console.Write("Password: ");
                password = Console.ReadLine();
                if (password.Length == 0)
                    password = null;
            }

            // create Ftp object and connect to the server
            Ftp ftp = new Ftp();
            ftp.Connect(uri.Host, uri.Port);

            // login with username and password
            ftp.Login(username, password);

            // change directory to specified path
            ftp.ChangeDirectory(uri.AbsolutePath);

            // retrieve the directory listing
            FtpItemCollection list = ftp.GetList();

            // print the result
            foreach (FtpItem item in list)
            {
                if (item.IsLink)
                    Console.Write("s ");
                else if (item.IsDirectory)
                    Console.Write("d ");
                else
                    Console.Write("- ");

                Console.Write(item.LastWriteTime.Value.ToString("u").Substring(0, 16));
                Console.Write(item.Length.ToString().PadLeft(10, ' '));
                Console.Write(" {0}", item.Name);
                if (item.IsLink)
                    Console.Write(" -> " + item.SymlinkPath);
                Console.WriteLine();
            }

            // disconnect from the server
            ftp.Disconnect();
        }
    }
}
