void SaveMacroAssociationSettings(ComponentGroup componentGroup, string componentUid, ComponentGroupAssociation association)
 {
     _storageService.WriteSerializedValue(association.Settings, ComponentGroupsDirectory, componentGroup.Uid, "Macros", componentUid, DefaultFileNames.Settings);
 }
        public void InitializeComponentGroup(string uid)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }

            if (!_storageService.TryReadSerializedValue(out ComponentGroupConfiguration configuration, ComponentGroupsDirectory, uid, DefaultFileNames.Configuration))
            {
                throw new ComponentGroupNotFoundException(uid);
            }

            if (!_storageService.TryReadSerializedValue(out IDictionary <string, object> settings, ComponentGroupsDirectory, uid, DefaultFileNames.Settings))
            {
                settings = new Dictionary <string, object>();
            }

            var componentGroup = new ComponentGroup(uid);

            foreach (var setting in settings)
            {
                componentGroup.SetSetting(setting.Key, setting.Value);
            }

            var associationUids = _storageService.EnumerateDirectories("*", ComponentGroupsDirectory, uid, "Components");

            foreach (var associationUid in associationUids)
            {
                if (!_storageService.TryReadSerializedValue(out IDictionary <string, object> associationSettings, ComponentGroupsDirectory, uid, "Components", associationUid, DefaultFileNames.Settings))
                {
                    associationSettings = new Dictionary <string, object>();
                }

                var componentAssociation = new ComponentGroupAssociation();
                foreach (var associationSetting in associationSettings)
                {
                    componentAssociation.Settings[associationSetting.Key] = associationSetting.Value;
                }

                componentGroup.Components.TryAdd(associationUid, componentAssociation);
            }

            associationUids = _storageService.EnumerateDirectories("*", ComponentGroupsDirectory, uid, "Macros");
            foreach (var associationUid in associationUids)
            {
                if (!_storageService.TryReadSerializedValue(out IDictionary <string, object> associationSettings, ComponentGroupsDirectory, uid, "Macros", associationUid, DefaultFileNames.Settings))
                {
                    associationSettings = new Dictionary <string, object>();
                }

                var componentAssociation = new ComponentGroupAssociation();
                foreach (var associationSetting in associationSettings)
                {
                    componentAssociation.Settings[associationSetting.Key] = associationSetting.Value;
                }

                componentGroup.Macros.TryAdd(associationUid, componentAssociation);
            }

            lock (_componentGroups)
            {
                _componentGroups[uid] = componentGroup;
            }
        }