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

namespace ProgramLib
{
   /// <summary>
   /// Class to spawn thread to read power supply data
   /// </summary>
   internal class PowerSupplyReadThread : BasicThread
   {
      private enum Events
      {
         GLOBAL_QUIT,
         UUT_POWER_ON,
         UUT_POWER_OFF,

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

      private ILogger _logger;

      private string _powerSupplySystemName;

      private List<string> _powerModuleNameList;
      Dictionary<string, Raytheon.Instruments.PowerSupplies.PowerSupplyModuleInfo> _powerSupplyModuleInfoDict;

      /// <summary>
      /// Constructor
      /// </summary>
      public PowerSupplyReadThread(string powerSupplySystemName)
      {
         _logger = LogManager.GetCurrentClassLogger();

         _powerSupplySystemName = powerSupplySystemName;

      }

      ~PowerSupplyReadThread()
      {
         _logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() ...");
      }

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

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

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

           _powerModuleNameList = Program.Instance().GetPowerSupplyMeasurementManager()[_powerSupplySystemName].GetModuleNames();
           _powerSupplyModuleInfoDict = Program.Instance().GetPowerSupplyMeasurementManager()[_powerSupplySystemName].GetPowerSupplyModuleInfoDict();

            while (true)
            {
               Events id = eventGroup.WaitAny();

               if (id == Events.UUT_POWER_ON)
               {
                  ReadPowerSupplyData();
               }
               else
                  break;
            }
         }
         catch (Exception ex)
         {
            _logger?.Error(ex.Message + "\n" + ex.StackTrace);
         }

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

      }

      /// <summary>
      /// Read power supply data and check for faults
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      private void ReadPowerSupplyData()
      {
         int pollRateMs = 1000;
         _logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() for {_powerSupplySystemName} is running...");

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

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

            double voltage;
            double current;
            double voltageSetPoint;
            bool isOn;
            int faultStatus;

            while (true)
            {
               Events id = eventGroup.WaitAny(pollRateMs);

               if (id == Events.EVENT_TIMED_OUT)
               {
                  foreach (string moduleName in _powerModuleNameList)
                  {
                     Program.Instance().GetPowerSupplyMeasurementManager()[_powerSupplySystemName].ReadPowerSupplyData(moduleName, out voltage, out current, out voltageSetPoint, out isOn, out faultStatus);

                     BitArray statusReg = new BitArray(new int[] { faultStatus });

                     bool ovpTriggeredInPowerSupply = statusReg[0];
                     bool ocpTriggeredInPowerSupply = statusReg[1];

                     if (!ovpTriggeredInPowerSupply && !ocpTriggeredInPowerSupply)
                     {
                        if (IsCurrentWithinLimits(moduleName, current))
                           break;

                        if (IsVoltageWithinLimits(moduleName, voltage))
                           break;
                     }
                     else
                     {
                        string errorMsg = String.Empty;

                        if (ovpTriggeredInPowerSupply)
                        {
                           errorMsg = moduleName + "'s OVP circuitry has tripped";
                        }
                        else if (ocpTriggeredInPowerSupply)
                        {
                           errorMsg = moduleName + "'s OCP circuitry has tripped";
                        }

                        HandleOutOfToleranceCondition(errorMsg);

                        break;
                     }
                  }
               }
               else
                  break;
            }
         }
         catch (Exception ex)
         {
            _logger.Error(ex.Message + "\n" + ex.StackTrace);
         }

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

      /// <summary>
      /// Check for current's upper limit
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      private bool IsCurrentWithinLimits(string moduleName, double current)
      {
         bool outOfLimits = false;
         Raytheon.Instruments.PowerSupplies.PowerSupplyModuleInfo powerSupplyModuleInfo = _powerSupplyModuleInfoDict[moduleName];

         if ((current < powerSupplyModuleInfo.currentLowerLimit_ || current > powerSupplyModuleInfo.currentUpperLimit_) && powerSupplyModuleInfo.isOn_)
         {
            outOfLimits = true;
            string errorMsg = moduleName + "'s current is out of limits (" + powerSupplyModuleInfo.currentLowerLimit_.ToString("0.00") + "," + powerSupplyModuleInfo.currentUpperLimit_.ToString("0.00") + "). Current reading: " + current.ToString("0.00");
            HandleOutOfToleranceCondition(errorMsg);
         }

         return outOfLimits;
      }

      /// <summary>
      /// Check for voltage's limit exceedence
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      private bool IsVoltageWithinLimits(string moduleName, double voltage)
      {
         bool outOfLimits = false;
         Raytheon.Instruments.PowerSupplies.PowerSupplyModuleInfo powerSupplyModuleInfo = _powerSupplyModuleInfoDict[moduleName];

         if ((voltage < powerSupplyModuleInfo.voltageLowerLimit_ || voltage > powerSupplyModuleInfo.voltageUpperLimit_) && powerSupplyModuleInfo.isOn_)
         {
            outOfLimits = true;
            string errorMsg = moduleName + "'s voltage is out of limits (" + powerSupplyModuleInfo.voltageLowerLimit_.ToString("0.00") + "," + powerSupplyModuleInfo.voltageUpperLimit_.ToString("0.00") + "). Voltage reading: " + voltage.ToString("0.00");
            HandleOutOfToleranceCondition(errorMsg);
         }

         return outOfLimits;
      }

      /// <summary>
      /// Handle power fault conditions
      /// </summary>
      /// <param name=""></param>
      /// <returns></returns>
      private void HandleOutOfToleranceCondition(string errorMsg)
      {
         if (!Program.Instance()._eventManager[EventManager.Events.FATAL_FAILURE].WaitOne(0))
         {
            Program.Instance().SetFatalErrorMsgFromSupportThread(errorMsg);
            Program.Instance()._eventManager[EventManager.Events.FATAL_FAILURE].Set();
         }
      }

   }
}
