/*-------------------------------------------------------------------------
//                           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;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Raytheon.Instruments;
using Raytheon.Common;
using NLog;
using System.Windows.Interop;
using ProgramGui;
using NationalInstruments.TestStand.Interop.API;
using System.Runtime.ExceptionServices;

namespace ProgramLib
{
   /// <summary>
	/// Initialization of measurement managers and get methods
	/// </summary>
   public partial class Program
   {
      // Declare all the instrument managers here
      private PowerSupplyMeasurementManager _psManager = null;
      private ProgramGuiManager _guiManager = null;

      /// <summary>
		/// Initialize power supply measurement manager
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public void InitializePowerSupplyMeasurementManager()
      {
         try
         {
            _psManager = new PowerSupplyMeasurementManager(_instrumentManager, _fileAndFolderManager.getFile(FileAndFolderManager.Files.POWER_SUPPLY_SELF_TEST_DATETIME));
            _psManager.Initialize();
         }
         catch (Exception ex)
         {
            _logger.Error(ex.Message + "\n" + ex.StackTrace);

            // DO NOT THROW in this block
            // this function will handle the error accordingly since we could be calling this from third-party test executive like TestStand
            TerminateTestOnMainThreadError(ex);
         }
      }

      /// <summary>
		/// Initialize GUI manager
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public void InitializeGuiManager()
      {
         try
         {
            _guiManager = new ProgramGuiManager();
            _guiManager.Initialize();
         }
         catch (Exception ex)
         {
            _logger.Error(ex.Message + "\n" + ex.StackTrace);

            // DO NOT THROW in this block
            // this function will handle the error accordingly since we could be calling this from third-party test executive like TestStand
            TerminateTestOnMainThreadError(ex);
         }
      }

      /// <summary>
      /// Start all the monitor/read threads here
      /// Call this only after ProgramGuiManager and PowerSupplyManager has been initialized since these threads could be
      /// accessing those objects
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      public void InitializeSupportThreads()
      {
         try
         {
            if (_threadList.Count() == 0)
            {
               _threadList.Add(new FailureMonitorThread());
               _threadList.Last().Start();

               ICollection<object> psList = _instrumentManager.GetInstruments(typeof(PowerSupply));

               foreach (PowerSupply ps in psList)
               {
                  _threadList.Add(new PowerSupplyReadThread(ps.Name));
                  _threadList.Last().Start();
               }

               _threadList.Add(new PassthroughDataGuiUpdateThread());
               _threadList.Last().Start();

               _threadList.Add(new PowerSupplyGuiUpdateThread());
               _threadList.Last().Start();
            }
         }
         catch (Exception ex)
         {
            _logger.Error(ex.Message + "\n" + ex.StackTrace);

            // DO NOT THROW in this block
            // this function will handle the error accordingly since we could be calling this from third-party test executive like TestStand
            TerminateTestOnMainThreadError(ex);
         }
      }

      /// <summary>
		/// Get power supply manager object
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public PowerSupplyMeasurementManager GetPowerSupplyMeasurementManager()
      {
         if (_psManager == null)
            InitializePowerSupplyMeasurementManager();

         return _psManager;
      }

      /// <summary>
		/// Get Gui manager object
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public ProgramGuiManager GetGuiManager()
      {
            if (_guiManager == null)
               InitializeGuiManager();

            return _guiManager;
      }
   }
}
