Imports System.IO
Imports IntraSell_DLL
Imports IntraSell_DLL.Printing
Imports IntraSell_Net_Base

Module ModuleZustellung

    Public Sub ErstelleGoogleMapsLink(link As LinkLabel, vorgang As VorgangInBearbeitung)
        Try
            Dim sql = "select plz, ort, adresse  from ofAdressen where plz is not null and ort is not null and  IDNR = " & vorgang.KundNr
            Dim rs = OpenRecordsetInMemory(sql)
            If rs.Rows.Count = 1 Then
                Dim plz = rs.Rows(0)("plz")
                Dim ort = rs.Rows(0)("ort")
                Dim adresse As String = ""

                If Not IsDBNull(rs.Rows(0)("adresse")) Then
                    adresse = rs.Rows(0)("adresse")
                    If adresse.Contains(",") Then
                        adresse = Split(adresse, ",")(0)
                    End If
                End If

                Dim params = UrlEncode(adresse) & ",+" & UrlEncode(plz) & "+" & UrlEncode(ort) & ",+Austria"
                link.Tag = "https://www.google.com/maps/place/" & params
                link.Text = plz & " " & ort & ", " & adresse

                RemoveHandler link.Click, AddressOf OpenLink
                AddHandler link.Click, AddressOf OpenLink
                link.Visible = True
            Else
                link.Visible = False
            End If

        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try

    End Sub

    ''' <summary>
    ''' TODO: mit der Funktion oben zusamenlegen
    ''' </summary>
    ''' <param name="idnr"></param>
    ''' <returns></returns>
    Function AdresseAlsGoogleMapsLink(idnr As Integer) As String
        Try
            Dim sql = "select plz, ort, adresse from ofAdressen where IDNR = " & idnr
            Dim rs = OpenRecordsetInMemory(sql)
            If rs.Rows.Count > 0 Then
                Dim plz = rs.Rows(0)("plz")
                Dim ort = CStr(rs.Rows(0)("ort"))
                Dim adresse As String = rs.Rows(0)("adresse")
                If Not IsDBNull(adresse) AndAlso Not IsDBNull(ort) _
                    AndAlso adresse.Length > 3 AndAlso ort.Length > 3 Then
                    If adresse.Contains(",") Then
                        adresse = Split(adresse, ",")(0)
                    End If
                    Dim params = UrlEncode(adresse) & ",+" & UrlEncode(plz) & "+" & UrlEncode(ort) & ",+Austria"
                    Return "https://www.google.com/maps/place/" & params
                End If
            End If
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Function AdresseAlsGoogleMapsLinkEFCore(idnr As Integer)
        Dim adresse = IntraSellContext.Adressen.Find(idnr)
        Dim params = $"{Web.HttpUtility.UrlEncode(adresse.Adresse)},+{Web.HttpUtility.UrlEncode(adresse.PLZ)}+{Web.HttpUtility.UrlEncode(adresse.Ort)},+Austria"
        Dim link = $"https://www.google.com/maps/place/{params}"
        Return link
    End Function


    Private Sub OpenLink(sender As Object, e As EventArgs)
        Dim link As LinkLabel = sender
        Process.Start(link.Tag)
    End Sub

    Private Function UrlEncode(input As String) As Object
        Return Web.HttpUtility.UrlEncode(input)
    End Function


#Region "Adresse als QR Code"

    ''' <summary>
    ''' Kundenadresse als QR Code drucken
    ''' </summary>
    ''' <param name="idnr"></param>
    ''' <returns></returns>
    Function PrintQRCodeAdresseAlsQRCode(idnr As Integer) As String
        Dim qrCodeFile = Nothing

        If SaveQRCodeAdresse(idnr, qrCodeFile) AndAlso File.Exists(qrCodeFile) Then
            Dim qrCodeFilenamePOS = qrCodeFile.Replace(".png", ".pos")
            Dim QRCodeImage As Image = Image.FromFile(qrCodeFile)
            Dim prt As New ClassPrinter(Nothing, 3, QRCodeImage)
            If Not File.Exists(qrCodeFilenamePOS) Then
                prt.LoadImageToPOSPrinter(QRCodeImage, printFileImmediately:=False, tmpFilename:=qrCodeFilenamePOS)
                AppendToFile(qrCodeFilenamePOS, "Tipp: QR Code scannen mit Handy -> Navigation")
            End If
            prt.PrintCachedLogo(tmpFilename:=qrCodeFilenamePOS)
        End If

        Return qrCodeFile
    End Function

    ''' <summary>
    ''' Kundenadresse als QR Code Bitmap speichern
    ''' </summary>
    ''' <param name="idnr">Kunden ID</param>
    ''' <param name="filename">Ziel Dateiname</param>
    ''' <returns>true wenn erfolgreich, sonst false</returns>
    Public Function SaveQRCodeAdresse(idnr As Integer, ByRef filename As String) As Boolean
        Try
            filename = "google_maps_adresse_" & idnr & ".png"

            If File.Exists(filename) Then
                Return True
            End If

            Dim link = AdresseAlsGoogleMapsLink(idnr)
            If link IsNot Nothing AndAlso link.Length > 5 Then
                Dim qrUtil = New QRCoder.QRCodeUtil()
                Dim bitmap = qrUtil.RenderQrCode(link)
                bitmap.Save(filename, Imaging.ImageFormat.Png)
                Return True
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
        Return False
    End Function


    ''' <summary>
    ''' Kundenadresse als QR Code Bitmap speichern
    ''' </summary>
    ''' <param name="idnr">Kunden ID</param>
    ''' <param name="filename">Ziel Dateiname</param>
    ''' <returns>true wenn erfolgreich, sonst false</returns>
    Public Function SaveQRCodeFeedback(idnr As Integer, ByRef filename As String) As Boolean
        Try
            filename = "order_feedback_mine_" & idnr & ".png"

            If File.Exists(filename) Then
                Return True
            End If

            Dim tel = FirstRow("select IFNULL(Tel, Mobil) from ofAdressen where idnr = " & idnr)
            If Not IsDBNull(tel) Then
                Dim link = VarValue_Default(Variablen.ORDER_FEEDBACK_URL, "https://weborder.pro/order-feedback-mine/[tel]")
                If link IsNot Nothing Then
                    link = link.Replace("[tel]", UrlEncode(tel))
                    Dim qrUtil = New QRCoder.QRCodeUtil()
                    Dim bitmap = qrUtil.RenderQrCode(link)
                    bitmap.Save(filename, Imaging.ImageFormat.Png)
                    Return True
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
        Return False
    End Function


#End Region


End Module
