' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.Xml

Public Class VIDAL_Rest
    'http://editeur.vidal.fr/demo/rest/index.php
    'http://apirest-dev.vidal.fr/
    'http://api.vidal.fr/
    Public app_id As String = "dc93b2e1"
    Public app_key As String = "d31d0dbe2fbf1c47ed7581013530a6b6"

    Public Structure lppr_struct
        Dim codelpp, prixlpp, Prestation, Type_prestation, Libellé, coef As String
    End Structure
    Public Sub LPPR_Sub(ByVal Vidal_Id As String, ByVal app_id As String, ByRef lppr_struct As List(Of lppr_struct))
        'app_id=dc93b2e1
        'app_key=d31d0dbe2fbf1c47ed7581013530a6b6
        Try

            Dim req = System.Net.HttpWebRequest.CreateHttp("http://apirest-dev.vidal.fr/rest/api/package/" & Vidal_Id & "/lppr?app_id=" & app_id & "&app_key=" & app_key)
            req.Timeout = "10000"
            req.Method = "GET"
            Dim rep As System.Net.HttpWebResponse
            rep = req.GetResponse()
            Dim StreamReader As System.IO.StreamReader
            StreamReader = New System.IO.StreamReader(rep.GetResponseStream())
            Dim doc As New XmlDocument
            doc.LoadXml(StreamReader.ReadToEnd)
            Dim root As XmlNode = doc.DocumentElement
            Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
            nsmgr.AddNamespace("bk", "http://www.w3.org/2005/Atom")
            Dim nsmgr2 As New XmlNamespaceManager(doc.NameTable)
            nsmgr2.AddNamespace("bk2", "http://api.vidal.net/-/spec/vidal-api/1.0/")
            Dim entries As XmlNodeList = root.SelectNodes("descendant::bk:entry", nsmgr)

            For Each entry As XmlNode In entries
                If entry.Attributes("vidal:categories").Value.ToString = "LPPR" Then
                    Dim my_lppr_struct As New lppr_struct
                    For Each child_node As XmlNode In entry.ChildNodes

                        Select Case child_node.Name
                            Case "vidal:code"
                                my_lppr_struct.codelpp = child_node.InnerText
                            Case "vidal:refundBase"
                                my_lppr_struct.prixlpp = child_node.InnerText
                            Case "vidal:act"
                                my_lppr_struct.Prestation = child_node.Attributes("name").Value
                            Case "vidal:service"
                                Select Case child_node.InnerText
                                    Case "BUYING"
                                        my_lppr_struct.Type_prestation = "A"
                                    Case "DELIVERY"
                                        my_lppr_struct.Type_prestation = "V"
                                    Case "MAINTENANCE"
                                        my_lppr_struct.Type_prestation = "E"
                                    Case "RENTING"
                                        my_lppr_struct.Type_prestation = "L"
                                    Case "REPAIR"
                                        my_lppr_struct.Type_prestation = "R"
                                    Case "SERVICE"
                                        my_lppr_struct.Type_prestation = "S"
                                    Case Else
                                        my_lppr_struct.Type_prestation = "P"
                                End Select
                            Case "vidal:name"
                                my_lppr_struct.Libellé = child_node.InnerText
                            Case "vidal:coef"
                                my_lppr_struct.coef = child_node.InnerText
                        End Select
                    Next
                    If COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(my_lppr_struct.codelpp, Val(Format(Now, "yyyyMMdd"))).Rows.Count = 0 Then
                        COMPTOIR_MAIN.LPPTableAdapter.Insert(my_lppr_struct.codelpp, (Val(my_lppr_struct.prixlpp) * 100).ToString, "99999999", "00010101", False, my_lppr_struct.Prestation, "200", "", "", False, False, _
                                                             my_lppr_struct.Libellé, "", "", "", "", "", my_lppr_struct.Type_prestation)
                    End If
                    my_lppr_struct = Nothing

                End If
            Next
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", ex.ToString, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End Try
    End Sub
    Public Function LPPR_get_Coef(ByVal Vidal_Id As String, ByVal codelpp As String) As Decimal
        'app_id=dc93b2e1
        'app_key=d31d0dbe2fbf1c47ed7581013530a6b6
        'Try

        Dim req = System.Net.HttpWebRequest.CreateHttp("http://apirest-dev.vidal.fr/rest/api/package/" & Vidal_Id & "/lppr?app_id=" & app_id & "&app_key=" & app_key)
        req.Timeout = "30000"
        req.Method = "GET"
        Dim rep As System.Net.HttpWebResponse
        rep = req.GetResponse()
        Dim StreamReader As System.IO.StreamReader
        StreamReader = New System.IO.StreamReader(rep.GetResponseStream())
        Dim doc As New XmlDocument
        doc.LoadXml(StreamReader.ReadToEnd)
        Dim root As XmlNode = doc.DocumentElement
        Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
        nsmgr.AddNamespace("bk", "http://www.w3.org/2005/Atom")
        Dim nsmgr2 As New XmlNamespaceManager(doc.NameTable)
        nsmgr2.AddNamespace("bk2", "http://api.vidal.net/-/spec/vidal-api/1.0/")
        Dim entries As XmlNodeList = root.SelectNodes("descendant::bk:entry", nsmgr)

        For Each entry As XmlNode In entries
            If entry.Attributes("vidal:categories").Value.ToString = "LPPR" Then
                Dim codelpp_found As String = ""
                Dim coef As Integer
                For Each child_node As XmlNode In entry.ChildNodes

                    Select Case child_node.Name
                        Case "vidal:code"
                            codelpp_found = child_node.InnerText

                        Case "vidal:coef"
                            coef = Val(child_node.InnerText)
                    End Select
                Next
                If codelpp_found = codelpp Then
                    Return coef
                End If
            End If
        Next
        Return 0
    End Function

    Public Function Search_by_string(ByVal searched As String, ByVal filter As String) As XmlDocument
        Try

            'http://api.vidal.fr
            'Dim req = System.Net.HttpWebRequest.CreateHttp("http://api.vidal.fr/rest/api/search?q=&code=3302210&filter=&app_id=" & app_id & "&app_key=" & app_key)
            Dim req = System.Net.HttpWebRequest.CreateHttp("http://api.vidal.fr/rest/api/search?q=" & searched & "&app_id=" & app_id & "&app_key=" & app_key)
            req.Timeout = "30000"
            req.Method = "GET"
            Dim rep As System.Net.HttpWebResponse
            rep = req.GetResponse()
            Dim StreamReader As System.IO.StreamReader
            StreamReader = New System.IO.StreamReader(rep.GetResponseStream())
            Dim doc As New XmlDocument
            doc.LoadXml(StreamReader.ReadToEnd)
            Return doc

        Catch ex As Exception
            Return Nothing
        End Try

    End Function
    Public Function Search_by_code(ByVal searched As String, ByVal filter As String) As XmlDocument
        Try

       
        Dim req = System.Net.HttpWebRequest.CreateHttp("http://api.vidal.fr/rest/api/search?q=&code=" & searched & "&filter=&app_id=" & app_id & "&app_key=" & app_key)
        req.Timeout = "30000"
        req.Method = "GET"
        Dim rep As System.Net.HttpWebResponse
        rep = req.GetResponse()
        Dim StreamReader As System.IO.StreamReader
        StreamReader = New System.IO.StreamReader(rep.GetResponseStream())
        Dim doc As New XmlDocument
        doc.LoadXml(StreamReader.ReadToEnd)
            Return doc
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function LPPR_by_codeVidal(ByVal codeVidal) As XmlDocument
        Try
            Dim req = System.Net.HttpWebRequest.CreateHttp("http://api.vidal.fr/rest/api/package/" & codeVidal & "/lppr?" & "&app_id=" & app_id & "&app_key=" & app_key)
            req.Timeout = "30000"
            req.Method = "GET"
            Dim rep As System.Net.HttpWebResponse
            rep = req.GetResponse()
            Dim StreamReader As System.IO.StreamReader
            StreamReader = New System.IO.StreamReader(rep.GetResponseStream())
            Dim doc As New XmlDocument
            doc.LoadXml(StreamReader.ReadToEnd)
            Return doc
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
End Class
