Imports System.IO

Public Class frmLogin
    Public ConnStrInfo As String
    Public ConnStr As String
    Public ConnStrMitarbeiter As String
    Public USRNAME As String
    '  Public USRBER As Integer
    Public USRID As Integer
    Public USRFirma As String

    Private ADMIN As New cOptionenDAL


    ' Private Version As String = "0.9.0"



    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        ' normaler Login
        VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = False
        If ADMIN.checkLogin(txtUser.Text, txtPwd.Text) Then
            loginOK()
        Else
            lblAlertTxt.Visible = True
        End If
    End Sub
    Private Sub loginOK()
        '  USRNAME = txtUser.Text
        '      USRBER = ADMIN.USERBER
        USRNAME = ADMIN.USRNAME
        USRID = ADMIN.USRID
        USRFirma = ADMIN.USRFirma

        frmMain.Show()
        Me.Hide()
    End Sub



    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles Me.Load

        ' UPDATEUpdater
        If updateUpdater() Then
            MsgBox("Der Admin-Installer wurde aktualisiert. Es muss ein Update durchgeführt werden.")
            UpdateMe(True)
        End If



        Dim parameter() As String = Environment.GetCommandLineArgs().ToArray
        If (parameter.Count - 1) >= 1 Then 'Höher als 1 weil der index 0 der Pfad zum programm ist
            For i = 1 To parameter.Count - 1
                Dim pf As New cProgramFunctions
                Dim kw As Integer = pf.DateToWeek(Now.AddDays(-1)).Substring(4)
                Dim year As Integer = Now.AddDays(-1).Year
                Dim STD_LIST = cDienstMitarbAbweichendeWochenstunden.GET_STD_LIST(frmDienstplanVariabel.CalendarWeek(kw, year))

                If parameter(i) = "initDP_ATILLA" Then pf.KWAbschluss(STD_LIST, "ATILLA", year, kw, pf.getSchicht(kw, year)) : Application.Exit()
                If parameter(i) = "initDP_SUB" Then pf.KWAbschluss(STD_LIST, "SUB", year, kw, pf.getSchicht(kw, year)) : Application.Exit()
                If parameter(i) = "initDP_WAI" Then pf.KWAbschluss(STD_LIST, "WAI", year, kw, pf.getSchicht(kw, year)) : Application.Exit()
            Next
        End If


        ' Dim tmp As String
        Me.Show()
        txtUser.Focus()
        lblAlertTxt.Visible = False
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        lblVersion.Text = lblVersion.Text & " " & Application.ProductVersion 'My.Resources.Version
        lblVersion.ContextMenuStrip = ctxUpdate

        'auf UPDATES prüfen:
        UpdateMe()

    End Sub

    Sub UpdateMe(Optional doUpdateAnyway = False)

        If doUpdateAnyway OrElse CInt(ADMIN.getAktuelleVersion().Replace(".", "")) > CInt(Application.ProductVersion.Replace(".", "")) Then
            Me.Enabled = False
            If doUpdateAnyway OrElse vbYes = MsgBox("Es ist eine neue Programm-Version verfügbar. Das Programm muss aktualisiert werden, bevor Sie es weiter verwenden können. " & vbNewLine & vbNewLine & "Update jetzt durchführen?", CType(vbQuestion + vbYesNo, MsgBoxStyle), "Update verfügbar") Then
                UpdateADMIN()
            Else
                Application.Exit()
            End If
        End If
    End Sub
    Private Sub txtPwd_KeyDown(sender As Object, e As KeyEventArgs) Handles txtUser.KeyDown, txtPwd.KeyDown
        If (e.KeyCode = Keys.Return) Then
            btnLogin.PerformClick() : e.SuppressKeyPress = True
        ElseIf (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
    Public Sub UpdateADMIN()
        If Not System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "ADMINUPDATER.exe") Then
            MsgBox("ERROR_UPDATE: Update konnte nicht fortgesetzt werden." & vbNewLine & vbNewLine & "Update-Datei existiert nicht.", vbCritical)
            Application.Exit()
        Else
            Process.Start("ADMINUPDATER.exe")
            Application.Exit()
        End If
    End Sub

    Private Sub frmLogin_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        'ADMIN-Login über Windows Benutzer
        ' MsgBox(Environment.UserDomainName & "/" & Environment.UserName)
        If Environment.UserDomainName & "/" & Environment.UserName = "VERAGOST/developer1" Then
            If vbYes = MsgBox("Autologin?", vbYesNoCancel) Then
                ' If ADMIN.getAdminLogin("ADMIN") Then loginOK()
                txtUser.Text = "ADMIN"
                txtPwd.Text = "verag#2"
                btnLogin.PerformClick()
            End If

        End If
    End Sub
    Private Sub lblVersion_Click(sender As Object, e As MouseEventArgs) Handles lblVersion.MouseClick, lblVersion.Click
        ctxUpdate.Show(lblVersion, e.Location)
    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles UpdateToolStripMenuItem.Click
        UpdateADMIN()
    End Sub

    Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
        Me.Close()
    End Sub



    Shared Function updateUpdater() As Boolean
        Try
            Dim aktVersion = My.Resources.UpdaterVersion ' Aktuelle Updater Version
            Dim F = "F:\Programme\ADMIN_install\"

            Select Case getStandort()
                '  Case "ATILLA"
                Case "IMEX" : F = "\\192.168.1.20\Daten\VERAG Programme\ADMIN_install\"
            End Select
            Dim doUpdaterUpdate = False
            If IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "\ADMINUPDATER_Version.txt") Then 'Updater muss auch existieren, sonst wird er auch beim Developer Rechner eingefügt...
                Dim fs As New FileStream(AppDomain.CurrentDomain.BaseDirectory & "\ADMINUPDATER_Version.txt", FileMode.Open, FileAccess.Read)
                Dim strmReader As New StreamReader(fs)
                Dim Version = strmReader.ReadLine
                If IsNumeric(Version.Replace(".", "")) Then
                    If Version.Replace(".", "") < aktVersion.Replace(".", "") Then
                        doUpdaterUpdate = True
                    End If
                End If
                fs.Close()
                strmReader.Close()
            Else
                doUpdaterUpdate = True
            End If
            If Not doUpdaterUpdate Then Return False

            If IO.Directory.Exists(F) And IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "\ADMINUPDATER.exe") Then 'Updater muss auch existieren, sonst wird er auch beim Developer Rechner eingefügt...
                For Each file As String In IO.Directory.GetFiles(F) ' Ermittelt alle Dateien des Ordners 
                    IO.File.Copy(file, AppDomain.CurrentDomain.BaseDirectory & cut_file(file), True) ' Kopiert die Dateien Next 
                Next
                For Each file As String In IO.Directory.GetDirectories(F) ' Ermittelt alle Unterordner des Ordners 
                    My.Computer.FileSystem.CopyDirectory(file, AppDomain.CurrentDomain.BaseDirectory & cut_file(file), True)
                Next
                Return True
            End If
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    Shared Function getStandort()
        Dim pfadDatei As String = System.AppDomain.CurrentDomain.BaseDirectory & "\Standort.txt"
        getStandort = "VERAG"

        If File.Exists(pfadDatei) Then
            Dim fs As New FileStream(pfadDatei, FileMode.Open, FileAccess.Read)
            Dim strmReader As New StreamReader(fs)
            getStandort = strmReader.ReadLine
            fs.Dispose()
            strmReader.Dispose()
        End If
    End Function


    Shared Function cut_file(ByVal file As String) As String ' Funktion zum Entfernen der Backslashs / Ordner While file.Contains("\") file = file.Remove(0, 1) End While Return file End Function 
        While file.Contains("\")
            file = file.Remove(0, 1)
        End While
        Return file
    End Function

End Class