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

namespace Raytheon.Instruments
{
   /// <summary>
   /// CDTS HAL Oscilloscope Driver for ZTEC ZT4442
   /// </summary>
   public class ScopeZtec : IOscilloScope
   {
      #region PrivateMembers
      private enum enSCOPEInput { DC_50, DC_1M, AC_1M };

      private IntPtr _handle;
      private string _resourceString;
      private State _state;
      private SelfTestResult _selfTestResult;
      private string _name;

      private readonly ILogger _logger;

      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      #endregion

      #region PrivateFunctions
      /// <summary>
      /// The Finalizer which will release resources if required
      /// </summary>
      ~ScopeZtec()
      {
         Dispose(false);
      }

      /// <summary>
      /// Dispose of this object
      /// </summary>
      /// <param name="disposing"></param>
      protected virtual void Dispose(bool disposing)
      {
         if (disposing)
         {
            if (_state == State.Ready)
            {
               Reset();

               int ret = ScopeZtMNativeMethods.ztscopeM_close(_handle);

               _state = State.Uninitialized;
            }
         }
      }

      /// <summary>
      /// Check instrument error status. If there is a failure, get its error message and generate an exception.
      /// </summary>
      /// <param name="bErrorOccurred">if set to <c>true</c> if error occurred </param>
      /// <param name="nErrorCode">The error code.</param>
      /// <param name="strErrorMsg">The error message.</param>
      /// <returns></returns>
      private void CheckError(out bool bErrorOccurred, out int nErrorCode, out string strErrorMsg)
      {
         bErrorOccurred = false;
         nErrorCode = 0;
         strErrorMsg = "";
         int numberOfErrors = 0;
         int ztError = ScopeZtMNativeMethods.ztscopeM_error_count(_handle, out numberOfErrors);
         if (numberOfErrors == 0)
         {
            bErrorOccurred = false;
         }
         else
         {
            bErrorOccurred = true;
            int[] errors = new int[numberOfErrors];
            byte[] errMsg = new byte[256];
            ztError = ScopeZtMNativeMethods.ztscopeM_errors(_handle, out numberOfErrors, errors);
            //this also clear the error buffer
            nErrorCode = errors[numberOfErrors - 1]; //take last error
            ScopeZtMNativeMethods.ztscopeM_error_description(nErrorCode, errMsg);
            strErrorMsg = System.Text.Encoding.Default.GetString(errMsg);
         }
      }

      /// <summary>
      /// Discards any queued errors, clears the instrument status, 
      /// and resets the status and event registers.
      /// </summary>
      /// <returns></returns>
      private void ClearError()
      {
         int numberOfErrors = 0;
         ScopeZtMNativeMethods.ztscopeM_error_count(_handle, out numberOfErrors);
         if (numberOfErrors > 0)
         {
            int[] errors = new int[numberOfErrors];
            //this retrieve and clear the error buffer
            ScopeZtMNativeMethods.ztscopeM_errors(_handle, out numberOfErrors, errors);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="channel"></param>
      private void ClearMeasurement(int channel)
      {
         //@@@ not sure if this is the correct command
         if (0 != ScopeZtMNativeMethods.ztscopeM_calculate_measure_clear(_handle, channel))
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="channel"></param>
      public void Disable(int channel)
      {
         int ret = ScopeZtMNativeMethods.ztscopeM_channel_enable(_handle, channel, 0x00000000);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="channel"></param>
      public void Enable(int channel)
      {
         int ret = ScopeZtMNativeMethods.ztscopeM_channel_enable(_handle, channel, 0x00000001);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// Gets or sets Configure input termination for channel: DC_50=0, DC_1M=1, AC_1M =2
      /// Use one of the three subcommands to enable the appropriate signal input termination. 
      /// The subcommands correspond to DC coupling with 50 ohm termination resistance, 
      /// DC coupling with 1 Mohm termination resistance, 
      /// and AC coupling with 1 Mohm termination resistance, respectively.
      /// </summary>
      /// <value>
      /// Configure input termination for channel: DC_50=0, DC_1M=1, AC_1M =2
      /// </value>
      private void InputImpedance(enSCOPEInput input, int channel)
      {
         //only valid for input channels
         if (channel >= 4)
         {
            throw new Exception("invalid channel number input: " + channel.ToString());
         }


         int V_coupling = 0;
         double V_impedance = 0.0;
         switch (input)
         {
            //#define ZTSCOPEM_COUP_AC                        0x00000000
            //#define ZTSCOPEM_COUP_DC                        0x00000001
            //#define ZTSCOPEM_COUP_ACLFR                     0x00000002
            //#define ZTSCOPEM_COUP_GND                       0x00000003
            //#define ZTSCOPEM_IMP_50                         50.0
            //#define ZTSCOPEM_IMP_1M                         1000000.0
            case enSCOPEInput.DC_50:
               V_coupling = 0x00000001; //ZTSCOPEM_COUP_DC
               V_impedance = 50.0;     //50 Ohm - ZTSCOPEM_IMP_50
               break;
            case enSCOPEInput.DC_1M:
               V_coupling = 0x00000001; //ZTSCOPEM_COUP_DC
               V_impedance = 1000000.0;     //ZTSCOPEM_IMP_1M
               break;
            case enSCOPEInput.AC_1M:
               V_coupling = 0x00000000; //ZTSCOPEM_COUP_AC
               V_impedance = 1000000.0;     //ZTSCOPEM_IMP_1M
               break;
            default: //unknown, default to DC_1M
               V_coupling = 0x00000001; //ZTSCOPEM_COUP_DC
               V_impedance = 1000000.0;     //ZTSCOPEM_IMP_1M
               break;
         }

         int ret = ScopeZtMNativeMethods.ztscopeM_single_input_coupling(_handle, channel, V_coupling);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         ret = ScopeZtMNativeMethods.ztscopeM_single_input_impedance(_handle, channel, V_impedance);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// If there is error, throw an exception with error query information.
      /// </summary>
      private void RaiseExceptionAtError()
      {
         bool bErrorOccurred = false;
         int nErrorCode = 0;
         string strErrorMsg = "";
         CheckError(out bErrorOccurred, out nErrorCode, out strErrorMsg);
         if (bErrorOccurred)
         {
            throw new Exception(strErrorMsg);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void Run()
      {
         int ret = ScopeZtMNativeMethods.ztscopeM_initiate(_handle);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// Causes the Scope to stop acquiring data.
      /// </summary>
      /// <returns></returns>
      private void Stop()
      {
         int ret = ScopeZtMNativeMethods.ztscopeM_abort(_handle);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// Gets or sets the display timescale in seconds per division. This actually set Sweep time to be 10x time scale.
      /// </summary>
      /// <value>
      /// The display timescale in seconds per division
      /// </value>
      private double TimeScale
      {
         get
         {
            throw new NotImplementedException();

            /*double sweepTime;
				ZTScopeM.ztscopeM_single_horizontal_time_query(m_Handle, out sweepTime);
				return sweepTime / 10.0;*/
         }
         set
         {
            int ret = ScopeZtMNativeMethods.ztscopeM_single_horizontal_time(_handle, value * 10);
            if (0 != ret)
            {
               RaiseExceptionAtError();
            }
         }
      }

      /// <summary>
      /// Gets or sets The display voltage scale in Volts per division
      /// </summary>
      /// <value>
      /// The display voltage scale in Volts per division
      /// </value>
      private void VoltScale(int channel, double range)
      {
         //only valid for input channels
         if (channel >= 4)
         {
            throw new Exception("invalid channel number input: " + channel.ToString());
         }

         double V_range = 400.0;
         //need range check
         if (range > 40)
         {
            V_range = 400.0; //set to the maximum range
         }
         else
         {
            V_range = range * 10;
         }

         int ret = ScopeZtMNativeMethods.ztscopeM_single_input_range(_handle, channel, V_range);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// Gets or sets The display offset voltage in Volts
      /// A positive voltage offset shifts the displayed signal (as well as ground and trigger level indicators) downward; 
      /// in other words, the center of the vertical scale is set to dOffset.
      /// </summary>
      /// <value>
      /// The display offset voltage in Volts
      /// </value>
      private void VoltOffset(int channel, double offset)
      {
         //only valid for input channels
         if (channel >= 4)
         {
            throw new Exception("invalid channel number input: " + channel.ToString());
         }

         int ret = ScopeZtMNativeMethods.ztscopeM_single_input_offset(_handle, channel, offset);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }
      #endregion

      #region PublicFunctions

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

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

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

         _resourceString = _configuration.GetConfigurationValue("ScopeZtec", "ResourceString", "");

         _state = State.Uninitialized;
         _selfTestResult = SelfTestResult.Unknown;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="deviceName"></param>
      public ScopeZtec(string deviceName, string resourceString)
      {
         _name = deviceName;
         _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
         _resourceString = resourceString;
         _state = State.Uninitialized;
         _selfTestResult = SelfTestResult.Unknown;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool ClearErrors()
      {
         ClearError();

         return true;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="channelNumber"></param>
      /// <param name="timePerDivison"></param>
      /// <param name="timeOffset"></param>
      /// <param name="voltageOffset"></param>
      /// <param name="voltageScale"></param>
      /// <param name="inputImpedance"></param>
      public void ConfigureChannel(int channelNumber, double timePerDivison, double timeOffset, double voltageOffset, double voltageScale, string inputImpedance)
      {
         // make sure it is stopped first
         Stop();

         // clear the measurement
         ClearMeasurement(channelNumber);

         //1. time per division
         TimeScale = timePerDivison;

         //2. time offset
         int ret = ScopeZtMNativeMethods.ztscopeM_single_horizontal_offset_time(_handle, timeOffset);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         //3. Volt per division
         VoltScale(channelNumber, voltageScale);

         //4. Volt offset
         VoltOffset(channelNumber, voltageOffset);

         // set input impedance to 1M ohmns.
         enSCOPEInput imped = enSCOPEInput.DC_1M;

         // The value of inputImpedance is expected to be "DC", equivalent to 1M ohmn; otherwise, it outputs error.
         if (inputImpedance.ToUpper().Trim() != "DC".ToUpper())
         {
            throw new Exception("ScopeSetupChannel() expected Input Impedance to be DC. If others are needed, need to update sw");
         }

         InputImpedance(imped, channelNumber);

         // It turns on the specified channel.
         Enable(channelNumber);

         // start it back up
         Run();
      }

      /// <summary>
      /// 
      /// </summary>
      public string DetailedStatus
      {
         get
         {
            return "This is a CDTS ZTec Scope";
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public bool DisplayEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// Dispose of this objects resources
      /// </summary>
      public void Dispose()
      {
         Dispose(true);

         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// 
      /// </summary>
      public bool FrontPanelEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool HasItTriggered()
      {
         int state = 0;
         int ret = ScopeZtMNativeMethods.ztscopeM_trigger_event_query(_handle, out state);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         if (state == 1)
         {
            return true;
         }
         else
         {
            return false;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public InstrumentMetadata Info
      {
         get
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Initialize()
      {
         // if we have not yet been initialized, go ahead and create the socket
         if (_state == State.Uninitialized)
         {
            int ret = ScopeZtMNativeMethods.ztscopeM_initialize(_resourceString,
                                                   0, // id_query
                                                   1, // reset
                                                   out _handle);

            if (ret != 0)
            {
               throw new Exception("call to ztscopeM_initialize returned " + ret);
            }

            Reset();

            // we already connected to the instrument in the constructor
            _state = State.Ready;
         }
         else
         {
            throw new Exception("expected the state of System " + _name + " to be Uninitialized, state was: " + _state.ToString());
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="command"></param>
      /// <returns></returns>
      public string IOQuery(string command)
      {
         throw new NotImplementedException();

         // send the command
         /*IOWrite(command, false);

			// clear our buffer
			Array.Clear(_readBuffer, 0, _readBuffer.Length);

			// read from the response
			int numBytesRead = _tcpStream.Read(_readBuffer, 0, _readBuffer.Length);

			// convert response to a string
			string rspStr = ConvertToString(ref _readBuffer);

			// check for errors
			int err = 0;
			string errorMessage = GetErrorCode(out err);
			if (err != 0)
			{
			   throw new Exception("ScopeZtec:IOQuery() - Scope " + _name + " returned error code: " + err.ToString() + "," + errorMessage);
			}

			return rspStr;*/
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="command"></param>
      public void IOWrite(string command, bool shallWeCheckForError = true)
      {
         throw new NotImplementedException();

         // convert to a byte array
         /*byte[] commandBuffer = Encoding.ASCII.GetBytes(command);

			// send the data out
			_tcpStream.Write(commandBuffer, 0, commandBuffer.Length);

			// check for errors
			if (shallWeCheckForError == true)
			{
			   int err = 0;
			   string errorMessage = GetErrorCode(out err);
			   if (err != 0)
			   {
				  throw new Exception("ScopeZtec:IOWrite() - Scope " + _name + " returned error code: " + err.ToString() + "," + errorMessage);
			   }
			}*/
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasureFallTime(int channelNumber)
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="channelNumber"></param>
      /// <returns></returns>
      public double MeasureFrequency(int channelNumber)
      {
         double freq = -1.0;

         int ret = ScopeZtMNativeMethods.ztscopeM_measure_immediate(_handle, 0x0000000D, channelNumber, out freq);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         return freq;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasureMaxVoltage(int channelNumber)
      {
         double max = -1.0;

         int ret = ScopeZtMNativeMethods.ztscopeM_measure_immediate(_handle, 0x00000012, channelNumber, out max);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         return max;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasureMinVoltage(int channelNumber)
      {
         double min = 999.99;

         int ret = ScopeZtMNativeMethods.ztscopeM_measure_immediate(_handle, 0x00000013, channelNumber, out min);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         return min;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasurePulseWidth(int channelNumber)
      {
         double pw = -1.0;

         int ret = ScopeZtMNativeMethods.ztscopeM_measure_immediate(_handle, 0x0000001B, channelNumber, out pw);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         return pw;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasureRiseTime(int channelNumber)
      {
         double rt = -1.0;

         int ret = ScopeZtMNativeMethods.ztscopeM_measure_immediate(_handle, 0x00000020, channelNumber, out rt);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         return rt;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public double MeasureVoltageLevel(int channelNumber)
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      public string Name
      {
         get
         {
            return _name;
         }
         set { _name = value; }
      }

      /// <summary>
      /// 
      /// </summary>
      public SelfTestResult SelfTestResult
      {
         get
         {
            return _selfTestResult;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public State Status
      {
         get
         {
            return _state;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public SelfTestResult PerformSelfTest()
      {
         int selfTestStatus = -1;
         //Returns the test status register. 
         //Bit 0- Baseboard Register Test Failed Bit 
         //Bit 1- Unused Bit 2- Baseboard ROM Test Failed Bit
         //Bit 3- Unused 
         //Bit 4- Reference Oscillator Test Failed Bit 
         //Bit 5- DRAM Test Failed Bit 
         //Bit 6- FlashMemory Test Failed Bit 
         //Bit 7- Unused Bit 
         //Bit 8- Digitizer 1Register Test Failed Bit 
         //Bit 9- Digitizer 2Register Test Failed Bit 
         //Bit 10-15- Unused Bits
         int ret = ScopeZtMNativeMethods.ztscopeM_self_test(_handle, out selfTestStatus);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }

         if (selfTestStatus == 0)
         {
            _selfTestResult = SelfTestResult.Pass;
         }
         else
         {
            _selfTestResult = SelfTestResult.Fail;
            throw new Exception("Self Test Failed with an Error Code: " + selfTestStatus.ToString());
         }

         return _selfTestResult;
      }

      /// <summary>
      /// 
      /// </summary>
      public void Reset()
      {
         int ret = ScopeZtMNativeMethods.ztscopeM_reset(_handle);
         if (0 != ret)
         {
            RaiseExceptionAtError();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void SaveImage(string fileName)
      {
         throw new NotImplementedException();
         /*try
			{
			   string timeStamp_date = DateTime.Now.ToString("yyyyMMdd");
			   string timeStamp_sec = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
			   string folderTimeStamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");

			   string srcPath = $"{m_DOUBLESLASH}{_address}{m_IMAGE_DEST}{m_DESIRED_FOLDER}{m_SLASH}{timeStamp_date}{m_SLASH}{folderTimeStamp}{m_SLASH}";

			   bool exists = System.IO.Directory.Exists(srcPath);

			   if (!exists)
			   {
				  try
				  {
					 System.IO.Directory.CreateDirectory(srcPath);
					 //m_SrcDirectory = $"{m_DOUBLESLASH}{_address}{m_IMAGE_DEST}{m_DESIRED_FOLDER}{m_SLASH}{testUnit}";
					 // m_TimeStampeDate = timeStamp_date;
				  }
				  catch (Exception)
				  {
					 Reset();
					 throw;
				  }
			   }

			   string m_scopeFileName = $"{timeStamp_date}{m_SLASH}{folderTimeStamp}{m_SLASH}{fileName}_{timeStamp_sec}.{m_IMAGE_FORMAT}";

			   string filename = $"{srcPath}{fileName}_{timeStamp_sec}";

			   string command = m_SAVEIMAGECMD + m_DOUBLEQUOTES + filename + m_DOUBLEQUOTES + m_COMMA + m_IMAGE_FORMAT + m_COMMA + m_IMAGE_SOURCE + m_COMMA + m_OPTION_ON + "\n";
			   IOWrite(command);
			}
			catch (Exception)
			{
			   Reset();
			   throw;
			}*/
      }

      /// <summary>
      /// 
      /// </summary>
      public void SetupTrigger(bool useRisingEdge, int channelNumber, double triggerLevel)
      {
         const int RISING_TRIGGER = 0x00000000;
         const int FALLING_TRIGGER = 0x00000001;

         try
         {
            Stop();

            int ret = ScopeZtMNativeMethods.ztscopeM_single_trigger_type(_handle, 0x00000000);
            if (0 != ret)
            {
               RaiseExceptionAtError();
            }

            int trigSource = channelNumber + 0x0000000A;
            int triggerEdge = FALLING_TRIGGER;

            if (useRisingEdge == true)
            {
               triggerEdge = RISING_TRIGGER;
            }

            ret = ScopeZtMNativeMethods.ztscopeM_trigger(_handle, trigSource, triggerLevel, triggerEdge);
            if (0 != ret)
            {
               RaiseExceptionAtError();
            }

            //ret = ScopeZtMNativeMethods.ztscopeM_trigger_holdoff(_handle, dHoldoffDuration, 1);

            Run();
         }
         catch (Exception)
         {
            Reset();
            throw;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Shutdown()
      {
         string errorMsg = "";

         if (_state == State.Ready)
         {
            try
            {
               Reset();
            }
            catch (Exception err)
            {
               errorMsg += err.Message + " ";
            }

            _state = State.Uninitialized;
         }

         // the stream was created in the constructor, dispose of it here
         try
         {
            int ret = ScopeZtMNativeMethods.ztscopeM_close(_handle);
         }
         catch (Exception err)
         {
            errorMsg += err.Message + " ";
         }

         if (errorMsg != "")
         {
            throw new Exception("System " + _name + " Had an error: " + errorMsg);
         }
      }
      #endregion
   }
}
