/// <summary>
 /// Removes the layer maps present in affected layers from the workbook map
 /// </summary>
 /// <param name="workbookMap">The workbook map which is in scope</param>
 /// <param name="affectedLayerList">The list of layers that need to be removed from the workbook map</param>
 internal static void RemoveAffectedLayers(this WorkbookMap workbookMap, List <LayerMap> affectedLayerList)
 {
     if (workbookMap != null && affectedLayerList != null)
     {
         foreach (LayerMap layerMap in affectedLayerList)
         {
             if (workbookMap.Exists(layerMap.LayerDetails.ID))
             {
                 workbookMap.AllLayerMaps.Remove(layerMap);
             }
         }
     }
 }
        /// <summary>
        /// This function is used to Add WWT layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        /// <param name="group">
        /// Group in which the layer is present.
        /// </param>
        private static void AddWWTLayers(WorkbookMap workbookMap, Group group)
        {
            foreach (string layerID in group.LayerIDs)
            {
                if (!workbookMap.Exists(layerID))
                {
                    Layer wwtLayer = WWTManager.GetLayerDetails(layerID, group, true);
                    if (wwtLayer != null)
                    {
                        workbookMap.AllLayerMaps.Add(new LayerMap(wwtLayer));
                    }
                }
            }

            foreach (Group child in group.Children)
            {
                AddWWTLayers(workbookMap, child);
            }
        }
        /// <summary>
        /// This function is used to Cleanup WWT layers from layer drop down when WWT is not running.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. Mark all Local IN WWT layer as not in sync with WWT.
        ///     3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void CleanUpWWTLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                // 1. Create en empty collection, since this method is called only when WWT is not running. Empty collection is needed
                //    to be passed for UpdateGroupStatus method.
                ICollection <Group> groups           = new List <Group>();
                LayerMap            selectedLayerMap = workbookMap.SelectedLayerMap;

                // 2. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                // 3. On Cleanup, no WWT layers are connected with WWT.
                workbookMap.LocalLayerMaps.ForEach(
                    layer =>
                {
                    // 3.a. Set IsNotInSync.
                    layer.IsNotInSync = true;

                    // 3.b. If the group (reference frame/ Layer Group) is deleted in WWT, then set the IsDeleted flag in Group to true.
                    UpdateGroupStatus(layer.LayerDetails.Group, groups);
                });

                // 4. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        /// <summary>
        /// This function is used to refresh the layer drop down with the latest layers from WWT.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. Insert and update deleted WWT Layers.
        ///     3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void RefreshLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                LayerMap            selectedLayerMap = workbookMap.SelectedLayerMap;
                ICollection <Group> groups           = WWTManager.GetAllWWTGroups(true);

                // 1. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                // 2. Insert and update deleted WWT Layers
                workbookMap.SyncWWTLayers(groups);

                // 3. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        /// <summary>
        /// This function is used to Load all WWT layers on the load of a workbook.
        /// This function performs the following operations:
        ///     1. Remove all WWT only layers.
        ///     2. On load of a workbook, no layers are connected with WWT.
        ///     3. Insert and update deleted WWT Layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        internal static void LoadWWTLayers(this WorkbookMap workbookMap)
        {
            if (workbookMap != null)
            {
                LayerMap selectedLayerMap = workbookMap.SelectedLayerMap;

                // 1. Remove all WWT only layers.
                workbookMap.RemoveWWTLayers();

                ICollection <Group> groups = WWTManager.GetAllWWTGroups(true);

                // 2. On load of a workbook, no layers are connected with WWT.
                workbookMap.LocalInWWTLayerMaps.ForEach(
                    layer =>
                {
                    // 2.a. Set IsNotInSync.
                    layer.IsNotInSync = true;

                    // 2.b. Update all group references.
                    Group group = SearchGroup(layer.LayerDetails.Group.Name, layer.LayerDetails.Group.Path, groups);
                    if (group != null)
                    {
                        layer.LayerDetails.Group = group;
                    }
                    else
                    {
                        layer.LayerDetails.Group.IsDeleted = true;
                    }
                });

                // 3. Synchronize WWT Layers
                workbookMap.SyncWWTLayers(groups);

                // 4. Set the Selected layer map to null if the Selected WWT layer was deleted in WWT.
                //  If the selected layer is WWT layer/is localInWWT which is not deleted, this will be selected while
                if (selectedLayerMap != null && selectedLayerMap.MapType == LayerMapType.WWT && !workbookMap.Exists(selectedLayerMap.LayerDetails.ID))
                {
                    workbookMap.SelectedLayerMap = null;
                }
            }
        }
        /// <summary>
        /// This function is used to Add WWT layers.
        /// </summary>
        /// <param name="workbookMap">
        /// Parent container.
        /// </param>
        /// <param name="group">
        /// Group in which the layer is present.
        /// </param>
        private static void AddWWTLayers(WorkbookMap workbookMap, Group group)
        {
            foreach (string layerID in group.LayerIDs)
            {
                if (!workbookMap.Exists(layerID))
                {
                    Layer wwtLayer = WWTManager.GetLayerDetails(layerID, group, true);
                    if (wwtLayer != null)
                    {
                        workbookMap.AllLayerMaps.Add(new LayerMap(wwtLayer));
                    }
                }
            }

            foreach (Group child in group.Children)
            {
                AddWWTLayers(workbookMap, child);
            }
        }