private void SelectedGroupByMatch(ImageListView imageListView,
                                          bool checkFileCreated, bool checkMediaTaken, bool checkAllDates, int maxDayRange,
                                          bool checkLocationName, bool checkLocationCity, bool checkLocationDistrict, bool checkLocationCountry, bool checkAllLocations)
        {
            try
            {
                ImageListViewHandler.SuspendLayout(imageListView1);
                ImageListViewHandler.Enable(imageListView, false);
                TreeViewFolderBrowserHandler.Enabled(treeViewFolderBrowser1, false);
                GlobalData.DoNotTrigger_ImageListView_SelectionChanged = true;

                #region Do the work
                using (new WaitCursor())
                {
                    ImageListViewItemCollection imageListViewItems = imageListView.Items;

                    List <GroupMacth> groupMacthSourceList = new List <GroupMacth>();


                    #region Init
                    foreach (ImageListViewItem imageListViewItem in imageListView.SelectedItems)
                    {
                        Metadata metadata = databaseAndCacheMetadataExiftool.ReadMetadataFromCacheOnly(new FileEntryBroker(imageListViewItem.FileFullPath, imageListViewItem.DateModified, MetadataBrokerType.ExifTool));

                        GroupMacth groupMacthSource = new GroupMacth();
                        groupMacthSource.IsMetadataNull   = (metadata == null);
                        groupMacthSource.FileDate         = imageListViewItem.Date;
                        groupMacthSource.MediaTaken       = metadata?.MediaDateTaken;
                        groupMacthSource.LocationName     = metadata?.LocationName;
                        groupMacthSource.LocationCity     = metadata?.LocationCity;
                        groupMacthSource.LocationDistrict = metadata?.LocationState;
                        groupMacthSource.LocationCountry  = metadata?.LocationCountry;
                        groupMacthSourceList.Add(groupMacthSource);
                    }
                    #endregion


                    imageListView.ClearSelection();

                    #region Find and Select
                    foreach (ImageListViewItem imageListViewItem in imageListView.Items)
                    {
                        Metadata metadata = databaseAndCacheMetadataExiftool.ReadMetadataFromCacheOnly(new FileEntryBroker(imageListViewItem.FileFullPath, imageListViewItem.DateModified, MetadataBrokerType.ExifTool));

                        GroupMacth groupMacthCheckWith = new GroupMacth();
                        groupMacthCheckWith.IsMetadataNull   = (metadata == null);
                        groupMacthCheckWith.FileDate         = imageListViewItem.Date;
                        groupMacthCheckWith.MediaTaken       = metadata?.MediaDateTaken;
                        groupMacthCheckWith.LocationName     = metadata?.LocationName;
                        groupMacthCheckWith.LocationCity     = metadata?.LocationCity;
                        groupMacthCheckWith.LocationDistrict = metadata?.LocationState;
                        groupMacthCheckWith.LocationCountry  = metadata?.LocationCountry;

                        bool isItemsEqual = false;

                        foreach (GroupMacth groupMacthSource in groupMacthSourceList)
                        {
                            isItemsEqual = GroupMacth.IsMatch(groupMacthSource, groupMacthCheckWith, checkFileCreated, checkMediaTaken,
                                                              checkAllDates, maxDayRange,
                                                              checkLocationName, checkLocationCity, checkLocationDistrict, checkLocationCountry, checkAllLocations);
                            if (isItemsEqual)
                            {
                                break;
                            }
                        }

                        if (isItemsEqual)
                        {
                            imageListViewItem.Selected = true;
                        }
                        else
                        {
                            imageListViewItem.Selected = false;
                        }
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                KryptonMessageBox.Show("Unexpected error occur.\r\nException message:" + ex.Message + "\r\n",
                                       "Unexpected error occur", MessageBoxButtons.OK, MessageBoxIcon.Error, showCtrlCopy: true);
            }
            finally
            {
                GlobalData.DoNotTrigger_ImageListView_SelectionChanged = false;
                TreeViewFolderBrowserHandler.Enabled(treeViewFolderBrowser1, true);
                ImageListViewHandler.Enable(imageListView, true);
                ImageListViewHandler.ResumeLayout(imageListView1);

                imageListView.Focus();

                ImageListView_SelectionChanged_Action_ImageListView_DataGridView(false);
            }
        }
        public static bool IsMatch(GroupMacth groupMacthSource, GroupMacth groupMacthCheckWith,
                                   bool checkFileCreated, bool checkMediaTaken, bool checkAllDates, int maxDayRange,
                                   bool checkLocationName, bool checkLocationCity, bool checkLocationDistrict, bool checkLocationCountry, bool checkAllLocations)
        {
            bool isItemsEqual = true;

            if (groupMacthSource.IsMetadataNull != groupMacthCheckWith.IsMetadataNull)
            {
                isItemsEqual = false;
            }

            bool didAnyCheckedDatesMissmatch = false;
            bool didAnyCheckedDatesMatch     = false;

            if (checkFileCreated)
            {
                if (DateRangeAbs(groupMacthCheckWith.FileDate, groupMacthSource.FileDate) > maxDayRange)
                {
                    didAnyCheckedDatesMissmatch = true;
                }
                else
                {
                    didAnyCheckedDatesMatch = true;
                }
            }
            if (checkMediaTaken)
            {
                if (DateRangeAbs(groupMacthCheckWith.MediaTaken, groupMacthSource.MediaTaken) > maxDayRange)
                {
                    didAnyCheckedDatesMissmatch = true;
                }
                else
                {
                    didAnyCheckedDatesMatch = true;
                }
            }
            if (checkAllDates && didAnyCheckedDatesMissmatch)
            {
                isItemsEqual = false;
            }
            if (checkFileCreated || checkMediaTaken)
            {
                if (!checkAllDates && !didAnyCheckedDatesMatch)
                {
                    isItemsEqual = false;
                }
            }

            bool didAnyCheckedLocationMissmatch = false;
            bool didAnyCheckedLocationMatch     = false;

            if (checkLocationName)
            {
                if (groupMacthCheckWith.LocationName != groupMacthSource.LocationName)
                {
                    didAnyCheckedLocationMissmatch = true;
                }
                else
                {
                    didAnyCheckedLocationMatch = true;
                }
            }
            if (checkLocationCity)
            {
                if (groupMacthCheckWith.LocationCity != groupMacthSource.LocationCity)
                {
                    didAnyCheckedLocationMissmatch = true;
                }
                else
                {
                    didAnyCheckedLocationMatch = true;
                }
            }
            if (checkLocationDistrict)
            {
                if (groupMacthCheckWith.LocationDistrict != groupMacthSource.LocationDistrict)
                {
                    didAnyCheckedLocationMissmatch = true;
                }
                else
                {
                    didAnyCheckedLocationMatch = true;
                }
            }
            if (checkLocationCountry)
            {
                if (groupMacthCheckWith.LocationCountry != groupMacthSource.LocationCountry)
                {
                    didAnyCheckedLocationMissmatch = true;
                }
                else
                {
                    didAnyCheckedLocationMatch = true;
                }
            }
            if (checkAllLocations && didAnyCheckedLocationMissmatch)
            {
                isItemsEqual = false;
            }
            if (checkLocationName || checkLocationCity || checkLocationDistrict || checkLocationCountry)
            {
                if (!checkAllLocations && !didAnyCheckedLocationMatch)
                {
                    isItemsEqual = false;
                }
            }

            return(isItemsEqual);
        }
        private void SelectedGroupBySelections(ImageListView imageListView, int baseItemIndex, int direction, int maxSelectCount,
                                               bool checkFileCreated, bool checkMediaTaken, bool checkAllDates, int maxDayRange,
                                               bool checkLocationName, bool checkLocationCity, bool checkLocationDistrict, bool checkLocationCountry, bool checkAllLocations)
        {
            try
            {
                ImageListViewHandler.SuspendLayout(imageListView1);
                ImageListViewHandler.Enable(imageListView, false);
                TreeViewFolderBrowserHandler.Enabled(treeViewFolderBrowser1, false);
                GlobalData.DoNotTrigger_ImageListView_SelectionChanged = true;

                #region Do the work
                using (new WaitCursor())
                {
                    ImageListViewItemCollection imageListViewItems = imageListView.Items;
                    if (baseItemIndex < imageListViewItems.Count && direction != 0)
                    {
                        #region Init Variables
                        GroupMacth groupMacthSource = new GroupMacth();

                        ImageListViewItem imageListViewItem = imageListViewItems[baseItemIndex];
                        Metadata          metadata          = databaseAndCacheMetadataExiftool.ReadMetadataFromCacheOnly(new FileEntryBroker(imageListViewItem.FileFullPath, imageListViewItem.DateModified, MetadataBrokerType.ExifTool));

                        groupMacthSource.IsMetadataNull   = (metadata == null);
                        groupMacthSource.FileDate         = imageListViewItem.Date;
                        groupMacthSource.MediaTaken       = metadata?.MediaDateTaken;
                        groupMacthSource.LocationName     = metadata?.LocationName;
                        groupMacthSource.LocationCity     = metadata?.LocationCity;
                        groupMacthSource.LocationDistrict = metadata?.LocationState;
                        groupMacthSource.LocationCountry  = metadata?.LocationCountry;
                        #endregion

                        imageListView.ClearSelection();

                        #region Find and Select
                        int selectedCount = 0;
                        int itemIndex     = baseItemIndex;
                        while (itemIndex > -1 && itemIndex < imageListViewItems.Count && selectedCount < maxSelectCount)
                        {
                            imageListViewItem = imageListViewItems[itemIndex];
                            metadata          = databaseAndCacheMetadataExiftool.ReadMetadataFromCacheOnly(new FileEntryBroker(imageListViewItem.FileFullPath, imageListViewItem.DateModified, MetadataBrokerType.ExifTool));

                            GroupMacth groupMacthCheckWith = new GroupMacth();
                            groupMacthCheckWith.IsMetadataNull   = (metadata == null);
                            groupMacthCheckWith.FileDate         = imageListViewItem.Date;
                            groupMacthCheckWith.MediaTaken       = metadata?.MediaDateTaken;
                            groupMacthCheckWith.LocationName     = metadata?.LocationName;
                            groupMacthCheckWith.LocationCity     = metadata?.LocationCity;
                            groupMacthCheckWith.LocationDistrict = metadata?.LocationState;
                            groupMacthCheckWith.LocationCountry  = metadata?.LocationCountry;


                            bool isItemsEqual = GroupMacth.IsMatch(groupMacthSource, groupMacthCheckWith, checkFileCreated, checkMediaTaken,
                                                                   checkAllDates, maxDayRange,
                                                                   checkLocationName, checkLocationCity, checkLocationDistrict, checkLocationCountry, checkAllLocations);

                            if (isItemsEqual)
                            {
                                selectedCount++;
                                imageListViewItem.Selected = true;
                            }
                            else
                            {
                                imageListViewItem.Selected = false;
                            }

                            itemIndex += direction;
                        }
                        #endregion

                        imageListView.EnsureVisible(itemIndex - direction);
                        imageListView.EnsureVisible(baseItemIndex);
                    }
                    lastGroupBaseIndex = baseItemIndex;
                }
                #endregion
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                KryptonMessageBox.Show("Unexpected error occur.\r\nException message:" + ex.Message + "\r\n",
                                       "Unexpected error occur", MessageBoxButtons.OK, MessageBoxIcon.Error, showCtrlCopy: true);
            }
            finally
            {
                GlobalData.DoNotTrigger_ImageListView_SelectionChanged = false;
                TreeViewFolderBrowserHandler.Enabled(treeViewFolderBrowser1, true);
                ImageListViewHandler.Enable(imageListView, true);
                ImageListViewHandler.ResumeLayout(imageListView1);

                imageListView.Focus();
                ImageListView_SelectionChanged_Action_ImageListView_DataGridView(false);
            }
        }