' 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.Text
Imports System.IO

Public Class IAM_alimentation_DP
    Public interaction_ As IAM_Info.interaction_


    Private Sub IAM_alimentation_DP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = "Interaction détéctée pendant le processus de facturation:" + Chr(10) + _
            interaction_.Md1CIP + " " + interaction_.Md1Lib + " " + interaction_.Md1Date + Chr(10) + _
            interaction_.Md2CIP + " " + interaction_.Md2Lib + " " + interaction_.Md2Date
    End Sub

    Private Sub Send_requeste(ByVal interaction_ As IAM_Info.interaction_)
        Dim X_DP_ReqId As String = System.Guid.NewGuid.ToString
        Try

            If DP_objRequest_has_been_used = True Then
                If Not DP_objRequest Is Nothing Then DP_objRequest.Headers.Clear()
            Else
                DP_objRequest_has_been_used = True
            End If

            DP_objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/iam")
            DP_objRequest.Timeout = "10000"

            DP_objRequest.Method = "POST"
            DP_objRequest.ClientCertificates.Add(DP.DP_get_certif)

            DP_objRequest.UserAgent = Config.DP_EtaTyp & "-" & Config.DP_EtaIdent & "-" & Config.DP_LogValid & "-" & Config.DP_LogVers & "-" & Config.DP_LogVDP
            DP_objRequest.ContentType = "application/vnd.cnop.dp.iam"



            DP_objRequest.Headers.Add("X-DP-ReqId", X_DP_ReqId)

            Dim postdata As String = My.Computer.FileSystem.ReadAllText(Config.LocalNetwork_ServerName & "\DP\XML\model\iam.xml")
            postdata = Replace(postdata, "${Md1Date}", interaction_.Md1Date)
            postdata = Replace(postdata, "${Md1CIP}", interaction_.Md1CIP)
            postdata = Replace(postdata, "${Md1DC}", interaction_.Md1DC)
            postdata = Replace(postdata, "${Md1CIS}", interaction_.Md1CIS)
            postdata = Replace(postdata, "${Md1UCD}", interaction_.Md1UCD)
            postdata = Replace(postdata, "${Md1ATC}", interaction_.Md1ATC)
            postdata = Replace(postdata, "${Md1Lib}", interaction_.Md1Lib)
            postdata = Replace(postdata, "${Md1QteC}", interaction_.Md1QteC)
            postdata = Replace(postdata, "${Md1QteU}", interaction_.Md1QteU)
            postdata = Replace(postdata, "${Md2Date}", interaction_.Md2Date)
            postdata = Replace(postdata, "${Md2CIP}", interaction_.Md2CIP)
            postdata = Replace(postdata, "${Md2DC}", interaction_.Md2DC)
            postdata = Replace(postdata, "${Md2CIS}", interaction_.Md2CIS)
            postdata = Replace(postdata, "${Md2UCD}", interaction_.Md2UCD)
            postdata = Replace(postdata, "${Md2ATC}", interaction_.Md2ATC)
            postdata = Replace(postdata, "${Md2Orig}", interaction_.Md2Orig)
            postdata = Replace(postdata, "${Md2Lib}", interaction_.Md2Lib)
            postdata = Replace(postdata, "${Md2QteC}", interaction_.Md2QteC)
            postdata = Replace(postdata, "${Md2QteU}", interaction_.Md2QteU)
            postdata = Replace(postdata, "${DPCtx}", interaction_.DPCtx)
            postdata = Replace(postdata, "${IamNiv}", interaction_.IamNiv)
            Dim interaction_IamText = interaction_.IamText
            If interaction_IamText.Length > 300 Then
                interaction_IamText = Mid(interaction_IamText, 1, 299)
            End If
            interaction_IamText = Replace(interaction_IamText, "&", " ")
            interaction_IamText = Replace(interaction_IamText, "<", " ")
            interaction_IamText = Replace(interaction_IamText, ">", " ")
            interaction_IamText = Replace(interaction_IamText, Chr(34), " ")
            postdata = Replace(postdata, "${IamText}", interaction_IamText)
            'If interaction_.IamText.Length > 300 Then
            '    postdata = Replace(postdata, "${IamText}", Mid(interaction_.IamText, 1, 299))
            'Else
            '    postdata = Replace(postdata, "${IamText}", interaction_.IamText)
            'End If



            If RadioButton1.Checked = True Then
                interaction_.PhaDeci = "11"
            ElseIf RadioButton2.Checked = True Then
                interaction_.PhaDeci = "12"
            ElseIf RadioButton3.Checked = True Then
                interaction_.PhaDeci = "13"
            ElseIf RadioButton4.Checked = True Then
                interaction_.PhaDeci = "21"
            ElseIf RadioButton5.Checked = True Then
                interaction_.PhaDeci = "22"
            ElseIf RadioButton6.Checked = True Then
                interaction_.PhaDeci = "31"
            ElseIf RadioButton7.Checked = True Then
                interaction_.PhaDeci = "41"
            Else
                interaction_.PhaDeci = ""
            End If
            postdata = Replace(postdata, "${PhaDeci}", interaction_.PhaDeci)
            postdata = Replace(postdata, "${PreTyp}", interaction_.PreTyp)
            postdata = Replace(postdata, "${DisDept}", interaction_.DisDept)
            postdata = Replace(postdata, "${EtaTyp}", Config.DP_EtaTyp)
            postdata = Replace(postdata, "${PatAge}", interaction_.PatAge)
            postdata = Replace(postdata, "${PatSexe}", interaction_.PatSexe)
            postdata = Replace(postdata, "${BdmNom}", "VIDAL") 'interaction_.BdmNom)
            postdata = Replace(postdata, "${BdmVers}", "200C") 'interaction_.BdmVers)
            postdata = Replace(postdata, "${BdmMaj}", "20120423") 'interaction_.BdmMaj)
            postdata = Replace(postdata, "${PiamNiv}", interaction_.Piam1)

            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\XML\log\" & X_DP_ReqId & ".xml", postdata, False)
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata)
            DP_objRequest.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = DP_objRequest.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()

            DP.Write_Trace_Tech(X_DP_ReqId, "ALIMENTATION_IAM", "")


            Dim DP_IAM_objResponse As System.Net.HttpWebResponse = DP_objRequest.GetResponse()



            Dim StreamReader As System.IO.StreamReader
            StreamReader = New System.IO.StreamReader(DP_IAM_objResponse.GetResponseStream())
            Dim réponse As String = StreamReader.ReadToEnd.ToString
            If réponse = "ERREUR_TECHNIQUE" Then
                Dim Generator2 As System.Random = New System.Random()
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "A_REEMETTRE", 1, Now.AddMinutes(Generator2.Next(0, 59)), "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", "ERREUR_TECHNIQUE")
            ElseIf réponse = "" Then
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "OK", 1, Now, "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", "OK")
            Else
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, réponse, 1, Now, "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", réponse)
            End If


        Catch wex As System.Net.WebException
            Dim Generator2 As System.Random = New System.Random()
            DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "A_REEMETTRE", 1, Now.AddMinutes(Generator2.Next(0, 59)), "IAM", "IAM")
            If Not wex.Response Is Nothing Then
                Dim StreamReader As System.IO.StreamReader
                StreamReader = New System.IO.StreamReader(wex.Response.GetResponseStream)

                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", CType(wex.Response, System.Net.HttpWebResponse).StatusCode & "|" & StreamReader.ReadToEnd.ToString)
                StreamReader.Close()
            Else
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", wex.Message.ToString)

                DP.reset_connection()

            End If
        Catch ex As Exception

            DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", ex.Message.ToString)
        End Try
    End Sub
    Private Sub Send_requeste_client(ByVal interaction_ As IAM_Info.interaction_)
        Dim X_DP_ReqId As String = System.Guid.NewGuid.ToString


        Dim postdata As String = My.Computer.FileSystem.ReadAllText(Config.LocalNetwork_ServerName & "\DP\XML\model\iam.xml")
        postdata = Replace(postdata, "${Md1Date}", interaction_.Md1Date)
        postdata = Replace(postdata, "${Md1CIP}", interaction_.Md1CIP)
        postdata = Replace(postdata, "${Md1DC}", interaction_.Md1DC)
        postdata = Replace(postdata, "${Md1CIS}", interaction_.Md1CIS)
        postdata = Replace(postdata, "${Md1UCD}", interaction_.Md1UCD)
        postdata = Replace(postdata, "${Md1ATC}", interaction_.Md1ATC)
        postdata = Replace(postdata, "${Md1Lib}", interaction_.Md1Lib)
        postdata = Replace(postdata, "${Md1QteC}", interaction_.Md1QteC)
        postdata = Replace(postdata, "${Md1QteU}", interaction_.Md1QteU)
        postdata = Replace(postdata, "${Md2Date}", interaction_.Md2Date)
        postdata = Replace(postdata, "${Md2CIP}", interaction_.Md2CIP)
        postdata = Replace(postdata, "${Md2DC}", interaction_.Md2DC)
        postdata = Replace(postdata, "${Md2CIS}", interaction_.Md2CIS)
        postdata = Replace(postdata, "${Md2UCD}", interaction_.Md2UCD)
        postdata = Replace(postdata, "${Md2ATC}", interaction_.Md2ATC)
        postdata = Replace(postdata, "${Md2Orig}", interaction_.Md2Orig)
        postdata = Replace(postdata, "${Md2Lib}", interaction_.Md2Lib)
        postdata = Replace(postdata, "${Md2QteC}", interaction_.Md2QteC)
        postdata = Replace(postdata, "${Md2QteU}", interaction_.Md2QteU)
        postdata = Replace(postdata, "${DPCtx}", interaction_.DPCtx)
        postdata = Replace(postdata, "${IamNiv}", interaction_.IamNiv)
        interaction_.IamText = Replace(interaction_.IamText, Chr(34), " ")
        interaction_.IamText = Replace(interaction_.IamText, "&", " ")
        interaction_.IamText = Replace(interaction_.IamText, "'", " ")
        interaction_.IamText = Replace(interaction_.IamText, "<", " ")
        interaction_.IamText = Replace(interaction_.IamText, ">", " ")
        Dim interaction_IamText = interaction_.IamText
        If interaction_IamText.Length > 300 Then
            interaction_IamText = Mid(interaction_IamText, 1, 299)
        End If
        interaction_IamText = Replace(interaction_IamText, "&", " ")
        interaction_IamText = Replace(interaction_IamText, "<", " ")
        interaction_IamText = Replace(interaction_IamText, ">", " ")
        interaction_IamText = Replace(interaction_IamText, Chr(34), " ")
        postdata = Replace(postdata, "${IamText}", interaction_IamText)
        'If interaction_.IamText.Length > 300 Then
        '    postdata = Replace(postdata, "${IamText}", Mid(interaction_.IamText, 1, 299))
        'Else
        '    postdata = Replace(postdata, "${IamText}", interaction_.IamText)
        'End If

        If RadioButton1.Checked = True Then
            interaction_.PhaDeci = "11"
        ElseIf RadioButton2.Checked = True Then
            interaction_.PhaDeci = "12"
        ElseIf RadioButton3.Checked = True Then
            interaction_.PhaDeci = "13"
        ElseIf RadioButton4.Checked = True Then
            interaction_.PhaDeci = "21"
        ElseIf RadioButton5.Checked = True Then
            interaction_.PhaDeci = "22"
        ElseIf RadioButton6.Checked = True Then
            interaction_.PhaDeci = "31"
        ElseIf RadioButton7.Checked = True Then
            interaction_.PhaDeci = "41"
        End If
        postdata = Replace(postdata, "${PhaDeci}", interaction_.PhaDeci)
        postdata = Replace(postdata, "${PreTyp}", "M") ' interaction_.PreTyp)
        postdata = Replace(postdata, "${DisDept}", interaction_.DisDept)
        postdata = Replace(postdata, "${EtaTyp}", Config.DP_EtaTyp)
        postdata = Replace(postdata, "${PatAge}", interaction_.PatAge)
        postdata = Replace(postdata, "${PatSexe}", interaction_.PatSexe)
        postdata = Replace(postdata, "${BdmNom}", "VIDAL") 'interaction_.BdmNom)
        postdata = Replace(postdata, "${BdmVers}", "200C") 'interaction_.BdmVers)
        postdata = Replace(postdata, "${BdmMaj}", "20120423") 'interaction_.BdmMaj)
        postdata = Replace(postdata, "${PiamNiv}", interaction_.Piam1)

        My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", postdata, False)

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Config.DP_is_local_client = True Then
            Send_requeste_client(interaction_)
        Else

            Send_requeste(interaction_)
        End If

        Me.Close()

    End Sub
End Class