/*-------------------------------------------------------------------------
//                           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 NLog;
using Raytheon.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Raytheon.Instruments.PowerSupplies
{
   /// <summary>
   /// Class to simulate any power supply system
   /// </summary>
   public class PowerSupplySim : PowerSupply
   {
      private string _iniFilePath;

      private static ILogger _logger;
      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      /// <summary>
      /// Constructor
      /// </summary>
      public PowerSupplySim(string deviceInstanceName, IConfigurationManager configurationManager, ILogger logger)
      {
         Name = deviceInstanceName;
         _logger = logger;
         _configurationManager = configurationManager;
         _configuration = _configurationManager.GetConfiguration(Name);

         string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
         _iniFilePath = Path.Combine(assemblyFolder,_configuration.GetConfigurationValue(deviceInstanceName, "IniFilePath", $".\\{Raytheon.Common.GeneralConstants.InstrumentConfigFolder}\\{deviceInstanceName}.ini"));
      }

      /// <summary>
      /// Perform shutdown
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public override void Shutdown()
      {
      }

      /// <summary>
      /// Perform reset
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public override void Reset()
      {
      }

      /// <summary>
      /// Clear errors
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public override bool ClearErrors()
      {
         return true;
      }

      /// <summary>
      /// Group or couple modules as specified in config file
      /// Gather information for each power module from config file
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public override void Initialize()
      {
         _powerSupplyModule = new PowerSupplyModuleSim(_iniFilePath, Name);
      }

      /// <summary>
      /// Implement Indexer to obtain a power module
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public override PowerSupplyModule this[object powerDeviceId]
      {
         get
         {
            string powerDeviceName = String.Empty;

            if (powerDeviceId != null && (powerDeviceId.GetType().IsEnum || powerDeviceId is string))
            {
               powerDeviceName = powerDeviceId.ToString();
            }
            else if (powerDeviceId != null)
            {
               throw new ArgumentException($"{nameof(powerDeviceId)} must be null or enumerated or string type");
            }

            _powerSupplyModule.GetSemphamore().WaitOne();

            if (powerDeviceId != null)
               _powerSupplyModule.SetActivePowerModule(powerDeviceName);

            return _powerSupplyModule;
         }
      }
   }
}
