using Microsoft.Extensions.Logging;

namespace File_Folder_Helper.Helpers;

internal static class HelperSaveOrCopyContents
{

    internal static void SaveOrCopyContents(ILogger log, string argsZero, ConsoleKey consoleKey)
    {
        bool save;
        ConsoleKey? dfb;
        List<string> collection;
        SearchOption searchOption;
        long now = DateTime.Now.Ticks;
        string? parentDirectory = Path.GetDirectoryName(argsZero);
        if (string.IsNullOrEmpty(parentDirectory))
            throw new NotSupportedException();
        if (consoleKey == ConsoleKey.A)
            (dfb, save, searchOption) = (null, true, SearchOption.TopDirectoryOnly);
        else if (consoleKey == ConsoleKey.B)
            (dfb, save, searchOption) = (null, true, SearchOption.AllDirectories);
        else if (consoleKey == ConsoleKey.C)
            (dfb, save, searchOption) = (null, false, SearchOption.TopDirectoryOnly);
        else if (consoleKey == ConsoleKey.D)
            (dfb, save, searchOption) = (null, false, SearchOption.AllDirectories);
        else
            throw new NotSupportedException();
        for (int i = 0; i < short.MaxValue; i++)
        {
            if (dfb is null or not ConsoleKey.D and not ConsoleKey.F and not ConsoleKey.B)
            {
                log.LogInformation("D) Directory, F) File or B) Both?");
                dfb = Console.ReadKey().Key;
                log.LogInformation("{empty}", string.Empty);
                continue;
            }
#pragma warning disable IDE0072
            string fileName = dfb switch
            {
                ConsoleKey.D => "Directories",
                ConsoleKey.F => "Files",
                ConsoleKey.B => "Both",
                _ => throw new NotSupportedException(),
            };
#pragma warning restore IDE0072
            string filePathAndName = Path.Combine(parentDirectory, $"{fileName}.txt");
            if (dfb == ConsoleKey.F)
                collection = Directory.GetFiles(argsZero, "*", searchOption).ToList();
            else if (dfb == ConsoleKey.D)
                collection = Directory.GetDirectories(argsZero, "*", searchOption).ToList();
            else if (dfb == ConsoleKey.B)
            {
                collection = Directory.GetDirectories(argsZero, "*", searchOption).ToList();
                collection.AddRange(Directory.GetFiles(argsZero, "*", searchOption));
            }
            else
                throw new NotSupportedException();
            TimeSpan timeSpan = new(DateTime.Now.Ticks - now);
            log.LogInformation("{TotalSeconds} TotalSeconds", timeSpan.TotalSeconds);
            if (save)
            {
                File.WriteAllLines(filePathAndName, collection);
                log.LogInformation("Data written");
            }
            else
            {
                string text = string.Join(Environment.NewLine, collection);
                TextCopy.ClipboardService.SetText(text);
                log.LogInformation("Data stored in clipboard");
            }
            log.LogInformation("Press any key to close");
            break;
        }
    }

    internal static void IgnoreCaseAndRenameFilesToLowercase(ILogger log, string argsZero)
    {
        string fileName;
        int filesRenamed = 0;
        string[] files = Directory.GetFiles(argsZero, "*", SearchOption.TopDirectoryOnly);
        foreach (string file in files)
        {
            fileName = Path.GetFileName(file);
            if (fileName.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
                continue;
            File.Move(file, file.ToLower());
            filesRenamed++;
        }
        log.LogInformation("{filesRenamed}(s) renamed", filesRenamed);
    }

}