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

namespace ProgramLib
{
   /// <summary>
	/// Monitor fatal failure signaled by other threads
	/// </summary>
   internal class FailureMonitorThread : BasicThread
   {
      private enum Events
      {
         GLOBAL_QUIT,
         FATAL_FAILURE,

         // DO NOT change the name
         // This must be the last member in the enum
         EVENT_TIMED_OUT
      }

      private ILogger _logger;

      /// <summary>
		/// Constructor
		/// </summary>
      public FailureMonitorThread()
      {
         _logger = LogManager.GetCurrentClassLogger();
      }

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

      /// <summary>
		/// Method that executes on the thread.
		/// </summary>
      protected override void DoWork()
      {
         _logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is running...");

         try
         {
            Dictionary<Events, EventWaitHandle> eventDict = new Dictionary<Events, EventWaitHandle>();
            eventDict[Events.GLOBAL_QUIT] = Program.Instance()._eventManager[EventManager.Events.GLOBAL_QUIT];
            eventDict[Events.FATAL_FAILURE] = Program.Instance()._eventManager[EventManager.Events.FATAL_FAILURE];
            eventDict[Events.EVENT_TIMED_OUT] = null;

            EventGroup<Events, EventWaitHandle> eventGroup = new EventGroup<Events, EventWaitHandle>(eventDict);

            Events id = eventGroup.WaitAny();

            if (id == Events.FATAL_FAILURE)
            {
               Program.Instance()._eventManager[EventManager.Events.GLOBAL_QUIT].Set();

               UutPowerAction uutPowerAction = new UutPowerAction();
               uutPowerAction.UutPowerOff();

               Program.Instance().TerminateTestOnSupportThreadError();
            }
         }
         catch (Exception ex)
         {
            _logger.Error(ex.Message + "\n" + ex.StackTrace);
         }

         _logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is exiting...");

      }
   }
}
