using ExtensiblePortfolioSite.SDK.Plugins;

namespace ExtensiblePortfolioSite
{
    public class Program
    {
        private static void ParseArgs(String[] args, WebApplicationBuilder builder)
        {

            Boolean Error = false;
            
            Boolean help = false;
            Boolean log_nofile = false;
            string log_output = "logs/log_{date}.log";

            for (int x = 0; x < args.Length; x++)
            {
                String arg = args[x];
                if (arg[0] == '+')
                {
                    int s = arg.IndexOf('=');

                    String value = arg[(s + 1)..];

                    switch(arg[0..s])
                    {
                        case "+log_nofile":
                            log_nofile = true;
                            break;

                        case "+log_output":
                            log_output = value;
                            break;

                        case "+help":
                            help = true;
                            break;

                        default:
                            Console.Error.WriteLine($"Unknown Arg '{arg}'");
                            Error = true;
                            break;
                    }
                }
                else if(arg == "help")
                    help = true;
            }

            if(help)
            {
                Console.Error.WriteLine(
@"
[Help Message]
+log_nofile                 Disable Flle Logging
+log_output={filepath}      Set Log File Output
+help                       Display Help Message
"
                );
                Environment.Exit(Error ? 0 : 2);
            }

            if(Error)
            {
                Console.Error.WriteLine("Exitting!");
                Environment.Exit(2);
            }

            builder.Logging.ClearProviders();
            if (log_nofile)
                builder.Logging.AddProvider(new LoggerProvider());
            else
                builder.Logging.AddProvider(new LoggerProvider(
                    log_output
                    .Replace("{date}", DateTime.Now.ToString("yyyy-MM-dd"))
                    .Replace("{time}", DateTime.Now.ToString("HH.mm.ss"))
                    ));
        }

        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Load Config
            Config.LoadConfig();

            // Parse input args
            ParseArgs(args, builder);
            
            // Load Plugins
            PluginManager.LoadPlugins(Path.GetFullPath("Plugins/"));


            // Add services to the container.
            builder.Services.AddRazorPages();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapRazorPages();

            app.Run();
        }
    }
}