' 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.IO
Public Class Config_Lecteur

    Private Sub Lire_Session(ByVal numerosession)
        Me.UseWaitCursor = True
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim IdGroupeCourant As UInt16 = 0
        Dim OccurrenceGroupeCourant As UInt16 = 0
        Dim CompteurChampsTotal As Integer = 0
        While SGD_LireGroupeSuivant(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) = 0
            RichTextBox1.AppendText("Groupe:" + IdGroupeSuivant.ToString + "    Occurrence:" + OccurrenceGroupeSuivant.ToString + Chr(10))
            IdGroupeCourant = IdGroupeSuivant
            OccurrenceGroupeCourant = OccurrenceGroupeSuivant
            Dim i As UInt16 = 1
            Dim ValeurChamp As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            Dim TailleMax As Integer = 80
            While SGD_LireChamp(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, i, ValeurChamp, TailleMax) = 0
                CompteurChampsTotal = CompteurChampsTotal + 1
                RichTextBox1.AppendText("   Champ " + i.ToString + ":" + ValeurChamp + "  CompteurChampsTotal:" + CompteurChampsTotal.ToString)
                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = 80
                RichTextBox1.AppendText(Chr(10))
                i = i + 1
            End While
        End While
        Me.UseWaitCursor = False
    End Sub
    Private Sub Button_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clear.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub Button_LireConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LireConfig.Click
        Dim numerosession As Integer
        SGD_ReserverSession(1000, numerosession)
        SIS_LireConfig(numerosession)
        Lire_Session(numerosession)
        SGD_LibererSession(numerosession)
    End Sub

    Private Sub Button_LireDateLecteur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LireDateLecteur.Click
        Dim dateheure As String = "AAAAMMJJhhmm"
        SIS_LireDateLecteur(NomRessourceLecteur, dateheure)
        RichTextBox1.AppendText(Chr(10) + "Date Heure Lecteur: " + dateheure + Chr(10))
    End Sub

    Private Sub Button_MajDateLecteur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_MajDateLecteur.Click
        Dim dateheure As String = InputBox("Date Heure au fotmat AAAAMMJJhhmm:", "Saisie", Format(Now, "yyyyMMddhhmm"))
        Dim codeerreur As Integer = SIS_MajDateLecteur(NomRessourceLecteur, dateheure)
        If codeerreur = 0 Then
            RichTextBox1.AppendText(Chr(10) + "Date Heure Lecteur: " + dateheure + Chr(10))
        Else
            RichTextBox1.AppendText(Chr(10) + "Echec MAJDate Heure Lecteur: " + codeerreur.ToString + Chr(10))
        End If
    End Sub

    Private Sub Charger_Appli_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Charger_Appli_Button.Click
        RichTextBox1.AppendText("LA PROCEDURE CHARGEMENT DE L'APPLICATION DEMARRE" + Chr(10))
        Dim numerosession As Integer

        RichTextBox1.AppendText("RESERVER SESSION " + SGD_ReserverSession(900000, numerosession).ToString + Chr(10))
        RichTextBox1.AppendText("AJOUT GROUPE " + SGD_AjouterGroupe(numerosession, 51).ToString + Chr(10))
        Dim champ1 As Byte() = {&H1, &HFF, &HFF}
        ' Create a file and write the byte data to a file.
        Dim oFileStream As System.IO.FileStream
        oFileStream = New System.IO.FileStream("c:\bytesazertyqsdfgh", System.IO.FileMode.Create)
        oFileStream.Write(champ1, 0, champ1.Length)
        oFileStream.Close()
        RichTextBox1.AppendText(" CHAMP 1 " + SGD_AjouterChampFichier(numerosession, "c:\bytesazertyqsdfgh").ToString + Chr(10))
        My.Computer.FileSystem.DeleteFile("c:\bytesazertyqsdfgh")
        RichTextBox1.AppendText(" CHAMP 2 " + SGD_AjouterChamp(numerosession, "0230").ToString + Chr(10))
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Or OpenFileDialog1.CheckFileExists = False Then
            SGD_LibererSession(numerosession)
            Exit Sub
        End If
        'RichTextBox1.AppendText(" CHAMP 3 " + SGD_AjouterChampFichier(numerosession, "C:\DOSSIER_SV\simtla-2.30\taph0230.mnt").ToString + Chr(10))
        RichTextBox1.AppendText(" CHAMP 3 " + SGD_AjouterChampFichier(numerosession, OpenFileDialog1.FileName).ToString + Chr(10))
        Dim out As Integer = SIS_ChargerAppli(NomRessourceLecteur, numerosession).ToString
        RichTextBox1.AppendText(" CHARGER APPLI " + out.ToString + Chr(10))
        If out = 61587 Then
            RichTextBox1.AppendText("Version du logiciel déjà hébergée ou obsolète" + Chr(10))
        End If
        RichTextBox1.AppendText("LIBERER SESSION " + SGD_LibererSession(numerosession).ToString + Chr(10))


    End Sub
End Class