using System;
using Artemis.Storage.Entities.General;
namespace Artemis.Core.ScriptingProviders
{
///
/// Represents the configuration of a script
///
public class ScriptConfiguration : CorePropertyChanged, IStorageModel
{
private bool _hasChanges;
private bool _isSuspended;
private string _name;
private string? _pendingScriptContent;
private string? _scriptContent;
private string _scriptingProviderId;
///
/// Creates a new instance of the class
///
public ScriptConfiguration(ScriptingProvider provider, string name)
{
_scriptingProviderId = provider.Id;
_name = name;
Entity = new ScriptConfigurationEntity();
}
internal ScriptConfiguration(ScriptConfigurationEntity entity)
{
_scriptingProviderId = null!;
_name = null!;
Entity = entity;
Load();
}
///
/// Gets or sets the ID of the scripting provider
///
public string ScriptingProviderId
{
get => _scriptingProviderId;
set => SetAndNotify(ref _scriptingProviderId, value);
}
///
/// Gets or sets the name of the script
///
public string Name
{
get => _name;
set => SetAndNotify(ref _name, value);
}
///
/// Gets or sets the script's content
///
public string? ScriptContent
{
get => _scriptContent;
private set
{
if (!SetAndNotify(ref _scriptContent, value)) return;
OnScriptContentChanged();
}
}
///
/// Gets or sets the pending changes to the script's content
///
public string? PendingScriptContent
{
get => _pendingScriptContent;
set
{
if (string.IsNullOrWhiteSpace(value))
value = null;
if (!SetAndNotify(ref _pendingScriptContent, value)) return;
HasChanges = ScriptContent != PendingScriptContent;
}
}
// TODO: Implement suspension
///
/// [NYI] Gets or sets a boolean indicating whether this configuration is suspended
///
public bool IsSuspended
{
get => _isSuspended;
set => SetAndNotify(ref _isSuspended, value);
}
///
/// Gets or sets a boolean indicating whether this configuration has pending changes to it's
///
///
public bool HasChanges
{
get => _hasChanges;
set => SetAndNotify(ref _hasChanges, value);
}
///
/// If active, gets the script
///
public Script? Script { get; internal set; }
internal ScriptConfigurationEntity Entity { get; }
///
/// Applies the to the
///
public void ApplyPendingChanges()
{
ScriptContent = PendingScriptContent;
HasChanges = false;
}
///
/// Discards the
///
public void DiscardPendingChanges()
{
PendingScriptContent = ScriptContent;
HasChanges = false;
}
#region Implementation of IStorageModel
///
public void Load()
{
ScriptingProviderId = Entity.ScriptingProviderId;
ScriptContent = Entity.ScriptContent;
PendingScriptContent = Entity.ScriptContent;
Name = Entity.Name;
}
///
public void Save()
{
Entity.ScriptingProviderId = ScriptingProviderId;
Entity.ScriptContent = ScriptContent;
Entity.Name = Name;
}
#endregion
#region Events
///
/// Occurs whenever the contents of the script have changed
///
public event EventHandler? ScriptContentChanged;
///
/// Invokes the event
///
protected virtual void OnScriptContentChanged()
{
ScriptContentChanged?.Invoke(this, EventArgs.Empty);
}
#endregion
}
}