/// <summary>
        /// assume that the selected folder will contain subfolders with numbered folder names.
        /// GPS that all the folders will refer will be similarly named, but with differing numbers
        /// The number will point to a specific GPS so folder names need not be precise. The number will be concatenated to
        /// a string that referes to an LGU so CON 11 which then points to a GPS already stored in the database
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="in_gps"></param>
        /// <param name="gpsNameStart">starting part of a gps name for example CON = concepcion </param>
        /// <param name="first"></param>
        /// <param name=""></param>
        /// <returns></returns>


        private static async Task ImportGPXByFolderAsync(string folder, GPS in_gps = null, bool?first = false, int yearStartProcess = 2019)
        {
            EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;

            if (importEvent != null)
            {
                ImportGPXEventArg e = new ImportGPXEventArg
                {
                    Intent = "start"
                };

                importEvent(null, e);
            }
            await Task.Run(() => ImportGPXByFolder(folder, in_gps, first, yearStartProcess));

            Logger.LogType = LogType.Logfile;
        }
        private static int ImportGPXByFolder(string folder, GPS in_gps = null, bool?first = false, int yearStartProcess = 2019)
        {
            if ((bool)first)
            {
                Logger.LogType = LogType.ImportGPXfFromFolder;
                _count         = 0;
                _gpxCount      = 0;
            }

            GPS gps         = null;
            GPS current_gps = null;

            if (in_gps != null)
            {
                gps         = in_gps;
                current_gps = in_gps;
            }
            //Logger.Log($"processing folder: {folder}");

            var folderName = System.IO.Path.GetFileName(folder);
            //if (ImportGPSData.GPSNameStart.Length > 0)
            //{

            string result = GetNumericPartOfFolderName(folderName);

            if (result.Length > 0)
            {
                int numericPart = int.Parse(result);
                if (numericPart >= ImportGPSData.StartGPSNumbering && numericPart <= ImportGPSData.EndGPSNumbering)
                {
                    gps = Entities.GPSViewModel.GetGPS($"{ImportGPSData.GPSNameStart} {GetNumericPartOfFolderName(folderName)}");
                }
                else
                {
                    return(0);
                }
            }
            //int numericPart = int.Parse(GetNumericPartOfFolderName(folderName));

            //}
            //else
            //{
            //    gps = Entities.GPSViewModel.GetGPSByName(folderName);
            //}

            if (gps != null)
            {
                current_gps = gps;
            }
            else if (gps == null && in_gps != null)
            {
                current_gps = in_gps;
            }


            var files = Directory.GetFiles(folder).Select(s => new FileInfo(s));

            if (files.Any())
            {
                foreach (var file in files)
                {
                    if (file.Extension.ToLower() == ".gpx")
                    {
                        //var folderName = System.IO.Path.GetFileName(folder);
                        //if(ImportGPSData.GPSNameStart.Length>0)
                        //{

                        //    string result = GetNumericPartOfFolderName(folderName);
                        //    if (result.Length > 0)
                        //    {
                        //        int numericPart = int.Parse(result);
                        //        if (numericPart >= ImportGPSData.StartGPSNumbering && numericPart <= ImportGPSData.EndGPSNumbering)
                        //        {
                        //            gps = Entities.GPSViewModel.GetGPS($"{ImportGPSData.GPSNameStart} {GetNumericPartOfFolderName(folderName)}");
                        //        }
                        //        else
                        //        {
                        //            return 0;
                        //        }
                        //    }
                        //    //int numericPart = int.Parse(GetNumericPartOfFolderName(folderName));

                        //}
                        //else {
                        //    gps = Entities.GPSViewModel.GetGPSByName(folderName);
                        //}

                        //if (gps != null)
                        //{
                        //    current_gps = gps;
                        //}
                        //else if (gps == null && in_gps != null)
                        //{
                        //    current_gps = in_gps;
                        //}

                        if (current_gps != null)
                        {
                            _gpxCount++;
                            GPXFile g = new GPXFile(file);
                            g.GPS = current_gps;
                            if (g.ComputeStats())
                            {
                                if (g.DateRangeStart.Year >= yearStartProcess)
                                {
                                    DeviceGPX d = new DeviceGPX
                                    {
                                        Filename       = file.Name,
                                        GPS            = current_gps,
                                        GPX            = g.XML,
                                        GPXType        = g.GPXFileType == GPXFileType.Track ? "track" : "waypoint",
                                        RowID          = Entities.DeviceGPXViewModel.NextRecordNumber,
                                        MD5            = CreateMD5(g.XML),
                                        TimeRangeStart = g.DateRangeStart,
                                        TimeRangeEnd   = g.DateRangeEnd
                                    };

                                    string fileProcessed = $@"{current_gps.DeviceName}:{file.FullName}";


                                    DeviceGPX saved = Entities.DeviceGPXViewModel.GetDeviceGPX(d);
                                    if (saved == null)
                                    {
                                        if (Entities.DeviceGPXViewModel.AddRecordToRepo(d))
                                        {
                                            _count++;
                                            fileProcessed += "  (ADDED)";

                                            EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                            if (importEvent != null)
                                            {
                                                ImportGPXEventArg e = new ImportGPXEventArg
                                                {
                                                    Intent        = "gpx saved",
                                                    GPS           = current_gps,
                                                    ImportedCount = _count,
                                                    GPXFileName   = file.Name
                                                };

                                                importEvent(null, e);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (saved.MD5 != d.MD5 && d.TimeRangeEnd > saved.TimeRangeEnd)
                                        {
                                            if (Entities.DeviceGPXViewModel.UpdateRecordInRepo(d))
                                            {
                                                EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                                if (importEvent != null)
                                                {
                                                    ImportGPXEventArg e = new ImportGPXEventArg
                                                    {
                                                        Intent        = "gpx file modified",
                                                        GPS           = current_gps,
                                                        ImportedCount = _count,
                                                        GPXFileName   = file.Name
                                                    };

                                                    importEvent(null, e);
                                                }
                                            }
                                            fileProcessed += " (MODIFIED ADDED)";
                                        }
                                        else
                                        {
                                            EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                            if (importEvent != null)
                                            {
                                                ImportGPXEventArg e = new ImportGPXEventArg
                                                {
                                                    Intent        = "gpx file duplicate",
                                                    GPS           = current_gps,
                                                    ImportedCount = _count,
                                                    GPXFileName   = file.Name
                                                };

                                                importEvent(null, e);
                                            }
                                            fileProcessed += "  (DUPLICATE)";
                                        }
                                    }
                                    //Console.WriteLine(fileProcessed);
                                    Logger.Log(fileProcessed);
                                }
                                else
                                {
                                    Logger.Log($"GPX file {file.FullName} time is {g.DateRangeEnd.ToString("MMM-dd-yyyy")} and is not saved");
                                }
                            }
                            else
                            {
                                Logger.Log($"Error computing stats for GPX file {file.FullName} and is not saved");
                            }
                        }
                    }
                }
            }

            foreach (var dir in Directory.GetDirectories(folder))
            {
                ImportGPXByFolder(dir, current_gps);
            }
            return(_count);
        }