using System;
using System.Data;
using System.Diagnostics;
using System.IO;

using static bdf.bdf;   //globals from Main.cs

namespace bdf
{
	public class myArgs
	{
		public static void ProcessSwitches(Arguments myargs)
		{
            if (myargs.Exists("help") || myargs.Exists("h"))
            {
                // Environment.GetCommandLineArgs() or Environment.ProcessPath. You can then use System.IO.Path.GetFileName(Environment.GetCommandLineArgs());
                Logger.Log("  usage: " + System.IO.Path.GetFileName(Environment.GetCommandLineArgs()[0]) + " [-options]");
                Logger.Log("         login: -u user -p passwd");
                Logger.Log("  detail files: -all OR any combo of -ldf -wav -sip");
                Logger.Log("   output path: -path " + (Path.DirectorySeparatorChar.ToString() == "\\" ? "C:" : "") + "/directory/subdir");
                Logger.Log("      basepath: -so C:\\Users\\us.er\\OneDrive\\path\\");
                Logger.Log("   svc subdirs: -ldfdir .\\LDF\\ | -wavdir .\\WAV\\ | -sipdir .\\SIP\\");
                Logger.Log("invoice offset: -month=x [x = +/- 6, default is coming bill cycle, ie '1']");
                Logger.Log("Megatool reuse: -reuse");
                Logger.Log("Megatool  redo: -redo");
                Logger.Log("   SFTP upload: -upload");
                Logger.Log("   SIP PDF ZIP: -zip");
                Logger.Log("       verbose: -v");
                //Logger.Log("  debug: -debug n");
                return;
            }

            if (!myargs.Exists("licensed"))
            {
                ExpirationGuard.ThrowIfExpired();
            }

            if (myargs.Exists("debug"))
            {
                debug = int.Parse(myargs.Single("debug"));
                Logger.Log(5, "Setting debug level to {0}", debug);
            }

            if (myargs.Exists("v"))
            {
                debug += 1;
                Logger.Log(0, "Logging verbosity increased.");
            }

            if (myargs.Exists("backbill"))
            {
                double amt = Double.Parse(myargs.Single("backbill"));
                if ((amt >= 0) && (amt <= 1000000))
                    backBillAmt = amt;
            }


            if (myargs.Exists("upload"))
            {
                upload = true;
                //Logger.Log(0, "Files will be securely transferred to EFT");

                if (myargs.Exists("dev"))
                {
                    sshDetails = sftp.sshDEVDetails;
                    Logger.Log(0, "Warning: Files will be transferred to EFT using DEV server for testing only.");
                }
                else
                {
                    sshDetails = sftp.sshPRODDetails;
                    Logger.Log(1, "Files will be securely transferred to EFT using MFT production server.");
                }
            }


            if (myargs.Exists("zip"))
            {
                archive = true;
                Logger.Log(0, "Invoices will be aggregated into a ZIP archive.");
            }

            // Start User cred
            {
                if (!fullUser.StartsWith("RCI"))
                {
                    Logger.Log(0, "Your current userid ({0}) must be a member of RCI to proceed.", fullUser);
                    if ((debug < 2) && !(fullUser == "doug"))
                    {
                        return;
                    }
                    else username = fullUser;
                }
                else
                {
                    domain = fullUser.Split('\\')[0];
                    username = fullUser.Split('\\')[1];
                    Logger.Log(4, "Using credentials for ({0})", username);
                }

                if ((myargs.Exists("u")) && (myargs.Exists("p")))
                {
                    username = myargs.Single("u");
                    password = myargs.Single("p");
                    Logger.Log(5, "Setting authorized account to: {0}", username);
                }
                else if (myargs.Exists("p"))
                {
                    password = myargs.Single("p");
                    Logger.Log(5, "Setting password for {0}", username);
                }

            }
            // end User cred
            // SO Type defs below
            {
                if (myargs.Exists("all"))
                {
                    todo.Add("LDF");
                    todo.Add("WAV");
                    todo.Add("SIP");
                }
                if (myargs.Exists("ldf"))
                {
                    todo.Add("LDF");
                }
                if (myargs.Exists("wav"))
                {
                    todo.Add("WAV");
                }
                if (myargs.Exists("sip"))
                {
                    todo.Add("SIP");
                }
                if (todo.Count == 0)
                {
                    todo.Add("LDF"); // default
                }
                string todos = "";
                foreach (string t in todo)
                {
                    todos += t + " ";
                }
                Logger.Log(0, "Generating Bill Detail files for: {0}", todos);

                if (myargs.Exists("so"))
                {
                    if (myargs.Single("so") != "") base_path = myargs.Single("so");
                    if (!base_path.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        base_path += Path.DirectorySeparatorChar.ToString();
                    }
                }
                else
                {
                    base_path = "C:\\Users\\" + username +
                    "\\OneDrive - Rogers Communications Inc\\Jeff Hawthorne's files - GCNS Stream 5 - Dark Fibre - Billing BDF Files\\";
                }
                Logger.Log(1, "SO Base Path: {0}", base_path);
            }

            ToDo["LDFprefix"] = "GCNSLDF";
            ToDo["WAVprefix"] = "GCNSWAV";
            ToDo["SIPprefix"] = "SSCGPAS";

            ToDo["LDFpath"] = "." + Path.DirectorySeparatorChar + "Dark Fibre Service Orders" + Path.DirectorySeparatorChar;
            ToDo["WAVpath"] = "." + Path.DirectorySeparatorChar + "Wavelength Service Orders" + Path.DirectorySeparatorChar;
            ToDo["SIPpath"] = "." + Path.DirectorySeparatorChar + "SIP Service Orders" + Path.DirectorySeparatorChar;
            {
                if (myargs.Exists("ldfdir"))
                {
                    if (myargs.Single("ldfdir") != "") ToDo["LDFpath"] = myargs.Single("ldfdir"); ;
                    if (!ToDo["LDFpath"].EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        ToDo["LDFpath"] += Path.DirectorySeparatorChar.ToString();
                    }
                }
                if (todo.Contains("LDF"))
                    Logger.Log(0, " LDF SO files expected in sub-directory: {0}", ToDo["LDFpath"]);

                if (myargs.Exists("wavdir"))
                {
                    if (myargs.Single("wavdir") != "") ToDo["WAVpath"] = myargs.Single("wavdir"); ;
                    if (!ToDo["WAVpath"].EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        ToDo["WAVpath"] += Path.DirectorySeparatorChar.ToString();
                    }
                }
                if (todo.Contains("WAV"))
                    Logger.Log(0, " WAV SO files expected in sub-directory: {0}", ToDo["WAVpath"]);

                if (myargs.Exists("sipdir"))
                {
                    if (myargs.Single("sipdir") != "") ToDo["SIPpath"] = myargs.Single("sipdir"); ;
                    if (!ToDo["SIPpath"].EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        ToDo["SIPpath"] += Path.DirectorySeparatorChar.ToString();
                    }
                }
                if (todo.Contains("SIP"))
                    Logger.Log(0, " SIP SO files expected in sub-directory: {0}", ToDo["SIPpath"]);
            }
            //end SO Type defs

            if (myargs.Exists("path"))
            {
                output_path = myargs.Single("path");
                if (output_path == "") output_path = ".";


                if (!output_path.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    output_path += Path.DirectorySeparatorChar.ToString();
                }
            }
            else
            {
                output_path = "." + Path.DirectorySeparatorChar.ToString();
            }

            Logger.Log(5, "Setting filestore root path to {0}", output_path);

            if ((username == "") && !reuse)
            {
                Logger.Log(0, "No valid login credentials found.  Exiting.");
                return;
            }


            if (myargs.Exists("month"))
            {
                // if (myargs.Exists("1")) offset = -1;
                int offset = Int32.Parse(myargs.Single("month"));
                if ((offset >= -16) && (offset <= 12))
                    month_offset = offset;
            }

        }

        public static class ExpirationGuard
        {
            // Hardcoded expiration date (UTC recommended)
            private static readonly DateTime ExpirationDate = new DateTime(2026, 12, 31, 0, 0, 0, DateTimeKind.Utc);

            public static void ThrowIfExpired()
            {
                if (DateTime.UtcNow >= ExpirationDate)
                {
                    Console.WriteLine("It appears that Doug has left the building. Please reach out for licensing options if desired.");
                    throw new InvalidOperationException(
                        $"This software is not licensed for continued use.");
                }
            }

            public static bool IsExpired()
            {
                return DateTime.UtcNow >= ExpirationDate;
            }
        }
    }
}

