﻿namespace CodeCafeEngine.Internal
{
    public sealed class InheritedProperty<T> where T : IInheritedProperty
    {
        /// <summary>
        /// The name of the property.
        /// </summary>
        private readonly string _propertyName;
        /// <summary>
        /// The owner game object.
        /// </summary>
        private readonly IGameObject _gameObject;
        /// <summary>
        /// The actual property.
        /// </summary>
        private T _property;

        /// <summary>
        /// Create a new inherited property.
        /// </summary>
        /// <param name="gameObject">The owner of the property.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="property">The value of the property.</param>
        public InheritedProperty(IGameObject gameObject, string propertyName, T property)
        {
            _propertyName = propertyName;
            _gameObject = gameObject;
            _property = property;
        }

        /// <summary>
        /// Set the value of the inherited property.
        /// </summary>
        /// <param name="value"></param>
        public void SetValue(T value)
        {
            T previous = (T)_property.Clone();
            _property = value;
            _gameObject.InheritedPropertyChangedOnSelf(this, new InheritedPropertyChangedArgs(_property, previous, _propertyName));
        }

        /// <summary>
        /// Get the value of the inherited property.
        /// </summary>
        /// <returns></returns>
        public T GetValue()
        {
            return _property;
        }
    }
}
