//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/

using System;
using System.Threading;
using NLog;
using Raytheon.Common;
using Raytheon.Instruments.Dmm;
using Raytheon.Units;

namespace Raytheon.Instruments
{
   /// <summary>
   /// A simulation implementation of the DMM interface.
   /// This class basically just absorbs function calls and returns dummy data.
   /// </summary>
   public class DMMSim : IDmm
   {
      #region PrivateMemberVariables

      private string _name;
      private double _lastRange;
      private State _state;
      private SelfTestResult _selfTestResult;

      private readonly ILogger _logger;

      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      #endregion

      #region PrivateFunctions

      /// <summary>
      /// The Finalizer.
      /// </summary>
      ~DMMSim()
      {
         Dispose(false);
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="disposing"></param>
      protected virtual void Dispose(bool disposing)
      {
      }
      #endregion

      #region PublicFunctions

      /// <summary>
      /// DMMSim factory constructor
      /// </summary>
      /// <param name="deviceName"></param>
      /// <param name="configurationManager"></param>
      public DMMSim(string deviceName, IConfigurationManager configurationManager)
      {
         Name = deviceName;

         _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");

         _configurationManager = configurationManager;
         _configuration = _configurationManager.GetConfiguration(Name);

         _lastRange = 0;

         _state = State.Uninitialized;

         _selfTestResult = SelfTestResult.Unknown;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="deviceName"></param>
      public DMMSim(string deviceName)
      {
         _name = deviceName;

         _lastRange = 0;

         _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");

         _state = State.Uninitialized;

         _selfTestResult = SelfTestResult.Unknown;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool ClearErrors()
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="type"></param>
      /// <param name="range"></param>
      /// <param name="resolution"></param>
      public void ConfigureCurrentMeasurement(MeasurementFunction type, Current range, Current resolution)
      {
         _lastRange = range.Amps;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="type"></param>
      /// <param name="range"></param>
      /// <param name="resolution"></param>
      public void ConfigureFrequencyMeasurement(MeasurementFunction type, Frequency range, Frequency resolution)
      {
         _lastRange = range.Hertz;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="type"></param>
      /// <param name="range"></param>
      /// <param name="resolution"></param>
      public void ConfigureResistanceMeasurement(MeasurementFunction type, Resistance range, Resistance resolution)
      {
         _lastRange = range.Ohms;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="type"></param>
      /// <param name="range"></param>
      /// <param name="resolution"></param>
      public void ConfigureVoltageMeasurement(MeasurementFunction type, Voltage range, Voltage resolution)
      {
         _lastRange = range.Volts;
      }

      /// <summary>
      /// 
      /// </summary>
      public string DetailedStatus
      {
         get
         {
            return "This is a Sim Dmm";
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public bool DisplayEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Dispose()
      {
         Dispose(true);

         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// 
      /// </summary>
      public bool FrontPanelEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public InstrumentMetadata Info
      {
         get
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Initialize()
      {
         if (_state == State.Uninitialized)
         {
            _selfTestResult = PerformSelfTest();
            _state = State.Ready;
         }
         else
         {
            throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString());
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="timeout"></param>
      /// <returns></returns>
      public Current MeasureCurrent(int timeout)
      {
         double max = _lastRange;

         double min = 1.0;

         Random rnd = new Random();

         double seed = rnd.NextDouble();

         double dataToReturn = (seed * (max - min)) + min;

         Thread.Sleep(200);

         return Current.FromAmps(dataToReturn);
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="timeout"></param>
      /// <returns></returns>
      public Frequency MeasureFrequency(int timeout)
      {
         double max = _lastRange;

         double min = 1.0;

         Random rnd = new Random();

         double seed = rnd.NextDouble();

         double dataToReturn = (seed * (max - min)) + min;

         Thread.Sleep(200);

         return Frequency.FromHertz(dataToReturn);
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="timeout"></param>
      /// <returns></returns>
      public Resistance MeasureResistance(int timeout)
      {
         double max = _lastRange;

         double min = 1.0;

         Random rnd = new Random();

         double seed = rnd.NextDouble();

         double dataToReturn = (seed * (max - min)) + min;

         Thread.Sleep(200);

         return Resistance.FromOhms(dataToReturn);
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="timeout"></param>
      /// <returns></returns>
      public Voltage MeasureVoltage(int timeout)
      {
         double max = _lastRange;

         double min = 1.0;

         Random rnd = new Random();

         double seed = rnd.NextDouble();

         double dataToReturn = (seed * (max - min)) + min;

         Thread.Sleep(200);

         return Voltage.FromVolts(dataToReturn);
      }


      /// <summary>
      /// 
      /// </summary>
      public MeasurementFunction MeasurementType
      {
         get
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public string Name
      {
         get { return _name; }
         set { _name = value; }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public SelfTestResult PerformSelfTest()
      {
         _selfTestResult = SelfTestResult.Pass;
         return _selfTestResult;
      }

      /// <summary>
      /// 
      /// </summary>
      public void Reset()
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      public SelfTestResult SelfTestResult
      {
         get
         {
            return _selfTestResult;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public State Status
      {
         get
         {
            return _state;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Shutdown()
      {
         _state = State.Uninitialized;
      }

      #endregion
   }
}
