#nullable enable
using System.Reflection;
namespace Terminal.Gui;
///
/// Defines a configuration settings scope. Classes that inherit from this abstract class can be used to define
/// scopes for configuration settings. Each scope is a JSON object that contains a set of configuration settings.
///
public class Scope : Dictionary
{ //, IScope> {
/// Crates a new instance.
public Scope () : base (StringComparer.InvariantCultureIgnoreCase)
{
foreach (KeyValuePair p in GetScopeProperties ())
{
Add (p.Key, new ConfigProperty { PropertyInfo = p.Value.PropertyInfo, PropertyValue = null });
}
}
/// Retrieves the values of the properties of this scope from their corresponding static properties.
public void RetrieveValues ()
{
foreach (KeyValuePair p in this.Where (cp => cp.Value.PropertyInfo is { }))
{
p.Value.RetrieveValue ();
}
}
/// Updates this instance from the specified source scope.
///
/// The updated scope (this).
public Scope? Update (Scope source)
{
foreach (KeyValuePair prop in source)
{
if (ContainsKey (prop.Key))
{
this [prop.Key].PropertyValue = this [prop.Key].UpdateValueFrom (prop.Value.PropertyValue!);
}
else
{
this [prop.Key].PropertyValue = prop.Value.PropertyValue;
}
}
return this;
}
/// Applies the values of the properties of this scope to their corresponding static properties.
///
internal virtual bool Apply ()
{
var set = false;
foreach (KeyValuePair p in this.Where (
t => t.Value != null
&& t.Value.PropertyValue != null
))
{
if (p.Value.Apply ())
{
set = true;
}
}
return set;
}
private IEnumerable> GetScopeProperties ()
{
return _allConfigProperties!.Where (
cp =>
(cp.Value.PropertyInfo?.GetCustomAttribute (
typeof (SerializableConfigurationProperty)
)
as SerializableConfigurationProperty)?.Scope
== GetType ()
);
}
}