//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY
WARNING - THIS DOCUMENT CONTAINS TECHNICAL DATA WHOSE EXPORT OR DISCLOSURE
TO NON-U.S. PERSONS, WHEREVER LOCATED, IS RESTRICTED BY THE INTERNATIONAL
TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R. SECTIONS 120-130). VIOLATIONS
ARE SUBJECT TO SEVERE CRIMINAL PENALTIES.

DISTRIBUTION STATEMENT F: FURTHER DISSEMINATION ONLY AS DIRECTED BY MISSILE
DEFENSE AGENCY, MDA/GMY NEXT GENERATION INTERCEPTOR PROJECT OFFICE
(DATE OF DETERMINATION 14 JUNE 2021) OR HIGHER DOD AUTHORITY.
OTHER REQUESTS FOR THIS DOCUMENT SHALL BE REFERRED TO: MISSILE DEFENSE
AGENCY, CONTRACTS DIRECTORATE, ATTN: GMY-K, BLDG. 5222 MARTIN ROAD,
REDSTONE ARSENAL, AL 35898.

WARNING - THIS DOCUMENT CONTAINS TECHNICAL DATA WHOSE EXPORT IS RESTRICTED
BY THE ARMS EXPORT CONTROL ACT (TITLE 22, U.S.C., SEC 2751, ET SEQ.) OR 
THE EXPORT ADMINISTRATION ACT OF 1979 (TITLE 50, U.S.C., APP.2401 ET SEQ), 
AS AMENDED. VIOLATIONS OF THESE EXPORT LAWS ARE SUBJECT TO SEVERE CRIMINAL 
PENALTIES. DISSEMINATE IN ACCORDANCE WITH PROVISIONS OF 
DOD DIRECTIVE 5230.25

DESTRUCTION NOTICE - FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN 
DOD 5220.22-M,
NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006, 
INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR 
DODM 5200.01-VOLUME 3, DOD INFORMATION SECURITY PROGRAM: PROTECTION OF 
CLASSIFIED INFORMATION, ENCLOSURE 3, SECTION 17. FOR CONTROLLED 
UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4, 
INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION
-------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using Raytheon.Instruments;
using static MeasurementManagerLib.BitMeasurementManager;

namespace MeasurementManagerLib
{
   /// <summary>
   /// 
   /// </summary>
   public class MalMeasurementLibManager : IDisposable
   {
      private FpgaMeasurementManager _fpgaMeasurementManager;
      private PowerSupplyMeasurementManager _powerSupplyMeasurementManager;
      private SwitchMeasurementManager _switchMeasurementManager;
      private BitMeasurementManager _bitMeasurementManager;
      private CoeMeasurementManager _coeMeasurementManager;
      private ChillerCartMeasurementManager _chillerMeasurementManager;
      private DioMeasurementManager _dioMeasurementManager;
      private JtagMeasurementManager _jtagMeasurementManager;
      private VideoRecorderMeasurementManager _videoRecorderMeasurementManager;
      private RelayMeasurementManager _relayMeasurementManager;
      private RfMeasurementManager _rfMeasurementManager;
      private SpaceChamberLspsMeasurementManager _spaceChamberLspsMeasurementManager;
      private TelemetryMeasurementManager _telemetryMeasurementManager;
      private CryoMeasurementManager _cryoMeasurementManager;
      private OpticalBenchMeasurementManager _opticalBenchMeasurementManager;

      public FpgaMeasurementManager FpgaMeasurementManager
      {
         get
         {
            if (_fpgaMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(FpgaMeasurementManager));

            return _fpgaMeasurementManager;
         }

         private set
         {
            _fpgaMeasurementManager = value;
         }
      }
      public PowerSupplyMeasurementManager PowerSupplyMeasurementManager
      {
         get
         {
            if (_powerSupplyMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(PowerSupplyMeasurementManager));

            return _powerSupplyMeasurementManager;
         }

         private set
         {
            _powerSupplyMeasurementManager = value;
         }
      }
      public SwitchMeasurementManager SwitchMeasurementManager
      {
         get
         {
            if (_switchMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(SwitchMeasurementManager));

            return _switchMeasurementManager;
         }

         private set
         {
            _switchMeasurementManager = value;
         }
      }
      public BitMeasurementManager BitMeasurementManager
      {
         get
         {
            if (_bitMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(BitMeasurementManager));

            return _bitMeasurementManager;
         }

         private set
         {
            _bitMeasurementManager = value;
         }
      }
      public CoeMeasurementManager CoeMeasurementManager
      {
         get
         {
            if (_coeMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(CoeMeasurementManager));

            return _coeMeasurementManager;
         }

         private set
         {
            _coeMeasurementManager = value;
         }
      }
      public ChillerCartMeasurementManager ChillerMeasurementManager
      {
         get
         {
            if (_chillerMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(ChillerCartMeasurementManager));

            return _chillerMeasurementManager;
         }

         private set
         {
            _chillerMeasurementManager = value;
         }
      }
      public DioMeasurementManager DioMeasurementManager
      {
         get
         {
            if (_dioMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(DioMeasurementManager));

            return _dioMeasurementManager;
         }

         private set
         {
            _dioMeasurementManager = value;
         }
      }
      public JtagMeasurementManager JtagMeasurementManager
      {
         get
         {
            if (_jtagMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(JtagMeasurementManager));

            return _jtagMeasurementManager;
         }

         private set
         {
            _jtagMeasurementManager = value;
         }
      }
      public VideoRecorderMeasurementManager VideoRecorderMeasurementManager
      {
         get
         {
            if (_videoRecorderMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(VideoRecorderMeasurementManager));

            return _videoRecorderMeasurementManager;
         }

         private set
         {
            _videoRecorderMeasurementManager = value;
         }
      }
      public RelayMeasurementManager RelayMeasurementManager
      {
         get
         {
            if (_relayMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(RelayMeasurementManager));

            return _relayMeasurementManager;
         }

         private set
         {
            _relayMeasurementManager = value;
         }
      }
      public RfMeasurementManager RfMeasurementManager
      {
         get
         {
            if (_rfMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(RfMeasurementManager));

            return _rfMeasurementManager;
         }

         private set
         {
            _rfMeasurementManager = value;
         }
      }
      public SpaceChamberLspsMeasurementManager SpaceChamberLspsMeasurementManager
      {
         get
         {
            if (_spaceChamberLspsMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(SpaceChamberLspsMeasurementManager));

            return _spaceChamberLspsMeasurementManager;
         }

         private set
         {
            _spaceChamberLspsMeasurementManager = value;
         }
      }
      public TelemetryMeasurementManager TelemetryMeasurementManager
      {
         get
         {
            if (_telemetryMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(TelemetryMeasurementManager));

            return _telemetryMeasurementManager;
         }

         private set
         {
            _telemetryMeasurementManager = value;
         }
      }
      public CryoMeasurementManager CryoMeasurementManager
      {
         get
         {
            if (_cryoMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(CryoMeasurementManager));

            return _cryoMeasurementManager;
         }

         private set
         {
            _cryoMeasurementManager = value;
         }
      }
      public OpticalBenchMeasurementManager OpticalBenchMeasurementManager
      {
         get
         {
            if (_opticalBenchMeasurementManager == null)
               throw new MalMeasurementManagerNullReferenceException(typeof(OpticalBenchMeasurementManager));

            return _opticalBenchMeasurementManager;
         }

         private set
         {
            _opticalBenchMeasurementManager = value;
         }
      }

      public IInstrumentManager InstrumentManager { get; private set; }

      ~MalMeasurementLibManager()
      {
         Dispose(false);
      }

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

      #region PublicFunctions
      /// <summary>
      /// 
      /// </summary>
      /// <param name=""></param>
      public MalMeasurementLibManager(IInstrumentManager instrumentManager)
      {
         InstrumentManager = instrumentManager;
      }

      /// <summary>
      /// Dispose of all resources
      /// </summary>
      public void Dispose()
      {
         Dispose(true);
      }

      /// <summary>
      /// Initialize the BIT interface manager
      /// </summary>
      /// <param name="measurementDefFile"></param>
      /// <param name="comDeviceName"></param>
      /// <param name="comNodeName"></param>
      /// <param name="binDataLogFilename"></param>
      /// <param name="asciiDataLogFileName"></param>
      /// <param name="callback"></param>
      public void InitializeBitMeasurementManager(string measurementDefFile, string comDeviceName, string comNodeName, string binDataLogFilename, string asciiDataLogFileName, MessageReceivedDelegate callback)
      {
         // create the object once no matter how many times the host calls this
         if (_bitMeasurementManager == null)
         {
            _bitMeasurementManager = new BitMeasurementManager(InstrumentManager, measurementDefFile, comDeviceName, comNodeName, binDataLogFilename, asciiDataLogFileName, callback);
         }
      }

      /// <summary>
      /// Initialize the BIT interface manager
      /// </summary>
      /// <param name="instrumentNames"></param>
      public void InitializeCoeMeasurementManager(string coeMeasurementManagerConfigFullPath)
      {
         if (_coeMeasurementManager == null)
         {
            _coeMeasurementManager = new CoeMeasurementManager(InstrumentManager, coeMeasurementManagerConfigFullPath);
            _coeMeasurementManager.InitNodes();
         }
      }

      /// <summary>
      /// Initialize the Chiller interface manager
      /// </summary>
      /// <param name="chillerName"></param>
      /// <param name="flowMeeterName"></param>
      /// <param name="tempSensorName"></param>
      /// <param name="errorLog"></param>
      public void InitializeChillerCartMeasurementManager(string chillerName, string flowMeeterName, string tempSensorName, string errorLog)
      {
         // create the object once no matter how many times the host calls this
         if (_chillerMeasurementManager == null)
         {
            _chillerMeasurementManager = new ChillerCartMeasurementManager(InstrumentManager, chillerName, flowMeeterName, tempSensorName, errorLog);
         }
      }

      /// <summary>
      /// Initialize the Dio interface manager
      /// </summary>
      public void InitializeDioMeasurementManager()
      {
         // create the object once no matter how many times the host calls this
         if (_dioMeasurementManager == null)
         {
            _dioMeasurementManager = new DioMeasurementManager(InstrumentManager);
         }
      }

      /// <summary>
      /// Initialize the Jtag interface manager
      /// </summary>
      /// <param name="instrumentName"></param>
      /// <param name="measurementDefFile"></param>
      public void InitializeJtagMeasurementManager(string instrumentName, string measurementDefFile)
      {
         // create the object once no matter how many times the host calls this
         if (_jtagMeasurementManager == null)
         {
            _jtagMeasurementManager = new JtagMeasurementManager(InstrumentManager, instrumentName, measurementDefFile);
         }
      }

      /// <summary>
      /// Initialize the fpga communication manager
      /// </summary>
      /// <param name="instrumentNames"></param>
      /// <param name="instrumentDefFile"></param>
      public void InitializeFpgaMeasurementManager(List<string> instrumentNames, string instrumentDefFile)
      {
         // create the object once no matter how many times the host calls this
         if (_fpgaMeasurementManager == null)
         {
            _fpgaMeasurementManager = new FpgaMeasurementManager(InstrumentManager, instrumentNames, instrumentDefFile);
         }
      }

      /// <summary>
      /// Initialize the power system and connect to the power supplies
      /// </summary>
      public void InitializePowerSupplyMeasurementManager()
      {
         // create the object once no matter how many times the host calls this
         if (_powerSupplyMeasurementManager == null)
         {
            _powerSupplyMeasurementManager = new PowerSupplyMeasurementManager(InstrumentManager);
         }
      }

      /// <summary>
      /// Initialize the relay manager
      /// </summary>
      /// <param name="instrumentNames"></param>
      /// <param name="configFileName"></param>
      public void InitializeRelayMeasurementManager(List<string> instrumentNames, string configFileName)
      {
         // create the object once no matter how many times the host calls this
         if (_relayMeasurementManager == null)
         {
            _relayMeasurementManager = new RelayMeasurementManager(InstrumentManager, instrumentNames, configFileName);
         }
      }

      /// <summary>
      /// Initializes Rf Measurement Manager
      /// </summary>
      /// <param name="switchInstrumentNames"></param>
      /// <param name="powerMeeterName"></param>
      /// <param name="signalGeneratorName"></param>
      /// <param name="specAnalyzerName"></param>
      /// <param name="configFileName"></param>
      public void InitializeRfMeasurementManager(List<string> switchInstrumentNames, string powerMeeterName, string signalGeneratorName, string specAnalyzerName, string configFileName)
      {
         // create the object once no matter how many times the host calls this
         if (_rfMeasurementManager == null)
         {
            _rfMeasurementManager = new RfMeasurementManager(InstrumentManager, switchInstrumentNames, powerMeeterName, signalGeneratorName, specAnalyzerName, configFileName);
         }
      }

      /// <summary>
      /// Initializes Space Chamber LSPS Measurement Manager
      /// </summary>
      /// <param name="lspsChamber"></param>
      /// <param name="netCdfData"></param>
      public void InitializeSpaceChamberLSPSMeasurementManager(string lspsChamber, string netCdfData)
      {
         // create the object once no matter how many times the host calls this
         if (_spaceChamberLspsMeasurementManager == null)
         {
            _spaceChamberLspsMeasurementManager = new SpaceChamberLspsMeasurementManager(InstrumentManager, lspsChamber, netCdfData);
         }
      }

      /// <summary>
      /// Initializes Switch Measurement Manager
      /// </summary>
      /// <param name="switchMeasurementManagerConfigFullPath"></param>
      public void InitializeSwitchMeasurementManager(string switchMeasurementManagerConfigFullPath, bool skipDmmInitialization = false)
      {
         // create the object once no matter how many times the host calls this
         if (_switchMeasurementManager == null)
         {
            _switchMeasurementManager = new SwitchMeasurementManager(InstrumentManager, switchMeasurementManagerConfigFullPath, skipDmmInitialization);
         }
      }

      /// <summary>
      /// Initialize the video recorder manager
      /// </summary>
      /// <param name="deviceName"></param>
      public void InitializeVideoRecorderMeasurementManager(string deviceName)
      {
         // create the object once no matter how many times the host calls this
         if (_videoRecorderMeasurementManager == null)
         {
            _videoRecorderMeasurementManager = new VideoRecorderMeasurementManager(InstrumentManager, deviceName);
         }
      }

      /// <summary>
      /// Initializes Cryo Measurement Manager
      /// </summary>
      /// <param name="commDeviceName"></param>
      /// <param name="serialDeviceName"></param>
      /// <param name="isThereHardware"></param>
      /// <param name="errorLogFileName"></param>
      public void InitializeCryoMeasurementManager(string commDeviceName, string serialDeviceName, bool isThereHardware, string errorLogFileName)
      {
         // create the object once no matter how many times the host calls this
         if (_cryoMeasurementManager == null)
         {
            _cryoMeasurementManager = new CryoMeasurementManager(InstrumentManager, commDeviceName, serialDeviceName, isThereHardware, errorLogFileName);
         }
      }

      /// <summary>
      /// Initializes Optical Bench Measurement Manager
      /// </summary>
      /// <param name="instrumentName"></param>
      /// <param name="measurementDefFile"></param>
      /// <param name="instrumentDefFile"></param>
      public void InitializeOpticalBenchMeasurementManager(string instrumentName, string measurementDefFile, string instrumentDefFile)
      {
         // create the object once no matter how many times the host calls this
         if (_opticalBenchMeasurementManager == null)
         {
            _opticalBenchMeasurementManager = new OpticalBenchMeasurementManager(InstrumentManager, instrumentName, measurementDefFile, instrumentDefFile);
         }
      }

      /// <summary>
      /// initializes Telemetry Measurement Manager for multiple instruments
      /// </summary>
      /// <param name="instrumentNames"></param>
      /// <param name="telemetryMalFile"></param>
      public void InitializeTelemetryMeasurementManager(List<string> instrumentNames, string telemetryMalFile)
      {

         // create the object once no matter how many times the host calls this
         if (_telemetryMeasurementManager == null)
         {
            _telemetryMeasurementManager = new TelemetryMeasurementManager(InstrumentManager, instrumentNames, telemetryMalFile);
         }
      }

      #endregion
   }
}
