using Microsoft.Extensions.Configuration;
using Phares.Shared;
using Serilog;
using System.Diagnostics;
using System.Reflection;
using View_by_Distance.Not.Copy.Copy.Models;
using View_by_Distance.Shared.Models.Stateless.Methods;

namespace View_by_Distance.Not.Copy.Copy;

public class Program
{

    public static void Secondary(List<string> args)
    {
        LoggerConfiguration loggerConfiguration = new();
        Assembly assembly = Assembly.GetExecutingAssembly();
        bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
        IsEnvironment isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
        IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddJsonFile(isEnvironment.AppSettingsFileName, optional: false, reloadOnChange: true);
        IConfigurationRoot configurationRoot = configurationBuilder.Build();
        AppSettings appSettings = Models.Binder.AppSettings.Get(configurationRoot);
        if (appSettings.MaxDegreeOfParallelism > Environment.ProcessorCount)
            throw new Exception("MaxDegreeOfParallelism must be =< Environment.ProcessorCount!");
        if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName))
            throw new Exception("Working directory name must have a value!");
        string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
        Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
        _ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
        Log.Logger = loggerConfiguration.CreateLogger();
        ILogger log = Log.ForContext<Program>();
        int silentIndex = args.IndexOf("s");
        if (silentIndex > -1)
            args.RemoveAt(silentIndex);
        try
        {
            if (args is null)
                throw new Exception("args is null!");
#nullable disable
            if (IProperty.IsWrongYear("-".Split(' '), "2021").Item1.HasValue)
                throw new Exception("-");
            if (IProperty.IsWrongYear("Christmass".Split(' '), "2021").Item1.HasValue)
                throw new Exception("Christmass");
            if (IProperty.IsWrongYear("Christmass 2021".Split(' '), "2021").Item1.Value)
                throw new Exception("Christmass");
            if (IProperty.IsWrongYear("Christmass ~2021".Split(' '), "2021").Item1.Value)
                throw new Exception("Christmass");
            if (IProperty.IsWrongYear("Christmass ~2021.4".Split(' '), "2021").Item1.Value)
                throw new Exception("Christmass");
            if (!IProperty.IsWrongYear("Christmass 2021".Split(' '), "2025").Item1.Value)
                throw new Exception("Christmass");
            if (!IProperty.IsWrongYear("Christmass ~2021".Split(' '), "2025").Item1.Value)
                throw new Exception("Christmass");
            if (!IProperty.IsWrongYear("Christmass ~2021.4".Split(' '), "2025").Item1.Value)
                throw new Exception("Christmass");
            if (IProperty.IsWrongYear("England 2017".Split(' '), "2017").Item1.Value)
                throw new Exception("England");
            if (IProperty.IsWrongYear("Logan Michael".Split(' '), "2021").Item1.HasValue)
                throw new Exception("Logan Michael");
            if (IProperty.IsWrongYear("Logan Michael 2021".Split(' '), "2021").Item1.Value)
                throw new Exception("Logan Michael");
            if (IProperty.IsWrongYear("Logan Michael ~2021".Split(' '), "2021").Item1.Value)
                throw new Exception("Logan Michael");
            if (IProperty.IsWrongYear("Logan Michael ~2021.4".Split(' '), "2021").Item1.Value)
                throw new Exception("Logan Michael");
            if (!IProperty.IsWrongYear("Logan Michael 2021".Split(' '), "2025").Item1.Value)
                throw new Exception("Logan Michael");
            if (!IProperty.IsWrongYear("Logan Michael ~2021".Split(' '), "2025").Item1.Value)
                throw new Exception("Logan Michael");
            if (!IProperty.IsWrongYear("Logan Michael ~2021.4".Split(' '), "2025").Item1.Value)
                throw new Exception("Logan Michael");
            if (IProperty.IsWrongYear("Logan Michael ~2021.4".Split(' '), "2021").Item2[0] != "~2021.4")
                throw new Exception("Logan Michael");
            if (IProperty.IsWrongYear("Chelsea's 2nd Birthday =2014".Split(' '), "2014").Item1.Value)
                throw new Exception("Chelsea");
#nullable restore
            if (IPath.GetDirectoryNames(@"C:\Tmp")[0] != @"C:\")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"C:\Tmp")[1] != "Tmp")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"C:\Tmp\mike.txt")[1] != "Tmp")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"C:\Tmp\a.txt")[1] != "Tmp")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"C:\Tmp\Mike\a.txt")[2] != "Mike")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"I:\Images 2019-06-08 - 34a9240ac28b52da97428d7725153a80a757ee6b - II\=2010.2 Summer\Dsc_8558.jpg")[0] != @"I:\")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"I:\Images 2019-06-08 - 34a9240ac28b52da97428d7725153a80a757ee6b - II\=2010.2 Summer\Dsc_8558.jpg")[1] != @"Images 2019-06-08 - 34a9240ac28b52da97428d7725153a80a757ee6b - II")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"I:\Images 2019-06-08 - 34a9240ac28b52da97428d7725153a80a757ee6b - II\=2010.2 Summer\Dsc_8558.jpg")[2] != @"=2010.2 Summer")
                throw new Exception();
            if (IPath.GetDirectoryNames(@"I:\Images 2019-06-08 - 34a9240ac28b52da97428d7725153a80a757ee6b - II\=2010.2 Summer")[2] != @"=2010.2 Summer")
                throw new Exception();
            Shared.Models.Console console = new();
            NotCopyCopy _ = new(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
        }
        catch (Exception ex)
        {
            log.Fatal(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
        }
        finally
        {
            Log.CloseAndFlush();
        }
        if (silentIndex > -1)
            log.Debug("Done. Bye");
        else
        {
            log.Debug("Done. Press 'Enter' to end");
            _ = Console.ReadLine();
        }
    }

    public static void Main(string[] args)
    {
        if (args is not null)
            Secondary(args.ToList());
        else
            Secondary(new List<string>());
    }

}