Imports IntraSell_DLL
Imports IntraSell_Net_Base
Imports IntraSell_Net_Base.dsAdressen

Public Class WerbeNachricht
    Inherits AbstractForm

    Dim neueNachricht As ServiceSMS.KundenNotifizierung
    Shared Property Kundenliste As IntraSell_Net_Base.dsAdressen.ofAdressenlisteDataTable

    Private Sub MyBase_Verlassen() Handles MyBase.Verlassen
        Me.WindowState = FormWindowState.Minimized
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub WerbeNachricht_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.ParentBindingNavigator.Visible = False
            If ExistsVarValue("SMS_BETREFF") Then
                TextBoxBetreff.Text = VarValue("SMS_BETREFF")
            End If

            If ExistsVarValue("SMS_TEXT") Then
                TextBoxNachricht.Text = VarValue("SMS_TEXT")
            End If

            If ExistsVarValue("SMS_MOBILE") Then
                TextBoxAbsenderMobile.Text = VarValue("SMS_MOBILE")
            Else
                TextBoxAbsenderMobile.Text = VarValue(Variablen.FIRMA_TEL)
            End If

            If ExistsVarValue("NACHRICHT_ABSENDER_EMAIL") Then
                TextBoxAbsenderEmail.Text = VarValue("NACHRICHT_ABSENDER_EMAIL")
            Else
                TextBoxAbsenderEmail.Text = VarValue(Variablen.FIRMA_EMAIL)
            End If

            If Not Kundenliste Is Nothing Then
                TextBoxEmfaenger.Text = Kundenliste.Rows.Count & " Kunden ausgewählt!"
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonGenerateSMS_Click(sender As Object, e As EventArgs) Handles ButtonGenerateSMS.Click
        Try
            SetVarValue("SMS_BETREFF", TextBoxBetreff.Text)
            SetVarValue("SMS_TEXT", TextBoxNachricht.Text)

            Dim MAX_SMS_TOSEND = VarValue_Default("MAX_SMS_TOSEND", 3)
            If TextBoxNachricht.Text.Length > 160 Then
                MsgBox("Diese Nachricht hat " & TextBoxNachricht.Text.Length & " Zeichen! Eine SMS hat maximal 160 Zeichen!", vbOKOnly)
                Exit Sub
            End If
            Dim cnt = 0
            Dim m As New dsMailtosend.mailstosendDataTable
            Dim tab As New dsMailtosendTableAdapters.mailstosendTableAdapter
            For Each x As IntraSell_Net_Base.dsAdressen.ofAdressenlisteRow In Kundenliste.Rows
                If Not ModuleAnrufe.HatHeuteNachrichtBekommen(x.IDNR) Then
                    Dim newR = m.NewmailstosendRow()
                    newR.Subject = TextBoxBetreff.Text
                    'TODO Platzhalter
                    newR.MailText = TextBoxNachricht.Text
                    newR.Recipient = x.IDNR
                    m.AddmailstosendRow(newR)

                    AnrufProtokollieren(x.IDNR, TextBoxBetreff.Text + ":" + TextBoxNachricht.Text, "SMS/Email")
                    cnt += 1
                End If

                If cnt >= MAX_SMS_TOSEND Then
                    Exit For
                End If
            Next
            tab.Update(m)
            MsgBox("Es wurden " & cnt & " Nachrichten von Maximal " & MAX_SMS_TOSEND & " generiert! Lassen Sie das Fenster offen bis alle SMS gesendet sind.")

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TimerSend_Tick(sender As Object, e As EventArgs) Handles TimerSend.Tick
        Try
            Dim m As New dsMailtosend.mailstosendDataTable
            Dim tab As New dsMailtosendTableAdapters.mailstosendTableAdapter

            Dim tabKunden As New ofadressenDataTable
            Dim tabAdapterKunden As New dsAdressenTableAdapters.ofadressenTableAdapter
            Dim kunde As dsAdressen.ofadressenRow

            tab.Fill(m)
            Label1.Text = "Es gibt " & m.Rows.Count & " Nachrichten zu senden!"
            If m.Rows.Count > 0 Then
                Dim mRow As dsMailtosend.mailstosendRow = m.Rows(0)
                neueNachricht = New ServiceSMS.KundenNotifizierung
                tabKunden.Clear()
                tabAdapterKunden.FillByIDNR(tabKunden, mRow.Recipient)
                kunde = tabKunden.Rows(0)

                Dim kundemobil As String = ""
                Dim kundetel As String = ""
                Dim kundeemail As String = ""
                Try
                    If Not kunde.IsMobilNull Then kundemobil = kunde.Mobil
                    If Not kunde.IsTelNull Then kundetel = kunde.Tel
                    If Not kunde.IsEmailNull Then kundeemail = kunde.Email
                Catch ex As Exception

                End Try
                If kundemobil.Length > 5 Or kundetel.Length > 5 Then
                    If kundemobil.Length > 5 Then
                        neueNachricht.SendSMS = True
                        neueNachricht.MobileNummerEmpfaenger = kundemobil
                    Else
                        If kundetel.Length > 5 Then
                            neueNachricht.SendSMS = True
                            neueNachricht.MobileNummerEmpfaenger = kundetel
                        End If
                    End If
                    neueNachricht.MobileNummerAbsender = Me.TextBoxAbsenderMobile.Text
                End If

                If kundeemail.Length > 5 Then
                    neueNachricht.SendEmail = True
                    neueNachricht.EmailBetreff = mRow.Subject
                    neueNachricht.EmailAdresseEmpfaenger = kundeemail
                    neueNachricht.EmailAdresseAbsender = Me.TextBoxAbsenderEmail.Text
                End If

                neueNachricht.Nachricht = mRow.MailText
                SendAsync(neueNachricht)

                tab.Delete(mRow.ID)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TextBoxAbsenderMobile_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAbsenderMobile.TextChanged
        SetVarValue("SMS_MOBILE", TextBoxAbsenderMobile.Text)
    End Sub

    Private Sub TextBoxAbsenderEmail_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAbsenderEmail.TextChanged
        SetVarValue("NACHRICHT_ABSENDER_EMAIL", TextBoxAbsenderEmail.Text)
    End Sub

    Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            TextBoxNachricht.Focus()
            TextBoxNachricht.SelectedText = ListBox1.SelectedItem
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class