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

namespace ProgramLib
{
   /// <summary>
	/// Initialization of measurement managers and get methods
	/// </summary>
   public partial class Program
   {
      private ProgramGuiManager _guiManager = null;

      internal bool _isUutPwrOn = false;
      internal PowerSupplySharedData PowerSupplySharedData { get; set; }

      /// <summary>
		/// Initialize power supply measurement manager
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public void InitializePowerSupplyMeasurementManager()
      {
         try
         {
            PowerSupplySharedData = new PowerSupplySharedData();
            MalMeasurementLibManager.InitializePowerSupplyMeasurementManager();
         }
         catch (Exception ex)
         {
            // 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 DIO measurement manager
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public void InitializeDioMeasurementManager()
      {
         try
         {
            MalMeasurementLibManager.InitializeDioMeasurementManager(); ;
         }
         catch (Exception ex)
         {
            // 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 switch measurement manager
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      public void InitializeSwitchMeasurementManager()
      {
         try
         {
            MalMeasurementLibManager.InitializeSwitchMeasurementManager();
         }
         catch (Exception ex)
         {
            // 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)
         {
            // 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> powerSystemList = _instrumentManager.GetInstruments(typeof(IPowerSupplySystem));

               foreach (IPowerSupplySystem powerSystem in powerSystemList)
               {
                  _threadList.Add(new PowerSupplyReadThread(powerSystem.Name));
                  _threadList.Last().Start();
               }

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

               _threadList.Add(new PowerSupplyUpdateThread());
               _threadList.Last().Start();
            }
         }
         catch (Exception ex)
         {
            // 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 Gui manager object
		/// </summary>
		/// <param name=""></param>
		/// <returns></returns>
      internal ProgramGuiManager GetGuiManager()
      {
            if (_guiManager == null)
               InitializeGuiManager();

            return _guiManager;
      }
   }
}
