using System.Globalization;
using Aitex.Core.RT.DataCollection.HighPerformance;
using Aitex.Core.Util;
using MECF.Framework.Common.MECF.Framework.Common.Utilities;
using MECF.Framework.RT.Core.IoProviders;

namespace Aitex.Core.RT.IOCore
{
	public class AOAccessor : IOAccessor<float>
	{
		public AOAccessor(string name, int index, string address, bool isSimulator = false, bool disImmCache = false)
			: base(name, index, address, IOType.AO, isSimulator, disImmCache)
		{
		}

		/// <inheritdoc />
		protected override void SetValue(int offset, float value)
		{
#if EN_AO_IMM_CACHE
			var oldValue = Buffer[offset];
#endif
			Buffer[offset] = value;
			Singleton<IoProviderManager>.Instance.GetProvider(base.Provider)?.SetValue(this, value);
			
#if EN_AO_IMM_CACHE
			//! NOTE THIS MAY CAUSE OVERRUN-DATA-TRACE ISSUE
			// Check if immediate cache needed
			var needImmCache = DoubleUtil.NotEqual(oldValue, value) & !IsSimulator & !DisableImmediatelyCache;
			if (needImmCache)
				DataTraceManager.Instance.ImmediateCache(this, new CacheDiagnosisInfo("", Name, Type, offset,
					value.ToString(CultureInfo.CurrentCulture)));

#endif
            
		}
	}
}
