''' <summary>
''' Module contenant quelques fonction utiles
''' </summary>
''' <remarks></remarks>
Module MdlCommonNH
    ''' <summary>
    ''' Retourne une des 2 expressionss
    ''' </summary>
    ''' <typeparam name="T">Type à utiliser</typeparam>
    ''' <param name="pCondition">Condition</param>
    ''' <param name="pTrue">Valeur retournée si <c>pCondition</c> est <c>True</c></param>
    ''' <param name="pFalse">Valeur retournée si <c>pCondition</c> est <c>False</c></param>
    ''' <returns></returns>
    ''' <remarks>Permet d'éviter l'utilisation de <c>CType</c> avec <c>IIF</c> par défaut</remarks>
    Public Function IIF(Of T)(ByVal pCondition As Boolean, ByVal pTrue As T, ByVal pFalse As T) As T
        If pCondition Then
            Return pTrue
        Else
            Return pFalse
        End If
    End Function

    ''' <summary>
    ''' Transfert d'un tableau d'octet vers une string
    ''' </summary>
    ''' <param name="ptArray">Tableau à transferrer</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Array2String(ByVal ptArray As Byte()) As String
        Dim lStr As String = ""

        For i As Integer = 0 To ptArray.GetUpperBound(0)
            lStr += " " + Right("00" + Hex$(ptArray(i)), 2)
        Next

        Return lStr.Trim
    End Function

    ''' <summary>
    ''' Retourne un nombre entier
    ''' </summary>
    ''' <param name="pMin">Valeur minimale désirée</param>
    ''' <param name="pMax">Valeur maximale désirée</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Rand(ByVal pMin As Integer, ByVal pMax As Integer) As Integer
        Return CInt(CInt(Int((pMax - pMin + 1) * Rnd() + pMin)))
    End Function

    ''' <summary>
    ''' Retourne la durée en secondes au format D HH:MM:SS
    ''' </summary>
    ''' <param name="pSec">Durée en secondes</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SecToHMS(ByVal pSec As Integer) As String
        Dim lStr As String = ""

        lStr = String.Format(":{0:00}", pSec Mod 60)
        pSec \= 60
        lStr = String.Format(":{0:00}", pSec Mod 60) + lStr
        pSec \= 60
        lStr = String.Format("j {0:00}", pSec Mod 24) + lStr
        pSec \= 24
        lStr = CStr(pSec) + lStr

        Return lStr
    End Function


#Region "CRC et opération tableaux"
    ''' <summary>
    ''' Calcule le CRC16 d'un tableau d'octet
    ''' </summary>
    ''' <param name="ptByte">Tableau à calculer</param>
    ''' <param name="pExistCRC">Signal si les 2 derniers octets  sont à ignorer (contenant déjà un CRC)</param>
    Public Function CalcCRC16(ByVal ptByte As Byte(), Optional ByVal pExistCRC As Boolean = False) As UShort
        Dim j As Integer
        Dim Crc As Integer
        Dim n As Integer
        Dim Retenue As Boolean

        Crc = &HFFFF
        For j = 0 To ptByte.GetUpperBound(0) - IIF(pExistCRC, 2, 0)
            Crc = Crc Xor CInt(ptByte(j))
            n = 0
            Do
                Retenue = False
                If (Crc Mod &H2) <> 0 Then
                    Retenue = True
                End If
                Crc = Crc \ 2
                If Retenue Then
                    Crc = Crc Xor &HA001
                End If
                n = n + 1
            Loop While (n <= 7)
        Next j

        Return CUShort(Crc)
    End Function

    ''' <summary>
    ''' Convertit le tableau de 2 octets en UShort
    ''' </summary>
    ''' <param name="ptByte">Tableau de 2 octet</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ByteArrayToUShortCRC(ByVal ptByte() As Byte) As UShort
        Dim i As Integer = ptByte.GetUpperBound(0)
        If i < 1 Then
            Return 0
        Else
            Return CUShort(CUShort(ptByte(i)) * 256) + ptByte(i - 1)
        End If
    End Function

    ''' <summary>
    ''' Convertit  un UShort en tableau de 2 octets
    ''' </summary>
    ''' <param name="pCRC">UShort à convertir</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UShortToByteArrayCRC(ByVal pCRC As UShort) As Byte()
        Return New Byte() {CByte(pCRC And &HFF), CByte((pCRC And &HFF00) \ &H100)}
    End Function

    ''' <summary>
    ''' Retourne une partie d'un tableau
    ''' </summary>
    ''' <param name="ptByte">Tableau à traiter</param>
    ''' <param name="pOffset">Offset de départ</param>
    ''' <param name="pLength">Longueur</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetArrayRange(ByVal ptByte As Byte(), ByVal pOffset As Integer, ByVal pLength As Integer) As Byte()
        Dim ltByte As New List(Of Byte)
        ltByte.AddRange(ptByte)
        Return ltByte.GetRange(pOffset, pLength).ToArray
    End Function
#End Region
End Module
