using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Quadarax.Foundation.QConsole.Configuration;
using Quadarax.Foundation.QConsole.Context;

namespace Quadarax.Foundation.QConsole.DevBench
{
    class Program
    {
        static void Main(string[] args)
        {
            //var input = "-c:\"fofo aa\" -new:12 -b:True";
            //var result = Split(input, "\"", new[] {" ", "-"});
            //Console.ReadLine();
            var config = new StartupConfiguration("MyTestConsole", Version.Parse("1.0.0"));
            config.AllowInteractive = false;
            config.WaitOnKeyInNonInteractiveMode = true;
            config.CommandDefinitionAssebmly = new Assembly[]{Assembly.GetEntryAssembly()};
            config.DisableDefaultCommands();
            config.ShowStatusOnEveryCommand = false;

             var console = new Engine(config, new DefualtEngineContext(), args);
            console.Start();

            Console.WriteLine("Press any key to exit (external wait)...");
            Console.ReadKey();
        }


        static string[] Split(string input, string valueBracket, string[] separators)
        {
            var result = new List<string>();
            var trimedInput = input.Trim();
            if (string.IsNullOrEmpty(trimedInput))
                return result.ToArray();

            var cValueBracked = valueBracket.ToCharArray().First();
            var cSeparators = separators.Select(x => x.ToCharArray().First());

            var nIndexStart = 0;
            var nIndexEnd = 0;
            var isInsideBracked = false;
            int offset = 0;

            for (offset = 0; offset < trimedInput.Length; offset++)
            {
                var current = trimedInput[offset];
                // current | isInside | Result
                //   1          1          0
                //   1          0          1
                //   0          1          1
                //   0          0          0
                isInsideBracked = current == cValueBracked ^ isInsideBracked;
                if (isInsideBracked)
                    continue;
                if (cSeparators.Contains(current))
                {
                    nIndexEnd = offset;
                    result.Add(trimedInput.Substring(nIndexStart,nIndexEnd - nIndexStart));
                    nIndexStart = nIndexEnd;
                    isInsideBracked = false;
                }
            }

            if (offset>nIndexStart)
                result.Add(trimedInput.Substring(nIndexStart,offset - nIndexStart));

            return result.Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();
        }
    }
}
