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


Public Class Technique_MAIN

    'lire_session
    'Public Sub LireSession_In_VersFichier(ByVal NuméroSession As UInt16, ByVal CheminFichier As String, ByVal ValeurChamp As String, ByVal NuméroGroupe As String)
    '    Using sw As New StreamWriter(CheminFichier, True)
    '        If ValeurChamp = "" And NuméroGroupe <> "" Then
    '            sw.WriteLine()
    '            sw.WriteLine("Session " + NuméroSession.ToString + " / Groupe " + NuméroGroupe)
    '        Else
    '            sw.WriteLine(ValeurChamp)
    '        End If
    '    End Using
    'End Sub

    Public Sub LireSession_Out_VersFichier2(ByVal NuméroSession As UInt16, ByVal CheminFichier As String)

        Dim rep As String = Mid(CheminFichier, 1, InStrRev(CheminFichier, "\") - 1)
        If My.Computer.FileSystem.DirectoryExists(rep) = False Then
            My.Computer.FileSystem.CreateDirectory(rep)
        End If

        Using sw As New StreamWriter(CheminFichier)

            Dim IdGroupeSuivant As UInt16
            Dim OccurrenceGroupeSuivant As UInt16
            Dim IdGroupeCourant As UInt16 = 0
            Dim OccurrenceGroupeCourant As UInt16 = 0
            While SGD_LireGroupeSuivant(NuméroSession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) = 0
                sw.WriteLine("Groupe:" + IdGroupeSuivant.ToString + "    Occurrence:" + OccurrenceGroupeSuivant.ToString)
                IdGroupeCourant = IdGroupeSuivant
                OccurrenceGroupeCourant = OccurrenceGroupeSuivant
                Dim i As UInt16 = 1


                While SGD_LireChampFichier(NuméroSession, IdGroupeCourant, OccurrenceGroupeCourant, i, "temp") = 0
                    Using sr As New StreamReader("temp")
                        sw.WriteLine("   Champ " + i.ToString + ":" + sr.ReadToEnd)
                    End Using
                    i = i + 1
                End While
            End While
        End Using
        Try
            My.Computer.FileSystem.DeleteFile("temp")
        Catch ex As Exception
            MsgBox("LireSession_Out_VersFichier:" + Chr(10) + ex.ToString)
        End Try

    End Sub

    Private Sub lire_session_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lire_session.Click
        Me.UseWaitCursor = True
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim numerosession As UInt16 = Val(TextBoxNumeroSession.Text)
        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

 

    'Clear
    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
        RichTextBox1.Clear()
    End Sub

    'LireGroupeSuivant
    Private Sub LireGroupeSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LireGroupeSuivant.Click
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim numerosession As UInt16 = Val(TextBoxNumeroSession.Text)
        Dim IdGroupeCourant As UInt16 = Val(TextBoxIdGroupe.Text)
        Dim OccurrenceGroupeCourant As UInt16 = Val(TextBoxOccurrenceGroupe.Text)
        RichTextBox1.AppendText((SGD_LireGroupeSuivant(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) Mod 65536).ToString + Chr(10))
        RichTextBox1.AppendText("IdGroupeSuivant:" + IdGroupeSuivant.ToString + "    OccurrenceGroupeSuivant:" + OccurrenceGroupeSuivant.ToString + Chr(10))
    End Sub

    'SIS_InitTrace
    Private Sub SIS_InitTraceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SIS_InitTraceButton.Click
        RichTextBox1.AppendText(SIS_InitTrace(TextBoxNomFichier.Text, ComboBoxModeOuverture.Text, ComboBoxModule.Text, ComboBoxNiveau.Text).ToString)
    End Sub

    'LireChamp
    Private Sub LireChamp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LireChamp.Click
        ValeurChamp = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        RichTextBox1.AppendText(SGD_LireChamp(TextBoxNumeroSession.Text, TextBoxIdGroupe.Text, TextBoxOccurrenceGroupe.Text, TextBoxNumeroChamp.Text, ValeurChamp, 10000).ToString + Chr(10))
        RichTextBox1.AppendText(ValeurChamp + Chr(10))
    End Sub

    Private Sub STS_InitTraceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles STS_InitTraceButton.Click
        RichTextBox1.AppendText(STS_InitTrace(TextBoxNomFichier.Text, ComboBoxModeOuverture.Text, ComboBoxModule.Text, ComboBoxNiveau.Text).ToString)
    End Sub


    Private Sub ButtonKillGalss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonKillGalss.Click
        killgalss()
    End Sub

    Private Sub Button_Histo_SMTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Histo_SMTP.Click
        Histo_SMTP.Visible = True
    End Sub

    Private Sub Button_Histo_ARL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Histo_ARL.Click
        Histo_ARL.Visible = True
    End Sub


    Private Sub Lire_Champ_Bin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lire_Champ_Bin.Click
        Dim mybyte As Byte
        SGD_LireChampBin(1, 2, 1, 1, mybyte, 1)
        RichTextBox1.AppendText(mybyte.ToString)


    End Sub


End Class