//                           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.Threading;
using NLog;
using Raytheon.Common;

namespace Raytheon.Instruments
{
   /// <summary>
   /// A simulated chiller
   /// </summary>
   public class ChillerSim : IChiller, IDisposable
   {
      #region PrivateMembers
      private double _setPoint;
      private static object _sync = new object();

      public string DetailedStatus { get; protected set; }

      public bool DisplayEnabled { get; set; }
      public bool FrontPanelEnabled { get; set; }

      public InstrumentMetadata Info { get; set; }

      public string Name { get; protected set; }

      public SelfTestResult SelfTestResult => PerformSelfTest();

      public State Status { get; set; }

      private readonly ILogger _logger;

      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      #endregion

      #region PrivateFunctions
      ~ChillerSim()
      {
         Dispose(false);
      }
      /// <summary>
      /// Dispose of the object's resources.
      /// </summary>
      /// <param name="disposing">Currently disposing.</param>
      protected virtual void Dispose(bool disposing)
      {

      }
      #endregion

      #region PublicFunctions

      /// <summary>
      /// ChillerFTS factory constructor
      /// </summary>
      /// <param name="deviceName"></param>
      /// <param name="configurationManager"></param>
      public ChillerSim(string deviceName, IConfigurationManager configurationManager)
      {
         Name = deviceName;

         _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");

         _configurationManager = configurationManager;
         _configuration = _configurationManager.GetConfiguration(Name);
      }

      /// <summary>
      /// The constructor
      /// </summary>
      /// <param name="deviceName"></param>
      public ChillerSim(string deviceName)
      {
         Name = deviceName;
         _setPoint = 20.0;
         _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
      }


      /// <summary>
      /// Stop the chiller pump.
      /// </summary>
      public void DisableFlow()
      {
      }

      /// <summary>
      /// Dispose of this objects resources
      /// </summary>
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
      public void Dispose()
      {
         Dispose(true);

         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// Start the chiller pump.
      /// </summary>
      public void EnableFlow()
      {
      }

      /// <summary>
      /// Query the current setting for the coolant.
      /// </summary>
      /// <returns>The current coolant setting.</returns>
      public double GetCoolantSetpoint()
      {
         return _setPoint;
      }

      /// <summary>
      /// Query the temperature of the coolant reservoir.
      /// </summary>
      /// <returns>The temperature of the coolant.</returns>
      public double GetCoolantTemperature()
      {
         double max = _setPoint + 5;

         double min = _setPoint - 5;

         Random rnd = new Random();

         double seed = rnd.NextDouble();

         double dataToReturn = (seed * (max - min)) + min;

         Thread.Sleep(100);

         return dataToReturn;
      }

      /// <summary>
      /// Set the coolant temperature to a desired setpoint.
      /// </summary>
      /// <param name="temp">The desired coolant temperature.</param>
      public void SetCoolantTemperature(double temp)
      {
         _setPoint = temp;
      }

      public bool ClearErrors()
      {
         throw new NotImplementedException();
      }

      public void Initialize()
      {
         throw new NotImplementedException();
      }

      public SelfTestResult PerformSelfTest()
      {
         throw new NotImplementedException();
      }

      public void Reset()
      {
         throw new NotImplementedException();
      }

      public void Shutdown()
      {
         throw new NotImplementedException();
      }

      #endregion
   }
}
