' 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/>.
Public Class IAM_Info

    Public Structure interaction_
        Dim Md1Date, Md1CIP, Md1UCD, Md1CIS, Md1ATC, Md1DC, Md1Lib, Md1QteC, Md1QteU As String
        Dim Md2Orig, Md2Date, Md2CIP, Md2UCD, Md2CIS, Md2ATC, Md2DC, Md2Lib, Md2QteC, Md2QteU As String
        Dim DisDept, PreTyp, DPCtx, IamNiv, IamText, PatAge, PatSexe, BdmNom, BdmVers, BdmMaj, Piam1, Piam2, Piam3, PhaDeci As String

    End Structure
    Public interactions_détectées As List(Of interaction_)
    Public Interactions_détectées_continuer_facturation As Boolean
    Public Structure Vidal_Produit
        Dim productId As String
        Dim origine As String
    End Structure
    Private Structure Product
        Dim Orig As String
        Dim ProductId As System.Nullable(Of Integer)
        Dim QteC As Integer
        Dim Date_ As String
        Dim Pretyp As String
    End Structure
    Private Product_list As List(Of Product)

    Public Sub recherche_IAM()


        RichTextBox1.Text = ""
        FlowLayoutPanel1.Controls.Clear()
        Application.DoEvents()

        Me.Visible = True
        Bandeau_info_bas_show("Recherche des interactions medicamenteuses ...")
        Try

            Product_list = New List(Of Product)

            'dispensation en cours
            For r = 0 To COMPTOIR_MAIN.Vente.RowCount - 2
                If Not COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.Vente_Vidal_ProductID.Index, r).Value Is Nothing Then
                    If COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.Vente_Vidal_ProductID.Index, r).Value.ToString <> "" Then
                        Dim prod As New Product
                        prod.ProductId = Val(COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.Vente_Vidal_ProductID.Index, r).Value)
                        prod.Orig = "C"
                        prod.QteC = Val(COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.VenteQuantité.Index, r).Value)
                        prod.Date_ = Format(COMPTOIR_MAIN.DateExecution.Value, "yyyyMMdd")
                        If UCase(COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.VenteType.Index, r).Value) = "VENTE DIRECTE" Then
                            prod.Pretyp = "O"
                        Else
                            prod.Pretyp = "M"
                        End If
                        Product_list.Add(prod)
                    End If
                End If
            Next

            'produits de l'historique local et DP
            Dim Vidal_PackService As New Vidal_PackService.PackService
            Vidal_PackService.Timeout = 3000
            Dim serveur_vidal_dispo As Boolean = True
            Try
                For r = 0 To COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView.RowCount - 1
                    If Not Vidal_PackService.searchByCip(COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView(COMPTOIR_MAIN.DP_form.CIP_Histo_DP.Index, r).Value) Is Nothing Then
                        Dim vidal_prod = Vidal_PackService.searchByCip(COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView(COMPTOIR_MAIN.DP_form.CIP_Histo_DP.Index, r).Value)
                        Dim not_in_Product_list = True
                        For Each prod As Product In Product_list
                            If Val(prod.ProductId) = Val(prod.ProductId) Then
                                not_in_Product_list = False
                                Exit For
                            End If
                        Next
                        If not_in_Product_list = True Then
                            Dim prod As New Product
                            prod.ProductId = vidal_prod.productId

                            If COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView(COMPTOIR_MAIN.DP_form.Origine_Histo_DP.Index, r).Value = "DP" Then
                                prod.Orig = "D" ' ‘D’ : historique DP  ‘L’ : historique local  ‘C’ : médicament de même dispensation en cours
                            Else
                                prod.Orig = "L"
                            End If
                            prod.QteC = Val(COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView(COMPTOIR_MAIN.DP_form.QteC_Histo_DP.Index, r).Value)
                            prod.Date_ = Format(COMPTOIR_MAIN.DP_form.Histo_DP_DataGridView(COMPTOIR_MAIN.DP_form.DisDate_Histo_DP.Index, r).Value, "yyyyMMdd")
                            prod.Pretyp = ""
                            Product_list.Add(prod)
                        End If
                    End If
                Next
            Catch ex As Exception
                serveur_vidal_dispo = False
                trace_log(ex.ToString)

            End Try

            'recharche des interactions
            Dim vidal As New VIDAL_InteractionService.InteractionService
            Dim interactionCouples() As VIDAL_InteractionService.interactionCouple
            'ReDim interactionCouples(100)
            Dim InteractionSeverityType As VIDAL_InteractionService.InteractionSeverityType
            Select Case Config.Piam1
                Case 1
                    InteractionSeverityType = VIDAL_InteractionService.InteractionSeverityType.TAKE_INTO_ACCOUNT
                Case 2
                    InteractionSeverityType = VIDAL_InteractionService.InteractionSeverityType.PRECAUTION_USE
                Case Else
                    InteractionSeverityType = VIDAL_InteractionService.InteractionSeverityType.DISADVISES_ASSOCIATION
                    'Case 3
                    '    InteractionSeverityType = VIDAL_InteractionService.InteractionSeverityType.DISADVISES_ASSOCIATION
                    'Case 4
                    '    InteractionSeverityType = VIDAL_InteractionService.InteractionSeverityType.CONTRAINDICATIONS
            End Select
            If serveur_vidal_dispo = True Then
                Dim ProductIds() As System.Nullable(Of Integer)
                ReDim ProductIds(Product_list.Count - 1)
                Dim idx = 0
                For Each prod As Product In Product_list
                    ProductIds(idx) = prod.ProductId
                    idx = idx + 1
                Next
                interactionCouples = vidal.getInteractionCouplesForProductIds(ProductIds, InteractionSeverityType).interactionCoupleList

                interactions_détectées = New List(Of interaction_)

                If interactionCouples.Length > 0 Then
                    RichTextBox1.Text = (interactionCouples.Count.ToString & " INTERACTION(s) DETECTÉE(s)")
                    For i = 0 To interactionCouples.Length - 1
                        Dim interaction_item As New interaction_
                        Dim prodA, prodB As New Product
                        For Each prod As Product In Product_list
                            If interactionCouples(i).productA.id = prod.ProductId Then
                                prodA = prod
                            ElseIf interactionCouples(i).productB.id = prod.ProductId Then
                                prodB = prod
                            End If
                        Next
                        If prodA.Pretyp <> "" Or prodB.Pretyp <> "" Then


                            Dim prodA_Vidal = Vidal_PackService.searchByProductId(prodA.ProductId)(0)
                            Dim prodB_Vidal = Vidal_PackService.searchByProductId(prodB.ProductId)(0)
                            interaction_item.Md1Date = prodA.Date_
                            interaction_item.Md1CIP = prodA_Vidal.cip13
                            interaction_item.Md1UCD = ""
                            interaction_item.Md1CIS = prodA_Vidal.cis
                            interaction_item.Md1ATC = ""
                            interaction_item.Md1DC = prodA_Vidal.name
                            interaction_item.Md1Lib = prodA_Vidal.prescriptionName
                            interaction_item.Md1QteC = prodA.QteC
                            interaction_item.Md1QteU = ""

                            interaction_item.Md2Orig = prodB.Orig ' ‘D’ : historique DP  ‘L’ : historique local  ‘C’ : médicament de même dispensation en cours
                            interaction_item.Md2Date = prodB.Date_
                            interaction_item.Md2CIP = prodB_Vidal.cip13
                            interaction_item.Md2UCD = ""
                            interaction_item.Md2CIS = prodB_Vidal.cis
                            interaction_item.Md2ATC = ""
                            interaction_item.Md2DC = prodB_Vidal.name
                            interaction_item.Md2Lib = prodB_Vidal.prescriptionName
                            interaction_item.Md2QteC = prodB.QteC
                            interaction_item.Md2QteU = ""

                            interaction_item.DisDept = "54" 'départemment
                            interaction_item.PreTyp = ""
                            Select Case COMPTOIR_MAIN.DP_LinkLabel.Text
                                Case "DP existant"
                                    If COMPTOIR_MAIN.LePatientSopposeÀLaConsultationDeSonDPToolStripMenuItem.Checked = False Then
                                        interaction_item.DPCtx = "C"
                                    Else
                                        interaction_item.DPCtx = "R"
                                    End If
                                Case "Créer DP"
                                    interaction_item.DPCtx = "P"
                                Case Else '"DP incomplet"
                                    interaction_item.DPCtx = "S"
                            End Select
                            Select Case interactionCouples(i).severity
                                Case VIDAL_InteractionService.InteractionSeverityType.TAKE_INTO_ACCOUNT
                                    interaction_item.IamNiv = "1"
                                Case VIDAL_InteractionService.InteractionSeverityType.PRECAUTION_USE
                                    interaction_item.IamNiv = "2"
                                Case VIDAL_InteractionService.InteractionSeverityType.DISADVISES_ASSOCIATION
                                    interaction_item.IamNiv = "3"
                                Case VIDAL_InteractionService.InteractionSeverityType.CONTRAINDICATIONS
                                    interaction_item.IamNiv = "4"
                            End Select

                            interaction_item.IamText = interactionCouples(i).riskComment
                            interaction_item.PatAge = Mid(CurrentBenéficiaire.G104C12, 1, 6)
                            If CurrentBenéficiaire.G104C9 <> "" Then
                                If Mid(CurrentBenéficiaire.G104C9, 1, 1) = "1" Then
                                    interaction_item.PatSexe = "M"
                                ElseIf Mid(CurrentBenéficiaire.G104C9, 1, 1) = "2" Then
                                    interaction_item.PatSexe = "F"
                                Else
                                    interaction_item.PatSexe = "U"
                                End If
                            Else
                                If Mid(CurrentBenéficiaire.G101C8, 1, 1) = "1" Then
                                    interaction_item.PatSexe = "M"
                                ElseIf Mid(CurrentBenéficiaire.G101C8, 1, 1) = "2" Then
                                    interaction_item.PatSexe = "F"
                                Else
                                    interaction_item.PatSexe = "U"
                                End If
                            End If
                            interaction_item.BdmNom = ""
                            interaction_item.BdmVers = ""
                            interaction_item.BdmMaj = ""
                            interaction_item.Piam1 = Config.Piam1
                            interaction_item.PhaDeci = ""
                            If prodA.Pretyp <> "" Then
                                interaction_item.PreTyp = prodA.Pretyp
                            ElseIf prodB.Pretyp <> "" Then
                                interaction_item.PreTyp = prodB.Pretyp
                            End If


                            If Not interactions_détectées.Contains(interaction_item) Then
                                Dim display As New IAM_Display
                                Select Case interaction_item.IamNiv
                                    Case "1"
                                        display.IamNiv_GroupBox.Text = "PRENDRE EN COMPTE"
                                    Case "2"
                                        display.IamNiv_GroupBox.Text = "PRECAUTION D'EMPLOI"
                                    Case "3"
                                        display.IamNiv_GroupBox.Text = "ASSOCIATION DECONSEILLEE"
                                    Case Is = "4"
                                        display.IamNiv_GroupBox.Text = "CONTRE_INDICATION"
                                End Select

                                Select Case prodA.Orig
                                    Case "D"
                                        display.ProdA_Label.Text = "(DP) " & prodA_Vidal.name
                                    Case "L"
                                        display.ProdA_Label.Text = "(Histo) " & prodA_Vidal.name
                                    Case Else
                                        display.ProdA_Label.Text = prodA_Vidal.name
                                End Select
                                Select Case prodB.Orig
                                    Case "D"
                                        display.ProdB_Label.Text = "(DP) " & prodB_Vidal.name
                                    Case "L"
                                        display.ProdB_Label.Text = "(Histo) " & prodB_Vidal.name
                                    Case Else
                                        display.ProdB_Label.Text = prodB_Vidal.name
                                End Select
                                display.IAM_RichTextBox.Text = interactionCouples(i).riskComment & Chr(13) & interactionCouples(i).precautionComment
                                FlowLayoutPanel1.Controls.Add(display)
                                interactions_détectées.Add(interaction_item)

                            End If
                        End If

                    Next

                Else
                    RichTextBox1.Text = ("AUCUNE INTERACTION DETECTÉE")
                    Interactions_détectées_continuer_facturation = True
                    Me.Visible = False
                End If
                Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
            Else
                RichTextBox1.Text = ("LE SERVEUR VIDAL N'A PAS REPONDU, AUCUNE INTERACTION N'A PU ETRE DETECTÉE")
                Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
            End If
        Catch ex As Exception
            RichTextBox1.Text = ("LE SERVEUR VIDAL N'A PAS REPONDU, AUCUNE INTERACTION N'A PU ETRE DETECTÉE")
            Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
        End Try


    End Sub
  

  
    Private Sub Prise_en_compte_Button_Click(sender As Object, e As EventArgs) Handles Prise_en_compte_Button.Click
        Interactions_détectées_continuer_facturation = True
        Me.Visible = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Interactions_détectées_continuer_facturation = False
        Me.Visible = False
    End Sub



    

    Private Sub IAM_Info_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If RichTextBox1.Text <> "" Then
            Me.Visible = True
        Else
            'Interactions_détectées_continuer_facturation = True
            'Me.Visible = False

        End If
    End Sub

 
End Class