using System;

namespace CareFusion.Lib.StorageSystem.Stock
{

    /// <summary>
    /// Allows to define how to handle a field update
    /// </summary>
    /// <typeparam name="T">The type of the field</typeparam>
    public readonly struct FieldUpdate<T>
    {
        private readonly T _value;
        private readonly bool _clearField;

        private FieldUpdate(T value, bool clearField)
        {
            _value = value;
            _clearField = clearField;
        }

        /// <summary>
        /// Create a field update that sets the field to the specific value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static FieldUpdate<T> Set(T value)
        {
            return new FieldUpdate<T>(value, false);
        }

        /// <summary>
        /// Create a field update that clears the field 
        /// </summary>
        /// <returns></returns>
        public static FieldUpdate<T> Clear()
        {
            return new FieldUpdate<T>(default, true);

        }


        public T Get()
        {
            if (!_clearField)
            {
                return _value;
            }

            throw new InvalidOperationException("value not set");
        }

        public bool IsSet() { return !_clearField; }
    }
}