Imports IntraSell_DLL
Imports IntraSell_Net_Base
Public Class Login
    Inherits AbstractForm

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Property LogInUsername As String

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub

    Private Sub OK_Click(sender As Object, e As System.EventArgs) Handles OK.Click
        Try
            Username = UsernameTextBox.Text
            saveLastUsername()
            If LoginUser(UsernameTextBox.Text, PasswordTextBox.Text) Then
                Ziffernblock1.ResetAmount()
                PasswordTextBox.Text = ""
                Close()
                Dispose()
            Else
                UsernameTextBox.ForeColor = Color.IndianRed
                PasswordTextBox.ForeColor = UsernameTextBox.ForeColor
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub Cancel_Click(sender As Object, e As System.EventArgs) Handles Cancel.Click
        Try
            Close()
            Application.Exit()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Supports commadn line args username and password
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Sub Login_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Try
                'command line arguments support e.g. username=ej password=ej
                If Environment.CommandLine.Contains("username") And Environment.CommandLine.Contains("password") Then
                    Dim username As String = Environment.GetCommandLineArgs(1).Split("=")(1)
                    Dim password As String = Environment.GetCommandLineArgs(2).Split("=")(1)
                    ModuleGlobals.Username = username
                    UsernameTextBox.Text = username
                    PasswordTextBox.Text = password
                    OK_Click(Nothing, Nothing)
                End If
            Catch ex As Exception
                WriteLog("Startparameters sind nicht wie erwartet. Beispiel ""username=ej password=ej""")
            End Try

            If Not NeedsLogin Then
                Close()
            End If

            'Load last User name 
            'load zuletzt angemeldeter benutzer
            ReadLastUsername()
            UsernameTextBox.Text = Username
            If LogInUsername & "" <> "" Then
                UsernameTextBox.Text = LogInUsername
            End If

            AddHandler Ziffernblock1.AmountChanged, AddressOf ZiffernblockOK

            UcToken1.RegisterKeyDown(UsernameTextBox)
            UcToken1.RegisterKeyDown(PasswordTextBox)

            ParentBindingNavigator.Visible = False
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub UsernameTextBox_Enter(sender As Object, e As EventArgs) Handles UsernameTextBox.Enter
        Ziffernblock1.Enabled = True
        Ziffernblock1.ResetAmount()
        ClearAllHandler()
        AddHandler Ziffernblock1.AmountChanging, AddressOf UsernameChanged
        AddHandler Ziffernblock1.AmountCleared, AddressOf UsernameCleared

    End Sub

    Private Sub PasswordTextBox_Enter(sender As Object, e As EventArgs) Handles PasswordTextBox.Enter
        Ziffernblock1.Enabled = True
        Ziffernblock1.ResetAmount()
        ClearAllHandler()
        AddHandler Ziffernblock1.AmountChanging, AddressOf PasswordChanged
        AddHandler Ziffernblock1.AmountCleared, AddressOf PasswordCleared

    End Sub

    Private Sub UsernameChanged(amount As Decimal)
        UsernameTextBox.Text = CType(amount, String)
    End Sub

    Private Sub UsernameCleared()
        UsernameTextBox.Text = ""
    End Sub

    Private Sub PasswordChanged(amount As Decimal)
        PasswordTextBox.Text = CType(amount, String)
    End Sub

    Private Sub PasswordCleared()
        PasswordTextBox.Text = ""
    End Sub

    Private Sub ZiffernblockOK()
        If Ziffernblock1.Amount <> "" Then
            OK_Click(Nothing, Nothing)
        End If
    End Sub

    Shared Sub Logout()
        MitarbeiterID = -1
        Username = Nothing
        LastLogin = Nothing
    End Sub

    Private Sub ClearAllHandler()
        RemoveHandler Ziffernblock1.AmountChanging, AddressOf PasswordChanged
        RemoveHandler Ziffernblock1.AmountCleared, AddressOf PasswordCleared
        RemoveHandler Ziffernblock1.AmountChanging, AddressOf UsernameChanged
        RemoveHandler Ziffernblock1.AmountCleared, AddressOf UsernameCleared
    End Sub

    Private Sub PasswordTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles PasswordTextBox.Validating
        Debug.Print("PasswordTextBox_Validating:" & PasswordTextBox.Text)
        'LoginMitToken(PasswordTextBox.Text)
    End Sub

    Sub LoginMitToken(token As String)
        Try
            If LoginUserMitToken(token) Then
                Ziffernblock1.ResetAmount()
                PasswordTextBox.Text = ""
                Close()
                Dispose()
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub UcToken1_TokenScanned(ByRef token As String) Handles UcToken1.TokenScanned
        LoginMitToken(token)
    End Sub
End Class
