/*-------------------------------------------------------------------------
//                           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 Raytheon.Common;
using Raytheon.Instruments;
using System;
using System.Collections.Generic;
using NLog;

namespace Raytheon
{
   /// <summary>
   /// Class for controlling all power supplies
   /// </summary>
   public class PowerModuleMeasurementManager
   {
      #region PrivateClassMembers
      /// <summary>
      /// NLog logger
      /// </summary>
      private static NLog.ILogger _logger;

      private PowerSupply _powerSupply;

      private IConfigurationFile _powerOffAndSelfTestConfig;

      #endregion

      public bool FrontPanelEnabled
      {
         private get { return false; }
         set
         {
            if (_powerSupply != null)
            {
               _powerSupply[null].FrontPanelEnabled = value;
            }
         }
      }

      /// <summary>
      /// constructor
      /// </summary>
      /// <param></param>
      public PowerModuleMeasurementManager(PowerSupply powerSupply, IConfigurationFile powerOffAndSelfTestConfig)
      {
         _logger = LogManager.GetCurrentClassLogger();

         _powerSupply = powerSupply;
         _powerOffAndSelfTestConfig = powerOffAndSelfTestConfig;
      }

      /// <summary>
		/// The Finalizer
		/// </summary>
		~PowerModuleMeasurementManager()
      {
         _logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() ...");
      }

      /// <summary>
      /// Initialize the instrument(s)
      /// </summary>
      public void Initialize()
      {
         _logger.Trace($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() method...");
      }

      /// <summary>
      /// Shuts down manager, clears resources
      /// </summary>
      public void Shutdown()
      {
         _logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() method...");
         _powerSupply.Shutdown();
      }

      /// <summary>
      /// Read various power supply data
      /// </summary>
      public void ReadPowerSupplyData(object module, out double voltage, out double current, out double voltageSetPoint, out bool isOn, out int faultStatus)
      {
         _powerSupply[module].ReadPowerSupplyData(out voltage, out current, out voltageSetPoint, out isOn, out faultStatus);
      }

      /// <summary>
      /// Get all the names of modules in the power system
      /// </summary>
      public List<string> GetModuleNames()
      {
         return _powerSupply.GetPowerSupplyModuleNames();
      }

      /// <summary>
      /// Get all the configuration information for each module
      /// </summary>
      public Dictionary<string, Instruments.PowerSupplies.PowerSupplyModuleInfo> GetPowerSupplyModuleInfoDict()
      {
         return _powerSupply.GetPowerSupplyModuleInfoDict();
      }

      /// <summary>
      /// Enable the output of the power supply.
      /// </summary>
      /// <param name="moduleName">The name of the module to enable.</param>
      public void OutputEnable(object module)
      {
         _powerSupply[module].On();
      }

      /// <summary>
      /// Disable the output of the power supply.
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public void OutputDisable(object module)
      {
         _powerSupply[module].ReadPowerSupplyData(out double voltage, out double current, out double voltageSetPoint, out bool isOn, out int faultStatus);

         if (isOn)
         {
            _powerSupply[module].Off();

            // save the time of this power off to file
            MeasurementManager.PowerSupply.Util.SaveTime(_powerSupply.Name, DateTime.Now.ToString(), null, _powerOffAndSelfTestConfig);
         }
      }
   }
}
