// add an item to the subgroup with the given name
        void AddToSubgroup(object item, LiveShapingItem lsi, CollectionViewGroupInternal group, int level, object name, bool loading)
        {
            CollectionViewGroupInternal subgroup;
            int index = (loading && IsDataInGroupOrder) ? group.LastIndex : 0;

            // find the desired subgroup using the map
            object groupNameKey = GetGroupNameKey(name, group);

            if (((subgroup = group.GetSubgroupFromMap(groupNameKey) as CollectionViewGroupInternal) != null) &&
                group.GroupBy.NamesMatch(subgroup.Name, name))
            {
                // Try best to set the LastIndex. If not possible reset it to 0.
                group.LastIndex = (group.Items[index] == subgroup ? index : 0);

                // Recursively call the AddToSubgroups method on subgroup.
                AddToSubgroups(item, lsi, subgroup, level + 1, loading);
                return;
            }

            // find the desired subgroup using linear search
            for (int n = group.Items.Count; index < n; ++index)
            {
                subgroup = group.Items[index] as CollectionViewGroupInternal;
                if (subgroup == null)
                {
                    continue;           // skip children that are not groups
                }
                if (group.GroupBy.NamesMatch(subgroup.Name, name))
                {
                    group.LastIndex = index;

                    // Update the name to subgroup map on the group.
                    group.AddSubgroupToMap(groupNameKey, subgroup);

                    // Recursively call the AddToSubgroups method on subgroup.
                    AddToSubgroups(item, lsi, subgroup, level + 1, loading);
                    return;
                }
            }

            // the item didn't match any subgroups.  Create a new subgroup and add the item.
            subgroup = new CollectionViewGroupInternal(name, group);
            InitializeGroup(subgroup, group.GroupBy, level + 1);

            if (loading)
            {
                group.Add(subgroup);
                group.LastIndex = index;
            }
            else
            {
                group.Insert(subgroup, item, ActiveComparer);
            }

            // Update the name to subgroup map on the group.
            group.AddSubgroupToMap(groupNameKey, subgroup);

            // Recursively call the AddToSubgroups method on subgroup.
            AddToSubgroups(item, lsi, subgroup, level + 1, loading);
        }
        // Token: 0x060073D2 RID: 29650 RVA: 0x00212494 File Offset: 0x00210694
        private void AddToSubgroups(object item, LiveShapingItem lsi, CollectionViewGroupInternal group, int level, bool loading)
        {
            object groupName = this.GetGroupName(item, group.GroupBy, level);

            if (groupName == CollectionViewGroupRoot.UseAsItemDirectly)
            {
                if (lsi != null)
                {
                    lsi.AddParentGroup(group);
                }
                if (loading)
                {
                    group.Add(item);
                    return;
                }
                int index  = group.Insert(item, item, this.ActiveComparer);
                int index2 = group.LeafIndexFromItem(item, index);
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index2));
                return;
            }
            else
            {
                ICollection collection;
                if ((collection = (groupName as ICollection)) == null)
                {
                    this.AddToSubgroup(item, lsi, group, level, groupName, loading);
                    return;
                }
                foreach (object name in collection)
                {
                    this.AddToSubgroup(item, lsi, group, level, name, loading);
                }
                return;
            }
        }
        // Token: 0x060073D3 RID: 29651 RVA: 0x00212564 File Offset: 0x00210764
        private void AddToSubgroup(object item, LiveShapingItem lsi, CollectionViewGroupInternal group, int level, object name, bool loading)
        {
            int    i            = (loading && this.IsDataInGroupOrder) ? group.LastIndex : 0;
            object groupNameKey = this.GetGroupNameKey(name, group);
            CollectionViewGroupInternal collectionViewGroupInternal;

            if ((collectionViewGroupInternal = group.GetSubgroupFromMap(groupNameKey)) != null && group.GroupBy.NamesMatch(collectionViewGroupInternal.Name, name))
            {
                group.LastIndex = ((group.Items[i] == collectionViewGroupInternal) ? i : 0);
                this.AddToSubgroups(item, lsi, collectionViewGroupInternal, level + 1, loading);
                return;
            }
            int count = group.Items.Count;

            while (i < count)
            {
                collectionViewGroupInternal = (group.Items[i] as CollectionViewGroupInternal);
                if (collectionViewGroupInternal != null && group.GroupBy.NamesMatch(collectionViewGroupInternal.Name, name))
                {
                    group.LastIndex = i;
                    group.AddSubgroupToMap(groupNameKey, collectionViewGroupInternal);
                    this.AddToSubgroups(item, lsi, collectionViewGroupInternal, level + 1, loading);
                    return;
                }
                i++;
            }
            collectionViewGroupInternal = new CollectionViewGroupInternal(name, group, false);
            this.InitializeGroup(collectionViewGroupInternal, group.GroupBy, level + 1);
            if (loading)
            {
                group.Add(collectionViewGroupInternal);
                group.LastIndex = i;
            }
            else
            {
                group.Insert(collectionViewGroupInternal, item, this.ActiveComparer);
            }
            group.AddSubgroupToMap(groupNameKey, collectionViewGroupInternal);
            this.AddToSubgroups(item, lsi, collectionViewGroupInternal, level + 1, loading);
        }
        // add an item to the desired subgroup(s) of the given group
        void AddToSubgroups(object item, LiveShapingItem lsi, CollectionViewGroupInternal group, int level, bool loading)
        {
            object      name = GetGroupName(item, group.GroupBy, level);
            ICollection nameList;

            if (name == UseAsItemDirectly)
            {
                // the item belongs to the group itself (not to any subgroups)
                if (lsi != null)
                {
                    lsi.AddParentGroup(group);
                }

                if (loading)
                {
                    group.Add(item);
                }
                else
                {
                    int localIndex = group.Insert(item, item, ActiveComparer);
                    int index      = group.LeafIndexFromItem(item, localIndex);
                    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
                }
            }
            else if ((nameList = name as ICollection) == null)
            {
                // the item belongs to one subgroup
                AddToSubgroup(item, lsi, group, level, name, loading);
            }
            else
            {
                // the item belongs to multiple subgroups
                foreach (object o in nameList)
                {
                    AddToSubgroup(item, lsi, group, level, o, loading);
                }
            }
        }