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

namespace Raytheon.MeasurementManager.PowerSupply
{
   /// <summary>
	/// Define non-specific constants
	/// </summary>
   internal static class Util
   {
      /// <summary>
      /// Read from the XML file the time for this power system
      /// If time of power off is available, we return this time
      /// If time of last self test run is available, we return this time
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public static string GetLastSavedTime(string powerSystem, IConfigurationFile powerOffAndSelfTestConfig)
      {
         string savedTime = "";
         string powerOffTime = "";
         string selfTestTime = "";

         List<PowerSupplySelfTestTime> powerOffEntryListTemp = new List<PowerSupplySelfTestTime>();

         List<PowerSupplySelfTestTime> powerOffEntryList = powerOffAndSelfTestConfig.ReadList<PowerSupplySelfTestTime>(nameof(PowerSupplySelfTestTime), nameof(PowerSupplySelfTestTime), powerOffEntryListTemp);

         foreach (var powerOffEntry in powerOffEntryList)
         {
            if (powerOffEntry._powerSystem == powerSystem)
            {
               if (DateTime.TryParse(powerOffEntry._powerOffTime, out DateTime dt))
                  powerOffTime = powerOffEntry._powerOffTime;
               
               if (DateTime.TryParse(powerOffEntry._selfTestTime, out dt))
                  selfTestTime = powerOffEntry._selfTestTime;
            }
         }

         if (String.IsNullOrEmpty(powerOffTime))
            savedTime = selfTestTime;
         else if (String.IsNullOrEmpty(selfTestTime))
            savedTime = powerOffTime;
         else if (!String.IsNullOrEmpty(powerOffTime) && !String.IsNullOrEmpty(selfTestTime))
         {
            if (DateTime.TryParse(powerOffTime, out DateTime powerOffDt) && DateTime.TryParse(selfTestTime, out DateTime selfTestDt))
            {
               if (DateTime.Compare(powerOffDt, selfTestDt) == 1)
                  savedTime = powerOffTime;
               else
                  savedTime = selfTestTime;
            }
         }

         return savedTime;
      }

      /// <summary>
      /// Save time of power off or self test event
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public static void SaveTime(string powerSystem, string powerOffTime, string selfTestTime, IConfigurationFile powerOffAndSelfTestConfig)
      {
         List<PowerSupplySelfTestTime> powerOffEntryListTemp = new List<PowerSupplySelfTestTime>();

         List<PowerSupplySelfTestTime> powerOffEntryList = powerOffAndSelfTestConfig.ReadList<PowerSupplySelfTestTime>(nameof(PowerSupplySelfTestTime), nameof(PowerSupplySelfTestTime), powerOffEntryListTemp);

         var entry = powerOffEntryList.Find(b => b._powerSystem == powerSystem);

         if (entry != null)
         {
            if (!string.IsNullOrEmpty(powerOffTime))
               entry._powerOffTime = powerOffTime;

            if (!string.IsNullOrEmpty(selfTestTime))
               entry._selfTestTime = selfTestTime;
         }
         else
         {
            PowerSupplySelfTestTime data = new PowerSupplySelfTestTime(powerSystem, powerOffTime, selfTestTime);
            if (string.IsNullOrEmpty(powerOffTime))
               data._powerOffTime = Raytheon.Common.GeneralConstants.DefaultConfigValue;

            if (string.IsNullOrEmpty(selfTestTime))
               data._selfTestTime = Raytheon.Common.GeneralConstants.DefaultConfigValue;

            powerOffEntryList.Add(data);
         }

         powerOffAndSelfTestConfig.WriteList(nameof(PowerSupplySelfTestTime), nameof(PowerSupplySelfTestTime), powerOffEntryList);
      }
   }
}
