Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class TAPIListener

    Public Property CallList As New Dictionary(Of String, Date)
    Dim SoundPlayer = New SoundPlayer(Nothing)

    <STAThread()> _
    Public Sub Init()
        AddHandler CommandRequestEvent, AddressOf CommandRequestHandler
    End Sub

    ''' <summary>
    ''' Handles commands from the events 
    ''' E.g. ring;-;06766063036
    ''' </summary>
    ''' <param name="cmd"></param>
    ''' <remarks></remarks>
    <STAThread()>
    Sub CommandRequestHandler(cmd As IntraSellCommand)
        Try
            If Not cmd.command = IntraSellCommands.ring Then
                Exit Sub
            End If

            IntraSell_Net_Base.ModuleLog.Log("TAPIListener.CommandRequestHandler " & cmd.Serialize)
            If TypeOf cmd IsNot IntraSellRingCommand Then
                Exit Sub
            End If

            Dim c = CType(cmd, IntraSellRingCommand)
            Dim telnummer As String = LandvorwahlEntfernen(c.Caller)

            Try
                If callList.ContainsKey(telnummer) Then
                    If callList(telnummer) > Now.AddSeconds(-30) Then 'ignore calls from same number from last 30 seconds 
                        IntraSell_Net_Base.ModuleLog.Log("TAPIListener.CommandRequestHandler Ignore " & cmd.ToString)
                        Exit Sub
                    End If
                End If
                callList.Remove(telnummer)
                callList.Add(telnummer, Now)
            Catch ex As Exception
                HandleAppError(ex, Silent:=True)
            End Try

            Try
                SoundPlayer.SoundPlayRing()
                'AnrufProtokollierenFuerAnrufendeNummer7


                Dim kundenSuchen1 = ModuleKunden.KundeSuchen(telnummer)
                Dim gefunden = AnrufProtokollierenFuerAnrufendeNummer(tel:=telnummer, Notizen:="Anruf von Tel " & telnummer, InformationPer:="")
                'Pop Up 
                Dim kundennamen = "Unbekannt"
                If gefunden > 0 Then
                    If kundenSuchen1.Count = 1 Then
                        kundennamen = KundeNamen(gefunden)
                    Else
                        kundennamen = "Mehrere Kunden gefunden: "
                        For Each kIDNR In kundenSuchen1
                            kundennamen += vbNewLine & KundeNamen(kIDNR) & " " & KundeAdresse(kIDNR)
                        Next
                    End If

                End If

                Dim aktion = New Aktion With {
                .ButtonText = "Anruf annehmen",
                .Params = New List(Of Object)
            }
                aktion.Params.Add(telnummer)
                Dim msgTitle = "Anruf von " & telnummer
                Dim msgText = "Sie bekommen Anruf von der Nummer " & telnummer & vbNewLine &
                    "Kunde " & kundennamen & "." & vbNewLine &
                    "Der Anruf wurde in der Anrufliste protokolliert."

                Dim msg = UserMessagePopUp(msgTitle, msgText,
                                           MsgBoxStyle.Information, aktion)
                AddHandler msg.OnAktion, AddressOf AnrufAnnehmenFuerAktion
            Catch ex As Exception
                HandleAppError(ex, Silent:=True)
            End Try

        Catch ex As Exception
            'macht nix.. weder msg noch log hilft mehr 
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    ''' <summary>
    ''' Landvorwahl aus der Nummer für AT Nummern entfernen
    ''' </summary>
    ''' <param name="telNr"></param>
    ''' <returns></returns>
    Public Function LandvorwahlEntfernen(telNr As String) As String
        If telNr Is Nothing Then
            Return telNr
        End If

        If telNr.StartsWith("%2B43") Then
            Return "0" & telNr.Substring(5)
        End If

        If telNr.StartsWith("0043") Then
            Return "0" & telNr.Substring(4)
        End If

        If telNr.StartsWith("+43") Then
            Return "0" & telNr.Substring(3)
        End If
        Return telNr
    End Function

    Private Sub AnrufAnnehmenFuerAktion(ByRef Aktion As Aktion)
        Try
            Dim telnummer = Aktion.Params(0)
            Dim tlBEst = New TelefonischeBestellung With {
                .MdiParent = Main
            }
            tlBEst.Show()
            tlBEst.SucheNachNummer(telnummer)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

End Class
