Imports Limilabs.Mail
Imports DigitalData.Modules.Base

Public Class MailContainer
    Private Const SUBJECT_MAX_LENGTH = 25

    ''' <summary>
    ''' The Mail object created by Limilabs
    ''' </summary>
    Public ReadOnly Property Mail As IMail

    ''' <summary>
    ''' The IMAP Id coming from the IMAP folder. Used to reference the mail.
    ''' </summary>
    Public ReadOnly Property ImapId As Integer

    ''' <summary>
    ''' The original MessageID from the eml file
    ''' </summary>
    Public ReadOnly Property MessageIdOriginal As String

    ''' <summary>
    ''' The new MessageID, which is generated by hashing the original MessageID
    ''' </summary>
    Public Property MessageId As String

    Public ReadOnly Property EMailDate As Date

    Public ReadOnly Property MessageIDHashSet As HashSet(Of String) = New HashSet(Of String)

    ''' <summary>
    ''' The subject, truncated to SUBJECT_MAX_LENGTH characters
    ''' </summary>
    Public ReadOnly Property Subject As String

    Public ReadOnly Property SubjectOriginal As String

    Public ReadOnly Property SenderDomain As String

    Public ReadOnly Property SenderAddress As String

    Public Sub New(pMail As IMail, pImapId As Integer)
        Mail = pMail
        ImapId = pImapId

        MessageIdOriginal = pMail.MessageID
        MessageId = StringEx.GetShortHash(pMail.MessageID)

        Subject = ObjectEx.NotNull(pMail.Subject.Truncate(SUBJECT_MAX_LENGTH), String.Empty)
        SubjectOriginal = ObjectEx.NotNull(pMail.Subject, String.Empty)

        SenderAddress = GetSenderAddress(pMail)
        SenderDomain = GetSenderDomain(pMail)

        EMailDate = GetEmailDate(pMail)
    End Sub

    ''' <summary>
    ''' Erzeugt eine Liste von n unterschiedlichen MessageIDs
    ''' Dabei verwenden wir den vorherigen Wert, um daraus einen neuen zu erzeugen
    ''' </summary>
    Public Sub InitializeHashsetOfMessageIDs()
        MessageIDHashSet.Clear()

        Dim nextMessageID As String

        If String.IsNullOrEmpty(MessageIdOriginal) = True Then
            nextMessageID = "1234567890ABCDEFGHIJKLMNOPQRSTUV" ' irgendwelche 32 Zeichen
        Else
            nextMessageID = MessageIdOriginal
        End If

        For index = 1 To 10
            Dim zeitMitMs As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
            nextMessageID = nextMessageID + "_" + index.ToString + "_" + zeitMitMs
            nextMessageID = StringEx.GetShortHash(nextMessageID)

            MessageIDHashSet.Add(nextMessageID)
        Next

    End Sub

    Private Function GetSenderAddress(pMail As IMail)
        Dim oMailBox = pMail.From.FirstOrDefault()
        Return oMailBox?.Address
    End Function

    Private Function GetSenderDomain(pMail As IMail)
        Dim oMailBox = pMail.From.FirstOrDefault()
        Return oMailBox.DomainPart
    End Function

    Public Sub SetMessageID(pMessageID As String)
        MessageId = pMessageID
    End Sub

    Private Function GetEmailDate(pMail As IMail) As Date
        If pMail.Date IsNot Nothing Then
            Return pMail.Date
        Else
            Return Date.MinValue
        End If
    End Function

End Class
