Imports System.Runtime.InteropServices
Imports System.Text

Public Class jtravaille

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Me.Close()
    End Sub

    Public Class INIHelper
        <DllImport("kernel32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function GetPrivateProfileString(ByVal section As String,
                                                    ByVal key As String,
                                                    ByVal defaultValue As String,
                                                    ByVal returnedString As StringBuilder,
                                                    ByVal size As Integer,
                                                    ByVal filePath As String) As Integer
        End Function

        Public Shared Function lireINI(ByVal section As String, Form1.ComboBox8.Text & ByVal key As String) As String
            Dim buffer As New StringBuilder(255)
            Dim path As String = "./badgeuse.ini"
            GetPrivateProfileString(section, key, "", buffer, 255, path)
            Return buffer.ToString()
        End Function
    End Class

    Private Sub jtravaille_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 1) On vide la combo au cas où
        ComboBox1.Items.Clear()

        ' 2) On copie tous les items depuis Form1.ComboBox2
        For Each perso As Object In Form1.ComboBox2.Items
            ComboBox1.Items.Add(perso)
        Next

        ' 3) On recherche l’index sélectionné dans Form1 et on l’applique
        If Form1.ComboBox2.SelectedIndex >= 0 Then
            ComboBox1.SelectedIndex = Form1.ComboBox2.SelectedIndex
        End If

        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()


        lm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_lm")
        If lm = "true" Then
            CheckBox1.Checked = True
        Else
            CheckBox1.Checked = False
        End If
        lam = INIHelper.lireINI("jtravaille", nomPersonne & "_lam")
        If lam = "true" Then
            CheckBox2.Checked = True
        Else
            CheckBox2.Checked = False
        End If
        mm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_mm")
        If mm = "true" Then
            CheckBox3.Checked = True
        Else
            CheckBox3.Checked = False
        End If
        mam = INIHelper.lireINI("jtravaille", nomPersonne & "_mam")
        If mam = "true" Then
            CheckBox4.Checked = True
        Else
            CheckBox4.Checked = False
        End If
        mem = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_mem")
        If mem = "true" Then
            CheckBox5.Checked = True
        Else
            CheckBox5.Checked = False
        End If
        meam = INIHelper.lireINI("jtravaille", nomPersonne & "_meam")
        If meam = "true" Then
            CheckBox6.Checked = True
        Else
            CheckBox6.Checked = False
        End If
        jm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_jm")
        If jm = "true" Then
            CheckBox7.Checked = True
        Else
            CheckBox7.Checked = False
        End If
        jam = INIHelper.lireINI("jtravaille", nomPersonne & "_jam")
        If jam = "true" Then
            CheckBox8.Checked = True
        Else
            CheckBox8.Checked = False
        End If
        vm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_vm")
        If vm = "true" Then
            CheckBox9.Checked = True
        Else
            CheckBox9.Checked = False
        End If
        vam = INIHelper.lireINI("jtravaille", nomPersonne & "_vam")
        If vam = "true" Then
            CheckBox10.Checked = True
        Else
            CheckBox10.Checked = False
        End If
        sm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_sm")
        If sm = "true" Then
            CheckBox11.Checked = True
        Else
            CheckBox11.Checked = False
        End If
        sam = INIHelper.lireINI("jtravaille", nomPersonne & "_sam")
        If sam = "true" Then
            CheckBox12.Checked = True
        Else
            CheckBox12.Checked = False
        End If
        dm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_dm")
        If dm = "true" Then
            CheckBox13.Checked = True
        Else
            CheckBox13.Checked = False
        End If
        dam = INIHelper.lireINI("jtravaille", nomPersonne & "_dam")
        If dam = "true" Then
            CheckBox14.Checked = True
        Else
            CheckBox14.Checked = False
        End If
    End Sub


    Private Sub Button1_LostFocus(sender As Object, e As EventArgs) Handles Button1.LostFocus
        Form1.rafraichir()
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "lm"


        Dim valeurStr As String = If(CheckBox1.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "lam"


        Dim valeurStr As String = If(CheckBox2.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "mm"


        Dim valeurStr As String = If(CheckBox3.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "mam"


        Dim valeurStr As String = If(CheckBox4.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox5_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "mem"


        Dim valeurStr As String = If(CheckBox5.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox6_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox6.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "meam"


        Dim valeurStr As String = If(CheckBox6.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox7_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox7.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "jm"


        Dim valeurStr As String = If(CheckBox7.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox8_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox8.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "jam"


        Dim valeurStr As String = If(CheckBox8.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox9_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox9.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "vm"


        Dim valeurStr As String = If(CheckBox9.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox10_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox10.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "vam"


        Dim valeurStr As String = If(CheckBox10.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox11_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox11.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "sm"


        Dim valeurStr As String = If(CheckBox11.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox12_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox12.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "sam"


        Dim valeurStr As String = If(CheckBox12.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox13_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox13.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "dm"


        Dim valeurStr As String = If(CheckBox13.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub CheckBox14_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox14.CheckedChanged
        Dim nomPersonne As String = ComboBox1.SelectedItem.ToString()
        Dim jour As String = "dam"


        Dim valeurStr As String = If(CheckBox14.Checked, "true", "false")
        ecrireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_" & jour, valeurStr)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged


        Dim nomPersonne = ComboBox1.SelectedItem?.ToString()
        If String.IsNullOrEmpty(nomPersonne) Then Exit Sub

        ' lit "true"/"False" et coche/décoche
        CheckBox1.Checked = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_lm") _
                                 .Trim() _
                                 .Equals("true", StringComparison.OrdinalIgnoreCase)



        lm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_lm")
        If lm = "true" Then
            CheckBox1.Checked = True
        Else
            CheckBox1.Checked = False
        End If
        lam = INIHelper.lireINI("jtravaille", nomPersonne & "_lam")
        If lam = "true" Then
            CheckBox2.Checked = True
        Else
            CheckBox2.Checked = False
        End If
        mm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_mm")
        If mm = "true" Then
            CheckBox3.Checked = True
        Else
            CheckBox3.Checked = False
        End If
        mam = INIHelper.lireINI("jtravaille", nomPersonne & "_mam")
        If mam = "true" Then
            CheckBox4.Checked = True
        Else
            CheckBox4.Checked = False
        End If
        mem = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_mem")
        If mem = "true" Then
            CheckBox5.Checked = True
        Else
            CheckBox5.Checked = False
        End If
        meam = INIHelper.lireINI("jtravaille", nomPersonne & "_meam")
        If meam = "true" Then
            CheckBox6.Checked = True
        Else
            CheckBox6.Checked = False
        End If
        jm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_jm")
        If jm = "true" Then
            CheckBox7.Checked = True
        Else
            CheckBox7.Checked = False
        End If
        jam = INIHelper.lireINI("jtravaille", nomPersonne & "_jam")
        If jam = "true" Then
            CheckBox8.Checked = True
        Else
            CheckBox8.Checked = False
        End If
        vm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_vm")
        If vm = "true" Then
            CheckBox9.Checked = True
        Else
            CheckBox9.Checked = False
        End If
        vam = INIHelper.lireINI("jtravaille", nomPersonne & "_vam")
        If vam = "true" Then
            CheckBox10.Checked = True
        Else
            CheckBox10.Checked = False
        End If
        sm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_sm")
        If sm = "true" Then
            CheckBox11.Checked = True
        Else
            CheckBox11.Checked = False
        End If
        sam = INIHelper.lireINI("jtravaille", nomPersonne & "_sam")
        If sam = "true" Then
            CheckBox12.Checked = True
        Else
            CheckBox12.Checked = False
        End If
        dm = INIHelper.lireINI("jtravaille", Form1.ComboBox8.Text & nomPersonne & "_dm")
        If dm = "true" Then
            CheckBox13.Checked = True
        Else
            CheckBox13.Checked = False
        End If
        dam = INIHelper.lireINI("jtravaille", nomPersonne & "_dam")
        If dam = "true" Then
            CheckBox14.Checked = True
        Else
            CheckBox14.Checked = False
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim result As DialogResult = MessageBox.Show(
        "Voulez-vous vraiment poursuivre ?",       ' Le message
        "Confirmation",                            ' Le titre de la fenêtre
        MessageBoxButtons.OKCancel,               ' Les boutons OK et Annuler
        MessageBoxIcon.Question                   ' L’icône (optionnel)
    )

        ' Si l’utilisateur choisit OK...
        If result = DialogResult.OK Then
            If ComboBox1.Items.Count < 2 Then
                MessageBox.Show("Il faut au moins deux personnes dans ComboBox1.", "Attention",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return
            End If

            ' Nom du fichier INI (chemin relatif)
            Dim iniFile As String = ".\badgeuse.ini"

            ' 1) Récupère la 1ʳᵉ personne
            Dim firstPerson As String = ComboBox1.Items(0).ToString()

            ' 2) Liste des clés à traiter
            Dim keys() As String = {
        "lm", "lam", "mm", "mam", "mem", "meam",
        "jm", "jam", "vm", "vam", "sm", "sam", "dm", "dam"
    }

            ' 3) Lit toutes les valeurs pour la première personne
            Dim valeurs(keys.Length - 1) As String
            For i As Integer = 0 To keys.Length - 1
                Dim fullKeySrc As String = $"{firstPerson}_{keys(i)}"
                valeurs(i) = lireINI("jtravaille", Form1.ComboBox8.Text & fullKeySrc)
                Debug.WriteLine($"Lecture [{firstPerson}] {fullKeySrc} = '{valeurs(i)}'")
            Next

            ' 4) Écrit ces mêmes valeurs pour chaque autre personne
            For idx As Integer = 1 To ComboBox1.Items.Count - 1
                Dim pers As String = ComboBox1.Items(idx).ToString()
                For i As Integer = 0 To keys.Length - 1
                    Dim fullKeyDst As String = $"{pers}_{keys(i)}"
                    ecrireINI("jtravaille", fullKeyDst, valeurs(i))
                    Debug.WriteLine($"Écriture [{pers}] {fullKeyDst} ← '{valeurs(i)}'")
                Next
            Next
            MessageBox.Show("Tous les jours travaillés sont changés selon ceux de la première personne", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ' TODO : remplacez cette ligne par votre code métier

        End If
    End Sub
End Class