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

Public Class Password


    Private Sub MaskedTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles password_MaskedTextBox.KeyDown
        If e.KeyCode = Keys.Enter Then
            password_validated = check_password(user_TextBox.Text, password_MaskedTextBox.Text)
            If password_validated = True Then
                If Id_User_locked_CheckBox.Checked = True Then
                    current_User.Locked = True
                    MENU_PRINCIPAL.user_Button.Visible = True
                    MENU_PRINCIPAL.user_Button_unlock.Visible = True
                    MENU_PRINCIPAL.user_Button.Text = user_TextBox.Text
                    If Not current_User.image Is Nothing Then
                        MENU_PRINCIPAL.user_Button.BackgroundImage = current_User.image
                        MENU_PRINCIPAL.user_Button.Font = New Font("Century Gothic", 10)
                        MENU_PRINCIPAL.user_Button.TextAlign = ContentAlignment.BottomRight
                        MENU_PRINCIPAL.user_Button.ForeColor = Color.Black
                    Else
                        MENU_PRINCIPAL.user_Button.BackgroundImage = Nothing
                        MENU_PRINCIPAL.user_Button.Font = New Font("Century Gothic", 15)
                        MENU_PRINCIPAL.user_Button.TextAlign = ContentAlignment.MiddleCenter
                        MENU_PRINCIPAL.user_Button.ForeColor = Color.White

                    End If
                Else
                    current_User.Locked = False
                End If
                Mes_Couleurs.Load_Config()
                Me.Close()
            End If
        Else
            Label1.Visible = False
        End If
    End Sub

    Public Function check_password(ByVal user As String, ByVal password As String) As Boolean
        Dim users = USERS_Select.UsersTableAdapter.GetDataBy_login(user)
        If users.Rows.Count = 0 Then
            Label1.Text = "invalide"
            Label1.Visible = True
            Return False
        End If
        If hash_sha256(password) = (users.Item(0).password) _
            Or (password = "" And users.Item(0).password = "") Then
            current_User.ID = user
            Try
                current_User.image = System.Drawing.Image.FromFile(Config.LocalNetwork_ServerName & "\images\users\" & users.Item(0).picture)
            Catch ex As Exception
                current_User.image = Nothing
            End Try
            current_User.comptage_boite = users.Item(0).comptage_boites

            If Changer_mdp_CheckBox.Checked = True Then
                Dim mdp1 As String = ""
                Dim mdp2 As String = ""
                While mdp1 <> mdp2 Or mdp1 = ""
                    mdp1 = InputBox("Entrez le nouveau mot de passe", "Changement mot de passe")
                    mdp2 = InputBox("Confirmez le nouveau mot de passe", "Controle mot de passe")
                End While
                USERS_Select.UsersTableAdapter.Update_password(hash_sha256(mdp1), user_TextBox.Text)
            End If

            Messagerie_interne_affiche.Check_UNREAD_messages(current_User.ID)

            Return True
        Else
            current_User.Clear()
            Label1.Text = "invalide"
            Label1.Visible = True
            Return False
        End If

    End Function

    Private Sub Password_Load(sender As Object, e As EventArgs) Handles Me.Load
        If current_User.Locked = True And user_TextBox.Text = current_User.ID Then
            password_validated = True
            Me.Close()
        End If
        password_validated = False
        password_MaskedTextBox.Text = ""
        Changer_mdp_CheckBox.Checked = False
        Id_User_locked_CheckBox.Checked = False
        Me.Show()
        password_MaskedTextBox.Focus()
    End Sub

    Public Function hash_sha256(ByVal text_hash As String) As String
        'On déclare la variable servant à crypter
        Dim sha256 As New SHA256Managed
        Dim TexteEnBit() As Byte
        Dim TexteHache() As Byte = Nothing

        ' Récupération de la valeur en bit du texte à hacher
        TexteEnBit = System.Text.Encoding.UTF8.GetBytes(text_hash)

        ' Hachage
        TexteHache = sha256.ComputeHash(TexteEnBit)
        'Libération des ressources
        sha256.Clear()

        ' Renvoi
        hash_sha256 = ByteArrayToString(TexteHache)
    End Function

    'Fonction pour convertir des bits en string
    Function ByteArrayToString(ByVal arrInput() As Byte) As String
        Dim i As Integer
        Dim sOutput As New StringBuilder(arrInput.Length)
        For i = 0 To arrInput.Length - 1
            sOutput.Append(arrInput(i).ToString("X2"))
        Next
        Return sOutput.ToString().ToLower
    End Function

 
    Private Sub password_MaskedTextBox_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles password_MaskedTextBox.MaskInputRejected

    End Sub
End Class