public static Ticket Parse(FileStream file, FileInfo info, SVCFirebase loggingSvc)
        {
            try
            {
                //Read it all
                var ticketRaw = File.ReadAllText(info.FullName);

                //Extract the line with the order number and the order type
                var delim      = "Order #: ";
                var delimStart = ticketRaw.IndexOf(delim);
                var delimEnd   = ticketRaw.IndexOf("\n", delimStart);
                var rawInfo    = ticketRaw.Substring(delimStart + delim.Length, delimEnd - delimStart - delim.Length);

                //Extract the order number
                var firstSpace  = rawInfo.IndexOf(" ");
                var orderNumStr = rawInfo.Substring(0, firstSpace);

                //Extract order kind
                var excludeOrderNum = rawInfo.Substring(firstSpace, rawInfo.Length - firstSpace);
                var kind            = excludeOrderNum.Trim(' ', '\r', '\n').Replace(" ", "");

                //Extract the date
                DateTime?date = null;
                try
                {
                    var dateRegX = new Regex(@"\d+\/\d+\/\d+ \d+:\d+:\d+ (AM|PM)");
                    var match    = dateRegX.Match(ticketRaw);
                    date = DateTime.Parse(match.ToString());
                }
                catch (Exception e)
                {
                    loggingSvc.LogEvent("TicketParserError: " + e.Message);
                }
                return(new Ticket {
                    OrderNumber = orderNumStr, OrderKind = kind, Timestamp = date
                });
            }
            catch (Exception e)
            {
                loggingSvc.LogEvent("TicketParser Error: " + e.Message);
            }
            return(null);
        }
        public static Credentials GetCreds(SVCFirebase loggingSVC)
        {
            var path = GetCredDocPath();

            //Check if credentials file exists
            if (!File.Exists(path))
            {
                //If the file doesn't exist, try to create it
                try
                {
                    //Create the JSON template for someone to fill out later
                    var template = new Credentials {
                        Key = "", Email = "", Password = ""
                    };

                    //Create the file, write the JSON in, then close the file
                    File.WriteAllText(path, JsonConvert.SerializeObject(template));
                }
                catch (Exception e)
                {
                    loggingSVC.LogEvent("Couldn't create cred file because: " + e.Message);
                    return(null);
                }
            }
            //Read the cred file and extract creds
            try
            {
                var credFileContents = File.ReadAllText(path);
                if (DetectsPossibleEncryptionFor(credFileContents))
                {
                    credFileContents = EncryptionBlackBox(credFileContents);    //If possibly encrypted, try decrpyting
                }
                var result = JsonConvert.DeserializeObject <Credentials>(credFileContents);
                File.WriteAllText(path, EncryptionBlackBox(credFileContents));  //If deserialization worked, then re-encrpyt the file
                return(result);
            }
            catch (Exception e)
            {
                loggingSVC.LogEvent("Couldn't read cred file because: " + e.Message);
                return(null);
            }
        }
        private async Task ProcessAsync(FileStream file, FileInfo info)
        {
            //Only process .SHD and .SPL files
            if (info.Extension != ".SHD" && info.Extension != ".SPL")
            {
                file.Close();
                return;
            }
            var shouldDelete = false;

            if (info.Length < 3)
            {
                shouldDelete = true;    //Don't even bother with tiny files. Sometimes random 1 byte files make it to the spool file, so just throw them away
            }
            else if (info.Extension == ".SPL")
            {
                Ticket ticketInfo = null;
                try
                {
                    file.Close();
                    ticketInfo = TicketParser.Parse(file, info, loggingSvc: svc);
                    file       = File.Open(info.FullName, FileMode.Open);
                }
                catch (Exception e)
                {
                    svc.LogEvent("ProcessAsync could not reopen file because: " + e.Message);
                }
                if (ticketInfo != null)
                {
                    //Try to get the date to tell Firebase exactly where to upload the file
                    var uploadDirectory = "";
                    if (ticketInfo.Timestamp.HasValue)
                    {
                        var ticketTime = (DateTime)ticketInfo.Timestamp;
                        uploadDirectory = ticketTime.ToString("yyyy/MM/dd");
                    }
                    //Delete if upload is successful
                    shouldDelete = await svc.UploadAsync(file, info,
                                                         rename : ticketInfo.OrderNumber + "_" + ticketInfo.OrderKind + ".txt", //Change extension to .txt to make life easier
                                                         directory : uploadDirectory);
                }
                else
                {
                    //This is an error case, but I still want to see the file get uploaded
                    var guidTitle = Guid.NewGuid();
                    shouldDelete = await svc.UploadAsync(file, info, rename : guidTitle + ".txt");  //At this point it had to be either a .SHD or .SPL file... Sometimes .SHDs get converted to .SPLs and I don't know why. (That kills the ticket parser)

                    svc.LogEvent("Null ticket info for " + info.Name + ", uploaded as " + guidTitle + ".txt" + " with size (bytes): " + info.Length.ToString());
                }
            }
            else if (info.Extension == ".SHD")
            {
                shouldDelete = true;    //Delete all .SHD no matter so the computer's spool folder doesn't get gunked up
            }
            file.Close();

            if (shouldDelete)
            {
                File.Delete(info.FullName);
            }
        }