// **********************************************************************************************************
//    EloadSystemScpiKeysightFactory.cs
//    2/20/2023
//    NGI - Next Generation Interceptor
//    
//    Contract No. HQ0856-21-C-0003/1022000209
//    
//    THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S. 
//    INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//    
//    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.
//    
//    UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//    
//    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.
//    
//    CONTROLLED BY: MISSILE DEFENSE AGENCY
//    CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
//    CUI CATEGORY: CTI 
//    DISTRIBUTION/DISSEMINATION CONTROL: F 
//    POC: Alex Kravchenko (1118268)
// **********************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using NLog;
using Raytheon.Common;
using Raytheon.Units;

namespace Raytheon.Instruments
{
   [ExportInstrumentFactory(ModelNumber = "EloadSystemScpiKeysightFactory")]
   public class EloadSystemScpiKeysightFactory : IInstrumentFactory
   {
      private readonly List<Type> _supportedInterfaces = new List<Type>();

      private readonly IConfigurationManager _configurationManager;
      private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
      private static string DefaultPath;

      public EloadSystemScpiKeysightFactory(string defaultConfigPath = DefaultConfigPath)
         : this(null, defaultConfigPath)
      {
      }

      /// <summary>
      /// EloadSystemScpiKeysightFactory injection constructor
      /// </summary>
      [ImportingConstructor]
      public EloadSystemScpiKeysightFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
                                   [Import(AllowDefault = true)] string defaultConfigPath = null)
      {
         DefaultPath = defaultConfigPath;

         if (LogManager.Configuration == null)
         {
            var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
         }

         _configurationManager = configManager ?? GetConfigurationManager();
         _supportedInterfaces.Add(typeof(IEload));
      }
      /// <summary>
      /// Gets the instrument
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public IInstrument GetInstrument(string name)
      {
         try
         {
            return new EloadSystemScpiKeysight(name, _configurationManager);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Gets the instrument
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public object GetInstrument(string name, bool simulateHw)
      {
         try
         {
            return new EloadSystemScpiKeysight(name, _configurationManager);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Gets supported interfaces
      /// </summary>
      /// <returns></returns>
      public ICollection<Type> GetSupportedInterfaces()
      {
         return _supportedInterfaces.ToArray();
      }

      /// <summary>
      /// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
      /// </summary>
      /// <returns></returns>
      private static IConfigurationManager GetConfigurationManager()
      {
         return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
      }


      #region PrivateFuctions
      /// <summary>
      /// Parses the power controller ini file. 
      /// This ini file defines the number of power systems/modules that this controller will manage.
      /// Upon parsing the file, this function populates this class members with the ini file information
      /// </summary>
      private static IELoadSystem CreateEloadSystemInstrument(string instrumentDefFile, string iniSectionName, bool isThereHardware)
      {
         const char COMMA_DELIM = ',';

         // interface types
         const string GPIB = "GPIB";
         const string RS232 = "232";

         // system
         const string BOARD_NUMBER_KEY = "BOARD_NUMBER";
         const string INTERFACE_KEY = "INTERFACE";
         const string ADDRESS_KEY = "ADDRESS";
         const string BAUD_RATE = "BAUD_RATE";
         const string PARITY = "PARITY";
         const string DATA_BITS = "DATA_BITS";
         const string STOP_BITS = "STOP_BITS";
         const string FLOW_CONTROL = "FLOW_CONTROL";
         const string MODULE_DEFINITION_KEY = "MODULE_DEFINITION";

         // modules
         const string MODE = "MODE";
         const string CHANNEL_NUM = "CHANNEL_NUM";
         const string SETPOINT_VALUE = "SETPOINT_VALUE";
         const string OCP = "OVER_CURRENT_PROTECTION";
         const string OVP = "OVER_VOLTAGE_PROTECTION";

         IniFile iniReader = new IniFile(instrumentDefFile);

         // pull out the address and the list of definitions
         string interfaceType = iniReader.ReadValue(iniSectionName, INTERFACE_KEY);
         string[] moduleDefinitionList = null;
         IELoadSystem loadSystem = null;
         if (interfaceType.ToUpper() == GPIB)
         {
            int gpibBoardNumber = Convert.ToInt32(iniReader.ReadValue(iniSectionName, BOARD_NUMBER_KEY));
            byte gpibAddress = Convert.ToByte(iniReader.ReadValue(iniSectionName, ADDRESS_KEY));
            string moduleDefinitionSections = iniReader.ReadValue(iniSectionName, MODULE_DEFINITION_KEY);
            moduleDefinitionList = moduleDefinitionSections.Split(COMMA_DELIM);

            loadSystem = new EloadSystemScpiKeysight(iniSectionName, gpibBoardNumber, gpibAddress, isThereHardware);

         }
         else if (interfaceType.ToUpper() == RS232)
         {
            string eloadSystemComAddress = iniReader.ReadValue(iniSectionName, ADDRESS_KEY);
            int eloadSystemComBaudRate = Convert.ToInt32(iniReader.ReadValue(iniSectionName, BAUD_RATE));
            Parity eloadSystemComParity = (Parity)Enum.Parse(typeof(Parity), iniReader.ReadValue(iniSectionName, PARITY));
            int eloadSystemComDataBits = Convert.ToInt32(iniReader.ReadValue(iniSectionName, DATA_BITS));
            Handshake flowControl = (Handshake)Enum.Parse(typeof(Handshake), iniReader.ReadValue(iniSectionName, FLOW_CONTROL));
            StopBits eloadSystemComStopBits = (StopBits)Enum.Parse(typeof(StopBits), iniReader.ReadValue(iniSectionName, STOP_BITS));

            string moduleDefinitionSections = iniReader.ReadValue(iniSectionName, MODULE_DEFINITION_KEY);
            moduleDefinitionList = moduleDefinitionSections.Split(COMMA_DELIM);

            loadSystem = new EloadSystemScpiKeysight(iniSectionName, eloadSystemComAddress, eloadSystemComBaudRate, eloadSystemComParity, eloadSystemComDataBits, eloadSystemComStopBits, flowControl, isThereHardware);
         }
         else
         {
            throw new Exception("EloadMeasurementInstruments::CreateEloadSystemInstrument() - Invalid Interface type in ini file. Ini file contained: " + interfaceType + ", supported types are: " + GPIB + "," + RS232);
         }

         foreach (string module in moduleDefinitionList)
         {
            string trimmedModuleName = module.TrimStart(' ');
            trimmedModuleName = trimmedModuleName.TrimEnd(' ');

            int modeTemp = Convert.ToInt32(iniReader.ReadValue(trimmedModuleName, MODE));
            int channelNumber = Convert.ToInt32(iniReader.ReadValue(trimmedModuleName, CHANNEL_NUM));
            double setpoint = Convert.ToDouble(iniReader.ReadValue(trimmedModuleName, SETPOINT_VALUE));
            double ocp = Convert.ToDouble(iniReader.ReadValue(trimmedModuleName, OCP));
            double ovp = Convert.ToDouble(iniReader.ReadValue(trimmedModuleName, OVP));

            EloadModuleMode mode;

            if (modeTemp == 0)
            {
               mode = EloadModuleMode.RESISTANCE;
            }
            else if (modeTemp == 1)
            {
               mode = EloadModuleMode.VOLTAGE;
            }
            else if (modeTemp == 2)
            {
               mode = EloadModuleMode.CURRENT;
            }
            else
            {
               throw new Exception("EloadMeasurementInstruments::CreateEloadSystemInstrument() - Invalid mode input: " + modeTemp.ToString());
            }

            loadSystem.AddEloadChannel(trimmedModuleName.ToUpper(), channelNumber, mode, setpoint, Current.FromAmps(ocp), Voltage.FromVolts(ovp));
         }

         return loadSystem;
      }
      #endregion

      #region PublicFuctions
      public static List<IELoadSystem> CreateEloadSystemInstrument(string instrumentDefFile, bool isThereHardware)
      {
         const string KEYSIGHT_ELOAD_SCPI = "KEYSIGHT_SCPI_ELOAD_SYSTEM";

         List<IELoadSystem> loadSystemsToReturn = new List<IELoadSystem>();

         IniFile iniReader = new IniFile(instrumentDefFile);

         List<string> loadSystemsToCreateKeys = iniReader.ReadAllKeys("ELOAD_SYSTEMS_TO_CREATE");

         foreach (string loadSystemName in loadSystemsToCreateKeys)
         {
            string loadSystemType = iniReader.ReadValue("ELOAD_SYSTEMS_TO_CREATE", loadSystemName);

            if (loadSystemType.ToUpper() == KEYSIGHT_ELOAD_SCPI.ToUpper())
            {
               IELoadSystem loadSystem = CreateEloadSystemInstrument(instrumentDefFile, loadSystemName, isThereHardware);

               loadSystemsToReturn.Add(loadSystem);
            }
            else
            {
               string errorMsg = "EloadMeasurementInstruments::CreateEloadSystemInstrument() - Unsupported ELoad instrument: " + loadSystemName + "\n";
               errorMsg += "Supported instrument are: " + KEYSIGHT_ELOAD_SCPI;
               throw new Exception(errorMsg);
            }
         }

         return loadSystemsToReturn;
      }
      #endregion

   }
}
