using System;
using System.Linq;
using Artemis.Storage.Entities.Profile.DataBindings;
namespace Artemis.Core
{
///
public class DataBindingRegistration : IDataBindingRegistration
{
internal DataBindingRegistration(LayerProperty layerProperty, DataBindingConverter converter,
Func getter, Action setter, string displayName)
{
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
Converter = converter ?? throw new ArgumentNullException(nameof(converter));
Getter = getter ?? throw new ArgumentNullException(nameof(getter));
Setter = setter ?? throw new ArgumentNullException(nameof(setter));
DisplayName = displayName ?? throw new ArgumentNullException(nameof(displayName));
}
///
/// Gets the layer property this registration was made on
///
public LayerProperty LayerProperty { get; }
///
/// Gets the converter that's used by the data binding
///
public DataBindingConverter Converter { get; }
///
/// Gets the function to call to get the value of the property
///
public Func Getter { get; }
///
/// Gets the action to call to set the value of the property
///
public Action Setter { get; }
///
public string DisplayName { get; }
///
public Type ValueType => typeof(TProperty);
///
/// Gets the data binding created using this registration
///
public DataBinding? DataBinding { get; internal set; }
///
public IDataBinding? GetDataBinding()
{
return DataBinding;
}
///
public IDataBinding? CreateDataBinding()
{
if (DataBinding != null)
return DataBinding;
DataBindingEntity? dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.Identifier == DisplayName);
if (dataBinding == null)
return null;
DataBinding = new DataBinding(LayerProperty, dataBinding);
return DataBinding;
}
///
public void ClearDataBinding()
{
if (DataBinding == null)
return;
// The related entity is left behind, just in case the data binding is added back later
LayerProperty.DisableDataBinding(DataBinding);
}
///
public object? GetValue()
{
return Getter();
}
}
}