#region Using Statements

using System;
using System.IO;

#endregion

namespace OneDriveArchiver
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            const string appName = "OneDriverArchiver";
            var targetPath = string.Empty;
            
            Console.WriteLine($"{appName} v.1.0");
            Console.WriteLine("(c) 2020 JR IT Services");
            
            if (args.Length < 1)
            {
                // handle improper arguments
                Console.WriteLine(
                    $"{appName}: Please call me with {appName} sourceDirectory [optional]targetDirectory.");
            }
            else
            {
                if (string.IsNullOrEmpty(args[0]))
                {
                    Console.WriteLine("Please specify a source directory.");
                }

                // check if the directory to process really exists
                if (!Directory.Exists(args[0]))
                {
                    Console.WriteLine($"{appName}: The source directory '{args[0]}' does not exist. Please specify an existing one.");
                }
                else
                {
                    // normal processing...
                    Console.WriteLine($"{appName}: starting up...");

                    // set target path
                    if (args.Length == 1)
                    {
                        targetPath = Path.GetFullPath(".");
                    }
                    else
                    {
                        targetPath = args[1];
                    }
                    
                    var startUpTime = DateTime.Now;

                    var filesToProcess = Directory.GetFiles(args[0]);
                    if (filesToProcess.Length > 1)
                    {
                        foreach (var item in filesToProcess)
                        {
                            Console.WriteLine($"{appName}: Processing file {item}...");

                            // skip ourselves...
                            if (item.ToLower().Contains("onedrivearchiver"))
                            {
                                Console.WriteLine(
                                    $"{appName}: Skipping processing of my own executable. Proceeding...");
                                continue;
                            }
                            
                            // we do not archive desktop.ini either...
                            if (item.ToLower().Contains("desktop.ini"))
                            {
                                Console.WriteLine($"{appName}: skipping desktop.ini file...");
                                continue;
                            }

                            // archive the file
                            IoHelper.ArchiveOneDriveFile(appName, targetPath,item);

                            Console.WriteLine($"{appName}: Processing for file {item} completed successfully.");
                        }
                    }
                    else
                    {
                        // no files found...
                        Console.WriteLine($"{appName}: No files found to process.");
                    }

                    var endProcessingTime = DateTime.Now;
                    var processingTime = endProcessingTime - startUpTime;

                    Console.WriteLine(
                        $"{appName}: Processing completed.\n{appName}: Processing time = {processingTime}\n{appName}: Number of files = {filesToProcess.Length}.");
                }
            }
        }
    }
}