/*-------------------------------------------------------------------------
//                           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.ComponentModel.Composition;
using System.IO;
using System.Reflection;

namespace Raytheon.Instruments.PowerSupplies
{
	[ExportInstrumentFactory(ModelNumber = "PowerSupplyKeysightN67xxFactory")]
	public class KeysightN67xxPowerFactory : IInstrumentFactory
	{
		/// <summary>
		/// The supported interfaces
		/// </summary>
		private readonly List<Type> _supportedInterfaces = new List<Type>();
		private static ILogger _logger;
		private readonly IConfigurationManager _configurationManager;
		private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
		private static string DefaultPath;

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

		/// <summary>
		/// COECommDeviceInstrumentFactory injection constructor
		/// </summary>
		/// <param name="configManager"></param>
		/// <param name="simEngine"></param>
		/// <param name="logger"></param>
		[ImportingConstructor]
		public KeysightN67xxPowerFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
									[Import(AllowDefault = true)] string defaultConfigPath = null)
		{
			DefaultPath = defaultConfigPath;
			_logger = LogManager.GetCurrentClassLogger();

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

			_configurationManager = configManager ?? GetConfigurationManager();
			_supportedInterfaces.Add(typeof(PowerSupply));
      }

      /// <summary>
      /// Gets the instrument
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public IInstrument GetInstrument(string name)
		{
			return null;
		}

      /// <summary>
      /// Gets the instrument
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public object GetInstrument(string name, bool simulateHw)
      {
         try
         {
				if (simulateHw)
               return new PowerSupplySim(name, _configurationManager, _logger);
				else
					return new KeysightN67xxPowerSupply(name, _configurationManager, _logger);
         }
         catch (Exception ex)
         {
            _logger.Error(ex, $"Unable to construct {name} instrument instance");
            return null;
         }
      }

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

		/// <summary>
		/// returns confiuration 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);
		}
	}
}