using System.Linq.Expressions;
using AIStudio.Settings.DataModel;
namespace AIStudio.Settings;
///
/// Represents configuration metadata for a specific class and property.
///
/// The class type that contains the configuration property.
/// The type of the configuration property value.
public record ConfigMeta : ConfigMetaBase
{
public ConfigMeta(Expression> configSelection, Expression> propertyExpression)
{
this.ConfigSelection = configSelection;
this.PropertyExpression = propertyExpression;
}
///
/// The expression to select the configuration class from the settings data.
///
private Expression> ConfigSelection { get; }
///
/// The expression to select the property within the configuration class.
///
private Expression> PropertyExpression { get; }
///
/// Indicates whether the configuration is locked by a configuration plugin.
///
public bool IsLocked { get; private set; }
///
/// The ID of the plugin that locked this configuration.
///
public Guid LockedByConfigPluginId { get; private set; }
///
/// How this setting is managed by a configuration plugin, if at all.
///
public ManagedConfigurationMode? ManagedMode { get; private set; }
///
/// The ID of the plugin that currently provides an editable default value.
///
public Guid EditableDefaultByConfigPluginId { get; private set; }
///
/// The default value for the configuration property. This is used when resetting the property to its default state.
///
public required TValue Default { get; init; }
///
/// Indicates whether a plugin contribution is available.
///
public bool HasPluginContribution { get; private set; }
///
/// The additive value contribution provided by a configuration plugin.
///
public TValue PluginContribution { get; private set; } = default!;
///
/// The ID of the plugin that provided the additive value contribution.
///
public Guid PluginContributionByConfigPluginId { get; private set; }
///
/// Locks the configuration state, indicating that it is controlled by a specific plugin.
///
/// The ID of the plugin that is locking this configuration.
public void LockConfiguration(Guid pluginId)
{
this.IsLocked = true;
this.LockedByConfigPluginId = pluginId;
this.ManagedMode = ManagedConfigurationMode.LOCKED;
this.EditableDefaultByConfigPluginId = Guid.Empty;
}
///
/// Resets the locked state of the configuration, allowing it to be modified again.
/// This will also reset the property to its default value.
///
public void ResetLockedConfiguration()
{
this.IsLocked = false;
this.LockedByConfigPluginId = Guid.Empty;
if (this.ManagedMode is ManagedConfigurationMode.LOCKED)
this.ManagedMode = null;
this.Reset();
}
///
/// Unlocks the configuration state without changing the current value.
///
public void UnlockConfiguration()
{
this.IsLocked = false;
this.LockedByConfigPluginId = Guid.Empty;
if (this.ManagedMode is ManagedConfigurationMode.LOCKED)
this.ManagedMode = null;
}
///
/// Marks the setting as having an editable default provided by a configuration plugin.
///
public void SetEditableDefaultConfiguration(Guid pluginId)
{
this.IsLocked = false;
this.LockedByConfigPluginId = Guid.Empty;
this.ManagedMode = ManagedConfigurationMode.EDITABLE_DEFAULT;
this.EditableDefaultByConfigPluginId = pluginId;
}
///
/// Clears the editable-default state without changing the current value.
///
public void ClearEditableDefaultConfiguration()
{
if (this.ManagedMode is ManagedConfigurationMode.EDITABLE_DEFAULT)
this.ManagedMode = null;
this.EditableDefaultByConfigPluginId = Guid.Empty;
}
///
/// Stores an additive plugin contribution.
///
public void SetPluginContribution(TValue value, Guid pluginId)
{
this.PluginContribution = value;
this.PluginContributionByConfigPluginId = pluginId;
this.HasPluginContribution = true;
}
///
/// Clears the additive plugin contribution without changing the current value.
///
public void ClearPluginContribution()
{
this.PluginContribution = default!;
this.PluginContributionByConfigPluginId = Guid.Empty;
this.HasPluginContribution = false;
}
///
/// Resets the configuration property to its default value.
///
private void Reset()
{
var configInstance = this.ConfigSelection.Compile().Invoke(SETTINGS_MANAGER.ConfigurationData);
var memberExpression = this.PropertyExpression.GetMemberExpression();
if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo)
propertyInfo.SetValue(configInstance, this.Default);
}
///
/// Sets the value of the configuration property to the specified value.
///
/// The value to set for the configuration property.
public void SetValue(TValue value)
{
var configInstance = this.ConfigSelection.Compile().Invoke(SETTINGS_MANAGER.ConfigurationData);
var memberExpression = this.PropertyExpression.GetMemberExpression();
if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo)
propertyInfo.SetValue(configInstance, value);
}
///
/// Gets the current value of the configuration property.
///
public TValue GetValue()
{
var configInstance = this.ConfigSelection.Compile().Invoke(SETTINGS_MANAGER.ConfigurationData);
var memberExpression = this.PropertyExpression.GetMemberExpression();
if (memberExpression.Member is System.Reflection.PropertyInfo propertyInfo && propertyInfo.GetValue(configInstance) is TValue value)
return value;
return default!;
}
}