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 managed by a plugin and is therefore locked.
///
public bool IsLocked { get; private set; }
///
/// The ID of the plugin that manages this configuration. This is set when the configuration is locked.
///
public Guid MangedByConfigPluginId { 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; }
///
/// Locks the configuration state, indicating that it is managed by a specific plugin.
///
/// The ID of the plugin that is managing this configuration.
public void LockManagedState(Guid pluginId)
{
this.IsLocked = true;
this.MangedByConfigPluginId = pluginId;
}
///
/// Resets the managed state of the configuration, allowing it to be modified again.
/// This will also reset the property to its default value.
///
public void ResetManagedState()
{
this.IsLocked = false;
this.MangedByConfigPluginId = Guid.Empty;
this.Reset();
}
///
/// Resets the configuration property to its default value.
///
public 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);
}
}