namespace Firefly
{
    public partial class Firefly
    {
        #region Command Line Parsing
        static async Task ParseArguments(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i].ToLower();

                switch (arg)
                {
                    case "--port":
                    case "-p":
                        if (i + 1 < args.Length && int.TryParse(args[i + 1], out int port))
                        {
                            serverPort = port;
                            Console.WriteLine($"Server port set to: {serverPort}");
                            i++; // Skip the next arg (the port number)
                        }
                        else
                        {
                            Console.WriteLine("Warning: Invalid port specified, using default port 6379");
                        }
                        break;

                    case "--bind":
                    case "-b":
                        if (i + 1 < args.Length)
                        {
                            bindAddress = args[i + 1];
                            if (bindAddress == "0.0.0.0")
                            {
                                Console.WriteLine("Server will listen on all network interfaces");
                            }
                            else
                            {
                                Console.WriteLine($"Server bind address set to: {bindAddress}");
                            }
                            i++; // Skip the next arg (the bind address)
                        }
                        else
                        {
                            Console.WriteLine("Warning: Bind address argument specified but no address provided");
                        }
                        break;

                    case "--password":
                    case "-pass":
                        if (i + 1 < args.Length)
                        {
                            serverPassword = args[i + 1];
                            Console.WriteLine("Password authentication enabled");
                            i++; // Skip the next arg (the password)
                        }
                        else
                        {
                            Console.WriteLine("Warning: Password argument specified but no password provided");
                        }
                        break;

                    case "--no-backup":
                    case "-nb":
                        backupsEnabled = false;
                        Console.WriteLine("Backups disabled");
                        break;

                    case "--backup-interval":
                    case "-bi":
                        if (i + 1 < args.Length && int.TryParse(args[i + 1], out int interval) && interval > 0)
                        {
                            autoBackupIntervalMinutes = interval;
                            Console.WriteLine($"Backup interval set to: {autoBackupIntervalMinutes} minutes");
                            i++; // Skip the next arg (the interval)
                        }
                        else
                        {
                            Console.WriteLine($"Warning: Invalid backup interval specified, using default ({autoBackupIntervalMinutes} minutes)");
                        }
                        break;

                    case "--max-backups":
                    case "-mb":
                        if (i + 1 < args.Length && int.TryParse(args[i + 1], out int maxFiles) && maxFiles > 0)
                        {
                            maxBackupFiles = maxFiles;
                            Console.WriteLine($"Maximum backup files set to: {maxBackupFiles}");
                            i++; // Skip the next arg (the count)
                        }
                        else
                        {
                            Console.WriteLine($"Warning: Invalid maximum backup files specified, using default ({maxBackupFiles})");
                        }
                        break;

                    case "--timeout":
                    case "-t":
                        if (i + 1 < args.Length && int.TryParse(args[i + 1], out int timeout) && timeout > 0)
                        {
                            connectionTimeoutSeconds = timeout;
                            Console.WriteLine($"Connection timeout set to: {connectionTimeoutSeconds} seconds");
                            i++; // Skip the next arg (the timeout)
                        }
                        else
                        {
                            Console.WriteLine($"Warning: Invalid timeout specified, using default ({connectionTimeoutSeconds} seconds)");
                        }
                        break;

                    case "--pipeline-test":
                    case "-pt":
                        Console.WriteLine("Running in pipeline test mode...");
                        
                        // Start the server in the background
                        var serverTask = Task.Run(async () => 
                        {
                            await StartServerAsync();
                        });
                        
                        // Give the server a moment to start up
                        await Task.Delay(1000);
                        
                        // Run the pipeline test with all arguments
                        await PipelineTest.RunTest(args);
                        
                        // After the test completes, exit the application
                        Environment.Exit(0);
                        return;

                    case "--help":
                    case "-h":
                        DisplayHelp();
                        Environment.Exit(0);
                        break;
                }
            }
        }

        static void DisplayHelp()
        {
            Console.WriteLine("\nFirefly Redis-compatible Server");
            Console.WriteLine("Usage: firefly [options]\n");
            Console.WriteLine("Options:");
            Console.WriteLine("  --port, -p <number>           Set server port (default: 6379)");
            Console.WriteLine("  --bind, -b <address>          Set server bind address (default: 127.0.0.1)");
            Console.WriteLine("  --password, -pass <string>    Set server password for AUTH command");
            Console.WriteLine("  --no-backup, -nb              Disable automatic backups");
            Console.WriteLine("  --backup-interval, -bi <mins> Set backup interval in minutes (default: 5)");
            Console.WriteLine("  --max-backups, -mb <number>   Set maximum number of backup files to keep (default: 10)");
            Console.WriteLine("  --timeout, -t <seconds>       Set client connection timeout in seconds (default: 300)");
            Console.WriteLine("  --pipeline-test, -pt          Run in pipeline test mode");
            Console.WriteLine("  --help, -h                    Display this help message\n");
            
            Console.WriteLine("Pipeline Test Options:");
            Console.WriteLine("  --host <address>              Set test host (default: 127.0.0.1)");
            Console.WriteLine("  --clients <number>            Number of concurrent clients (default: 1)");
            Console.WriteLine("  --commands <number>           Number of commands per client (default: 10000)");
            Console.WriteLine("  --batch <number>              Batch size for batched test (default: 100)");
            Console.WriteLine("  --buffer <size>               Buffer size in KB (default: 1024)\n");
            
            Console.WriteLine("Examples:");
            Console.WriteLine("  firefly --port 6380            Run server on port 6380");
            Console.WriteLine("  firefly --bind 0.0.0.0         Listen on all network interfaces");
            Console.WriteLine("  firefly --bind 192.168.1.10    Listen on a specific network interface");
            Console.WriteLine("  firefly --password secret123   Require password authentication");
            Console.WriteLine("  firefly -nb                    Run server with backups disabled");
            Console.WriteLine("  firefly -bi 10                 Run server with backups every 10 minutes");
            Console.WriteLine("  firefly -mb 5                  Keep only 5 most recent backup files");
            Console.WriteLine("  firefly -t 60                  Set connection timeout to 60 seconds");
            Console.WriteLine("  firefly -pt                    Run pipeline performance test");
            Console.WriteLine("  firefly -pt --clients 4        Run pipeline test with 4 concurrent clients");
            Console.WriteLine("  firefly -pt --host 192.168.1.100 --port 6380 --password mypass");
            Console.WriteLine("                                  Run pipeline test with custom connection settings");
            Console.WriteLine("  firefly -pt --commands 5000 --batch 50");
            Console.WriteLine("                                  Run pipeline test with custom command count and batch size");
        }
        #endregion
    }
} 