Namespace Parameters
    Public Class EventDataFormat
        Inherits ParameterFormat

        Enum E_FieldStartIndex
            E00_Reserved = 0
            E01_DataType = 4
            E02_DataSize = 5
            E03_DataBuffer = 6
            E04_Name = 22
            E05_XID = 38
        End Enum

        Enum E_FieldLength
            E00_Reserved = 4
            E01_DataType = 1
            E02_DataSize = 1
            E03_DataBuffer = 16
            E04_Name = 16
            E05_XID = 4
        End Enum

        Private _reservedByteArray(3) As Byte
        Private _dataType As Byte
        Private _dataSize As Byte
        Private _dataBufferByteArray() As Byte
        Private _dataBufferHexString As String
        Private _nameByteArray(15) As Byte
        Private _name As String
        Private _xidByteArray(3) As Byte
        Private _xidHexString As String

        ReadOnly Property DataType() As Byte
            Get
                Return _dataType
            End Get
        End Property

        ReadOnly Property DataByteArray() As Byte()
            Get
                Return _dataBufferByteArray
            End Get
        End Property

        ReadOnly Property DataHexString() As String
            Get
                Return MyLib.Encoding.ByteArrayToHexString(_dataBufferByteArray)
            End Get
        End Property

        ReadOnly Property Name() As String
            Get
                Return MyLib.Encoding.GetString(_nameByteArray)
            End Get
        End Property

        ReadOnly Property XID() As String
            Get
                Return Hex(_xidByteArray)
            End Get
        End Property


        Public Overloads Overrides Sub ParseParameter(ByRef parameterByteArray() As Byte)
            _errorCode = E_ErrorCodes.E01_Failed
            If parameterByteArray.Length <> 42 Then
                Throw New Exception("Event data bytes must be 42. Getting length is " & parameterByteArray.Length)
                Exit Sub
            End If

            Dim stepIndex As Integer
            Dim errorMessage As String

            stepIndex = 0
            errorMessage = ""
            'Start parse the fields from the input data bytes 
            Do
                Select Case stepIndex
                    Case 0
                        'Get Reserved field
                        Array.Copy(parameterByteArray, _
                                   E_FieldStartIndex.E00_Reserved, _
                                   _reservedByteArray, _
                                   0, _
                                   E_FieldLength.E00_Reserved)
                    Case 1
                        'Get Data Type field
                        _dataType = parameterByteArray(E_FieldStartIndex.E01_DataType)
                    Case 2
                        'Get Data Size field
                        _dataSize = parameterByteArray(E_FieldStartIndex.E02_DataSize)
                    Case 3
                        'Get Data Buffer
                        ReDim _dataBufferByteArray(_dataSize - 1)
                        Array.Copy(parameterByteArray, _
                                   E_FieldStartIndex.E03_DataBuffer, _
                                   _dataBufferByteArray, _
                                   0, _
                                   _dataSize)
                    Case 4
                        'Get Name
                        Array.Copy(parameterByteArray, _
                                   E_FieldStartIndex.E04_Name, _
                                   _nameByteArray, _
                                   0, _
                                   E_FieldLength.E04_Name)

                    Case 5
                        'Get XID Version
                        Array.Copy(parameterByteArray, _
                                   E_FieldStartIndex.E05_XID, _
                                   _xidByteArray, _
                                   0, _
                                   E_FieldLength.E05_XID)
                    Case 6
                        _errorCode = E_ErrorCodes.E00_Success
                        Exit Do
                    Case Else
                        Stop
                        Exit Do
                End Select

                stepIndex += 1
            Loop
        End Sub

        Public Overloads Overrides Sub ParseParameter(ByRef parameterByteArray() As Byte, _
                                                      ByVal startIndex As Integer, _
                                                      ByVal length As Integer)
            ParseParameter(parameterByteArray)
        End Sub
    End Class
End Namespace


