Imports System.Web.Mail
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Xml.Serialization
Imports CAMS_BL

Public Class Notification
    Private _recipients() As Recipient
    Private _strCaseID As String
    Private _strApplNIN As String
    Private _type As NotificationType
    Private _AppEmail As String = ConfigurationSettings.AppSettings("AppEmail")

#Region "Properties"

    Public Property Recipients() As Recipient()
        Get
            Return _recipients
        End Get
        Set(ByVal Value As Recipient())
            _recipients = Value
        End Set
    End Property

    Public Property CaseID() As String
        Get
            Return _strCaseID
        End Get
        Set(ByVal Value As String)
            _strCaseID = Value
        End Set
    End Property

    Public Property ApplNIN() As String
        Get
            Return _strApplNIN
        End Get
        Set(ByVal Value As String)
            _strApplNIN = Value
        End Set
    End Property

    Public Property Notification_Type() As NotificationType
        Get
            Return _type
        End Get
        Set(ByVal Value As NotificationType)
            _type = Value
        End Set
    End Property

#End Region

    Sub New(ByVal prmCaseID As String, ByVal prmApplNIN As String)
        _strCaseID = prmCaseID
        _strApplNIN = prmApplNIN
    End Sub

    Sub New(ByVal prmCaseID As String, ByVal prmApplNIN As String, ByVal prmRecipients As Recipient())
        _strCaseID = prmCaseID
        _strApplNIN = prmApplNIN
        _recipients = prmRecipients
    End Sub

    Private Shared Sub SendNotification(ByVal prmFrom As String, ByVal prmTo As String, _
                                    ByVal prmSubject As String, ByVal prmBody As String, Optional ByVal prmFilePath As String = "", _
                                    Optional ByVal prmCc As String = "", Optional ByVal prmBcc As String = "")

        Dim oMensaje As New MailMessage

        With oMensaje
            .From = prmFrom
            .To = prmTo
            .Subject = prmSubject
            .BodyFormat = MailFormat.Html
            .Body = prmBody

            If prmFilePath <> String.Empty Then .Attachments.Add(New MailAttachment(prmFilePath, MailEncoding.Base64))

            If prmCc <> String.Empty Then .Cc = prmCc

            If prmBcc <> String.Empty Then .Bcc = prmBcc

        End With

        Try
            SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer")
            SmtpMail.Send(oMensaje)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub ProcessNotification()
        If _recipients Is Nothing OrElse _recipients.Length = 0 Then Exit Sub
        If _AppEmail.Equals(String.Empty) Then Exit Sub

        Dim strSubjectFormat As String
        Dim strBodyFormat As String
        Dim oClient As EC_Client = GetClient(_strApplNIN)
        Dim strFrom As String = _AppEmail
        Dim strTo As String = ""
        Dim strCc As String = ""
        Dim strBcc As String = ""
        Dim oRecipient As Recipient

        If oClient Is Nothing Then Exit Sub

        If _type = NotificationType.SL21Issued Then
            strSubjectFormat = Constants.CONST_NOTIF_FORMAT_SL21ISSUED_SUBJECT
            strBodyFormat = Constants.CONST_NOTIF_FORMAT_SL21ISSUED_BODY
        ElseIf _type = NotificationType.CaseUnlocked Then
            strSubjectFormat = Constants.CONST_NOTIF_FORMAT_CASEUNLOCKED_SUBJECT
            strBodyFormat = Constants.CONST_NOTIF_FORMAT_CASEUNLOCKED_BODY
        End If

        strSubjectFormat = strSubjectFormat.Replace("<CASEID>", _strCaseID)
        strSubjectFormat = strSubjectFormat.Replace("<APPLNIN>", _strApplNIN)

        strBodyFormat = strBodyFormat.Replace("<SURNAME>", oClient.Surname)
        strBodyFormat = strBodyFormat.Replace("<NAME>", oClient.Name)

        For Each oRecipient In _recipients
            If oRecipient.SendAs = SendType.SEND_To Then
                strTo &= oRecipient.Email & "; "
            ElseIf oRecipient.SendAs = SendType.SEND_Cc Then
                strCc &= oRecipient.Email & "; "
            ElseIf oRecipient.SendAs = SendType.SEND_Bcc Then
                strBcc &= oRecipient.Email & "; "
            End If
        Next

        strTo = strTo.Trim
        strCc = strCc.Trim
        strBcc = strBcc.Trim

        If strTo.EndsWith(";") Then
            strTo = strTo.Substring(0, strTo.Length - 1)
        End If

        If strCc.EndsWith(";") Then
            strCc = strCc.Substring(0, strCc.Length - 1)
        End If

        If strBcc.EndsWith(";") Then
            strBcc = strBcc.Substring(0, strBcc.Length - 1)
        End If

        SendNotification(strFrom, strTo, strSubjectFormat, strBodyFormat, , strCc, strBcc)

    End Sub

    Private Function GetClient(ByVal strApplNIN As String) As EC_Client
        Dim oCC_Client As New CC_Client
        Dim oEC_Client As EC_Client

        Try
            oEC_Client = oCC_Client.GetClientByID(strApplNIN)
        Catch ex As Exception
            oEC_Client = Nothing
        End Try

        Return oEC_Client
    End Function

End Class

<Serializable()> _
Public Class Recipient
    Public Email As String
    Public SendAs As SendType
End Class

Public Enum SendType
    SEND_To = 1
    SEND_Cc = 2
    SEND_Bcc = 3
End Enum

Public Enum NotificationType
    SL21Issued = 1
    CaseUnlocked = 2
End Enum