using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.DataModelExpansions
{
///
/// Allows you to expand the application-wide datamodel
///
public abstract class DataModelExpansion : BaseDataModelExpansion where T : DataModel
{
///
/// The main data model of this data model expansion
/// Note: This default data model is automatically registered upon plugin enable
///
public T DataModel
{
get => (T) InternalDataModel;
internal set => InternalDataModel = value;
}
///
/// Hide the provided property using a lambda expression, e.g. HideProperty(dm => dm.TimeDataModel.CurrentTimeUTC)
///
/// A lambda expression pointing to the property to ignore
public void HideProperty(Expression> propertyLambda)
{
PropertyInfo propertyInfo = ReflectionUtilities.GetPropertyInfo(DataModel, propertyLambda);
if (!HiddenPropertiesList.Any(p => p.Equals(propertyInfo)))
HiddenPropertiesList.Add(propertyInfo);
}
///
/// Stop hiding the provided property using a lambda expression, e.g. ShowProperty(dm =>
/// dm.TimeDataModel.CurrentTimeUTC)
///
/// A lambda expression pointing to the property to stop ignoring
public void ShowProperty(Expression> propertyLambda)
{
PropertyInfo propertyInfo = ReflectionUtilities.GetPropertyInfo(DataModel, propertyLambda);
HiddenPropertiesList.RemoveAll(p => p.Equals(propertyInfo));
}
internal override void InternalEnablePlugin()
{
DataModel = Activator.CreateInstance();
DataModel.PluginInfo = PluginInfo;
DataModel.DataModelDescription = GetDataModelDescription();
base.InternalEnablePlugin();
}
internal override void InternalDisablePlugin()
{
DataModel = null;
base.InternalDisablePlugin();
}
}
}