Imports System.IO

Public Class frmProgDetails
    Private PROG As New cProgramme
    Public codename As String = ""

    Dim sicherheitsfrageUeberspringen = False


    Private Sub frmProgDetails_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        'Dim usrCntlProgramme As New usrCntlProgramme
        'usrCntlProgramme.init()
    End Sub

    Private Sub frmProgDetails_Load(sender As Object, e As EventArgs) Handles Me.Load
        init()
    End Sub
    Public Sub showData()
        If codename = "ADMIN" Then picIcon.Image = My.Resources.admin
        If codename = "UID" Then picIcon.Image = My.Resources.uid_ico
        If codename = "DISPO" Then picIcon.Image = My.Resources.dispo_ico
        If codename = "PARKZEIT" Then picIcon.Image = My.Resources.parkzeit
        If codename = "MONITORING" Then picIcon.Image = My.Resources.admin
        If codename = "ROUTINE_MANAGER" Then picIcon.Image = My.Resources.admin_monitoring
        If codename = "MAIL_SENDER" Then picIcon.Image = My.Resources.admin_monitoring
        If codename = "DOKU" Then picIcon.Image = My.Resources.doku_sym
        If codename = "RKSV_DE" Then picIcon.Image = My.Resources.admin_monitoring
        If codename = "VERAG_OCR" Then picIcon.Image = My.Resources.admin_monitoring
        If codename = "VERAGCallAssist" Then picIcon.Image = My.Resources.admin_monitoring



        lblTitel.Text = codename

        Dim daten As cProgramm = Nothing
        ' daten = New cProgramm
        daten = PROG.getProgrammByCodeName(codename)
        txtSourcePfad.Text = getSourcePath()


        txtDevPfad.Text = daten.prog_dev_path
        txtProdPfad.Text = daten.prog_prod_path
        txtInstallSourcePfad.Text = getSourceInstallPath() ' daten.prog_installer_source_path
        txtInstallDevPfad.Text = daten.prog_installer_dev_path
        txtInstallProdPfad.Text = daten.prog_installer_prod_path
        txtVersion.Text = daten.prog_aktuelleVersion
        txtInfo.Text = daten.prog_info

        txtProdPfadAtilla.Text = daten.prog_prod_path_atilla
        txtInstallProdPfadAtilla.Text = daten.prog_installer_prod_path_atilla

        txtProdPfadImex.Text = daten.prog_prod_path_imex
        txtInstallProdPfadimex.Text = daten.prog_installer_prod_path_imex

        txtProdPfadSBG.Text = daten.prog_prod_path_SBG
        txtInstallProdPfadSBG.Text = daten.prog_installer_prod_path_SBG

        txtProdPfadWAI.Text = daten.prog_prod_path_WAI
        txtInstallProdPfadWAI.Text = daten.prog_installer_prod_path_WAI

        txtProdPfadNKD.Text = daten.prog_prod_path_NKD
        txtInstallProdPfadNKD.Text = daten.prog_installer_prod_path_NKD

        txtProdPfadNEU.Text = daten.prog_prod_path_NEU
        txtInstallProdPfadNEU.Text = daten.prog_installer_prod_path_NEU

        txtProdPfadUNI.Text = daten.prog_prod_path_UNI
        txtInstallProdPfadUNI.Text = daten.prog_installer_prod_path_UNI

        txtProdPfadAMB.Text = daten.prog_prod_path_AMB
        txtInstallProdPfadAMB.Text = daten.prog_installer_prod_path_AMB

        txtProdPfadBackup.Text = daten.prog_prod_path_Backup

    End Sub

    Function getSourcePath() As String
        'PROG_initPath_AVISO 
        Return VERAG_PROG_ALLGEMEIN.cMitarbeiter.GetUserParam("PROG_initPath_" & codename.Replace("AVISO_NEW", "AVISO"),, "")
    End Function
    Function getSourceInstallPath() As String
        'PROG_initInstallPath_AVISO 
        Return VERAG_PROG_ALLGEMEIN.cMitarbeiter.GetUserParam("PROG_initInstallPath_" & codename.Replace("AVISO_NEW", "AVISO"),, "")
    End Function
    Private Sub btnVersionAkt_Click(sender As Object, e As EventArgs) Handles btnVersionAkt.Click
        PROG.setProgrammVersion(codename, txtVersion.Text)
        init()
    End Sub

    Private Sub btnSourceAkt_Click(sender As Object, e As EventArgs) Handles btnSourceAkt.Click
        PROG.setParam("prog_source_path", txtSourcePfad.Text, codename)
        init()
    End Sub
    Private Sub init()
        If codename = "NEW" Then
            btnInsert.Visible = True
            grpName.Visible = True
            btnDevAkt.Visible = False
            btnDevCopy.Visible = False
            btnInstallDevAkt.Visible = False
            btnInstallDevCopy.Visible = False
            btnInstallProdAkt.Visible = False
            btnInstallProdCopy.Visible = False
            btnInstallSourceAkt.Visible = False
            btnProdAkt.Visible = False
            btnProdCopy.Visible = False
            btnSourceAkt.Visible = False
            btnVersionAkt.Visible = False
            btnAktInfo.Visible = False
            lblTitelDetails.Visible = False
            lblTitel.Visible = False
            picIcon.Visible = False
        Else
            showData()
        End If

    End Sub

    Private Sub btnDevAkt_Click(sender As Object, e As EventArgs) Handles btnDevAkt.Click
        PROG.setParam("prog_dev_path", txtDevPfad.Text, codename)
        init()
    End Sub

    Private Sub btnProdAkt_Click(sender As Object, e As EventArgs) Handles btnProdAkt.Click
        PROG.setParam("prog_prod_path", txtProdPfad.Text, codename)
        init()
    End Sub

    Private Sub btnInstallSourceAkt_Click(sender As Object, e As EventArgs) Handles btnInstallSourceAkt.Click
        PROG.setParam("prog_installer_source_path", txtInstallSourcePfad.Text, codename)
        init()
    End Sub

    Private Sub btnInstallDevAkt_Click(sender As Object, e As EventArgs) Handles btnInstallDevAkt.Click
        PROG.setParam("prog_installer_dev_path", txtInstallDevPfad.Text, codename)
        init()
    End Sub

    Private Sub btnInstallProdAkt_Click(sender As Object, e As EventArgs) Handles btnInstallProdAkt.Click
        PROG.setParam("prog_installer_prod_path", txtInstallProdPfad.Text, codename)
        init()
    End Sub

    Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        Dim daten As New cProgramm
        daten.prog_codename = txtProgramCodeName.Text
        daten.prog_name = txtProgramName.Text
        daten.prog_aktuelleVersion = txtVersion.Text
        daten.prog_info = txtInfo.Text
        '   daten.prog_source_path = txtSourcePfad.Text
        daten.prog_dev_path = txtDevPfad.Text
        daten.prog_prod_path = txtProdPfad.Text
        '  daten.prog_installer_source_path = txtInstallSourcePfad.Text
        daten.prog_installer_dev_path = txtInstallDevPfad.Text
        daten.prog_installer_prod_path = txtInstallProdPfad.Text


        daten.prog_installer_prod_path_atilla = txtProdPfadAtilla.Text
        daten.prog_prod_path_atilla = txtInstallProdPfadAtilla.Text
        daten.prog_prod_path_imex = txtProdPfadImex.Text
        daten.prog_installer_prod_path_imex = txtInstallProdPfadimex.Text
        daten.prog_prod_path_SBG = txtProdPfadSBG.Text
        daten.prog_installer_prod_path_SBG = txtInstallProdPfadSBG.Text
        daten.prog_prod_path_WAI = txtProdPfadWAI.Text
        daten.prog_installer_prod_path_WAI = txtInstallProdPfadWAI.Text
        daten.prog_prod_path_NKD = txtProdPfadNKD.Text
        daten.prog_installer_prod_path_NKD = txtInstallProdPfadNKD.Text
        daten.prog_prod_path_NEU = txtProdPfadNEU.Text
        daten.prog_installer_prod_path_NEU = txtInstallProdPfadNEU.Text

        daten.prog_prod_path_UNI = txtProdPfadUNI.Text
        daten.prog_installer_prod_path_UNI = txtInstallProdPfadUNI.Text
        daten.prog_prod_path_AMB = txtProdPfadAMB.Text
        daten.prog_installer_prod_path_AMB = txtInstallProdPfadAMB.Text

        daten.prog_prod_path_Backup = txtProdPfadBackup.Text

        If PROG.insertProgramm(daten) Then Me.Close()

    End Sub

    Private Sub btnAktInfo_Click(sender As Object, e As EventArgs) Handles btnAktInfo.Click
        PROG.setParam("prog_info", txtInfo.Text, codename)
        init()
    End Sub

    Sub setBG(sender As Button, erg As Boolean)
        If erg Then
            sender.BackColor = Color.LightGreen
        Else
            sender.BackColor = Color.Red
        End If
    End Sub

    Dim PROG_FUNC As New cProgramFunctions

    Sub copyProgrammFiles(sender As Button, von As String, nach As String, cbx As Boolean, Optional standort As String = "", Optional LIST As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate) = Nothing)
        sender.BackColor = Color.LightGray
        Application.DoEvents()
        Me.Refresh()
        If von = "" Then sender.BackColor = Color.White : Exit Sub
        If nach = "" Then sender.BackColor = Color.White : Exit Sub
        If checkSicherheitsfrage(von, nach) Then
            setBG(sender, PROG_FUNC.copyProgram(von, nach, cbx, cbxIncrementalCopy.Checked))
        End If
        Application.DoEvents()
        Me.Refresh()

        If standort <> "" Then
            SaveLogFile(nach, standort)
        End If

    End Sub




    Function checkSicherheitsfrage(von, nach) As Boolean
        If sicherheitsfrageUeberspringen Then Return True
        If vbYes = MsgBox("Möchten Sie folgende Aktion ausführen?:" & vbNewLine & vbNewLine & " Kopiere alle Files von: " & von & vbNewLine & "Nach: " & nach, vbYesNo, "Aktion ausführen?") Then
            Return True
        End If
        Return False
    End Function
    Private Sub btnDevCopy_Click(sender As Object, e As EventArgs) Handles btnDevCopy.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtDevPfad.Text, cbxZip.Checked)
    End Sub

    Private Sub btnProdCopy_Click(sender As Object, e As EventArgs) Handles btnProdCopy.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfad.Text, cbxZip.Checked)

    End Sub

    Private Sub btnInstallDevCopy_Click(sender As Object, e As EventArgs) Handles btnInstallDevCopy.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallDevPfad.Text, cbxZip.Checked)

    End Sub

    Private Sub btnInstallProdCopy_Click(sender As Object, e As EventArgs) Handles btnInstallProdCopy.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfad.Text, cbxZip.Checked)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PROG.setParam("prog_prod_path_atilla", txtProdPfadAtilla.Text, codename)
        init()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        PROG.setParam("prog_installer_prod_path_atilla", txtInstallProdPfadAtilla.Text, codename)
        init()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadAtilla.Text, cbxZip.Checked, If(cbxStandorttxtErstellen.Checked, "ATILLA", ""))
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadAtilla.Text, cbxZip.Checked)

    End Sub


    Sub genTxtStandort(pfadAtilla)
        Try
            If Not File.Exists(pfadAtilla & "Standort.txt") Then
                File.Create(pfadAtilla & "Standort.txt")
            End If

            '   Dim fs As New FileStream(pfadAtilla & "Standort.txt", FileMode.CreateNew)
            ''   Dim sw As New StreamWriter(fs)
            '  sw.WriteLine("ATILLA")
            '  sw.Close()


            My.Computer.FileSystem.WriteAllText(pfadAtilla & "Standort.txt", "ATILLA", False)

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub

    Public Sub SaveLogFile(Pfad, TEXT)
        If Pfad.trim = "" Then Exit Sub
        Dim Dateiname As String = "\Standort.txt"

        Dim streami As FileStream
        If Directory.Exists(Pfad) = True Then
            streami = File.Create(Pfad & Dateiname)
        Else
            Directory.CreateDirectory(Pfad)
            streami = File.Create(Pfad & Dateiname)
        End If

        Dim cfile As StreamWriter = New StreamWriter(streami, System.Text.Encoding.Default)

        cfile.WriteLine(TEXT)
        cfile.Close()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        PROG.setParam("prog_prod_path_imex", txtProdPfadImex.Text, codename)
        init()
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadImex.Text, cbxZip.Checked, If(cbxStandorttxtErstellenImex.Checked, "IMEX", ""))

    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        PROG.setParam("prog_installer_prod_path_imex", txtInstallProdPfadimex.Text, codename)
        init()
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadimex.Text, cbxZip.Checked)

    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        PROG.setParam("prog_prod_path_sbg", txtProdPfadSBG.Text, codename)
        init()
    End Sub

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadSBG.Text, cbxZip.Checked, If(cbxStandorttxtErstellenSBG.Checked, "SBG", ""))

    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        PROG.setParam("prog_installer_prod_path_sbg", txtInstallProdPfadSBG.Text, codename)
        init()
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadSBG.Text, cbxZip.Checked)

    End Sub
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        If vbYes = MsgBox("Möchten Sie die Programmdaten an alle NL verteilen?", vbYesNo, "Aktion ausführen?") Then
            sicherheitsfrageUeberspringen = True
            btnDevCopy.PerformClick()
            btnProdCopy.PerformClick()
            Button2.PerformClick()
            Button6.PerformClick()
            Button12.PerformClick()
            Button16.PerformClick()
            Button18.PerformClick()
            Button20.PerformClick()
            Button30.PerformClick()
            Button28.PerformClick()
        End If
        sicherheitsfrageUeberspringen = False
    End Sub


    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        If vbYes = MsgBox("Möchten Sie die Programmdaten an alle NL verteilen?", vbYesNo, "Aktion ausführen?") Then
            sicherheitsfrageUeberspringen = True
            btnInstallDevCopy.PerformClick()
            btnInstallProdCopy.PerformClick()
            Button4.PerformClick()
            Button8.PerformClick()
            Button10.PerformClick()
            Button22.PerformClick()
            Button24.PerformClick()
            Button26.PerformClick()
            Button32.PerformClick()
            Button34.PerformClick()
        End If
        sicherheitsfrageUeberspringen = False
    End Sub

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        PROG.setParam("prog_prod_path_wai", txtProdPfadWAI.Text, codename)
        init()
    End Sub

    Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
        PROG.setParam("prog_prod_path_nkd", txtProdPfadNKD.Text, codename)
        init()
    End Sub

    Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
        PROG.setParam("prog_prod_path_neu", txtProdPfadNEU.Text, codename)
        init()
    End Sub

    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
        PROG.setParam("prog_installer_prod_path_wai", txtInstallProdPfadWAI.Text, codename)
        init()
    End Sub

    Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
        PROG.setParam("prog_installer_prod_path_uni", txtInstallProdPfadUNI.Text, codename)
        init()
    End Sub

    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
        PROG.setParam("prog_installer_prod_path_amb", txtInstallProdPfadAMB.Text, codename)
        init()
    End Sub

    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadWAI.Text, cbxZip.Checked, If(cbxStandorttxtErstellenWAI.Checked, "WAI", ""))

    End Sub

    Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadNKD.Text, cbxZip.Checked, If(cbxStandorttxtErstellenNKD.Checked, "NKD", ""))

    End Sub

    Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadNEU.Text, cbxZip.Checked, If(cbxStandorttxtErstellenNEU.Checked, "NEU", ""))

    End Sub

    Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadWAI.Text, cbxZip.Checked)

    End Sub

    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadUNI.Text, cbxZip.Checked)

    End Sub

    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadAMB.Text, cbxZip.Checked)

    End Sub

    Private Sub Button30_Click(sender As Object, e As EventArgs) Handles Button30.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadUNI.Text, cbxZip.Checked, If(cbxStandorttxtErstellenUNI.Checked, "UNISPED", ""))
    End Sub

    Private Sub Button28_Click(sender As Object, e As EventArgs) Handles Button28.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadAMB.Text, cbxZip.Checked, If(cbxStandorttxtErstellenAMB.Checked, "AMBAR", ""))
    End Sub

    Private Sub Button29_Click(sender As Object, e As EventArgs) Handles Button29.Click
        PROG.setParam("prog_prod_path_uni", txtProdPfadUNI.Text, codename)
        init()
    End Sub

    Private Sub Button27_Click(sender As Object, e As EventArgs) Handles Button27.Click
        PROG.setParam("prog_prod_path_amb", txtProdPfadAMB.Text, codename)
        init()
    End Sub

    Private Sub Button34_Click(sender As Object, e As EventArgs) Handles Button34.Click
        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadNKD.Text, cbxZip.Checked)
    End Sub


    Private Sub Button32_Click(sender As Object, e As EventArgs) Handles Button32.Click

        copyProgrammFiles(sender, txtInstallSourcePfad.Text, txtInstallProdPfadNEU.Text, cbxZip.Checked)
    End Sub

    Private Sub Button33_Click(sender As Object, e As EventArgs) Handles Button33.Click
        PROG.setParam("prog_installer_prod_path_nkd", txtInstallProdPfadNKD.Text, codename)
        init()
    End Sub

    Private Sub Button31_Click(sender As Object, e As EventArgs) Handles Button31.Click
        PROG.setParam("prog_installer_prod_path_neu", txtInstallProdPfadNEU.Text, codename)
        init()
    End Sub

    Dim LIST As New List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)
    Private Sub Button35_Click(sender As Object, e As EventArgs) Handles Button35.Click
        If txtSourcePfad.Text = "" Then Exit Sub
        If Not IO.Directory.Exists(txtSourcePfad.Text) Then Exit Sub
        Dim LIST = New List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)

        Dim dirName = ""
        VERAG_PROG_ALLGEMEIN.cProgrammeUpdate.RekursivGetFiles(New IO.DirectoryInfo(txtSourcePfad.Text), dirName, LIST, {})

        For Each f In IO.Directory.GetFiles(txtSourcePfad.Text)
            Dim FI As New IO.FileInfo(f)
            If dirName = "" Then
                dirName = FI.DirectoryName
                MsgBox(dirName)
            End If

            Dim UD As New VERAG_PROG_ALLGEMEIN.cProgrammeUpdate
            UD.pud_fileName = FI.Name
            UD.pud_filePathFolder = FI.DirectoryName.Replace(dirName, "")
            If UD.pud_filePathFolder = "" Then UD.pud_filePathFolder = Nothing
            UD.pud_file_Size = FI.Length
            UD.pud_file_Date = FI.LastWriteTime
            UD.pud_file_Verion = "" 'FI.
            LIST.Add(UD)
        Next
        Dim daten As cProgramm = PROG.getProgrammByCodeName(codename)
        If daten Is Nothing Then Exit Sub
        VERAG_PROG_ALLGEMEIN.cProgrammeUpdate.SAVE_LIST(daten.prog_id, txtVersion.Text.Replace(".", ""), codename, LIST)
    End Sub


    Public Function isNewFile(ByRef file As VERAG_PROG_ALLGEMEIN.cProgrammeUpdate, LIST_DEST As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)) As Boolean
        For Each l In LIST_DEST
            If l.pud_filePathFolder = file.pud_filePathFolder And l.pud_fileName = file.pud_fileName And l.pud_file_Size = file.pud_file_Size And l.pud_file_Date = file.pud_file_Date Then
                Return False
            End If
        Next
        Return True
    End Function

    Public Function isOldFile(ByRef file As VERAG_PROG_ALLGEMEIN.cProgrammeUpdate, LIST_SOURCE As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)) As Boolean
        For Each l In LIST_SOURCE
            If l.pud_filePathFolder = file.pud_filePathFolder And l.pud_fileName = file.pud_fileName And l.pud_file_Size = file.pud_file_Size And l.pud_file_Date = file.pud_file_Date Then
                Return False
            End If
        Next
        Return True
    End Function

    Private Sub Button36_Click(sender As Object, e As EventArgs) Handles Button36.Click
        PROG.setParam("prog_prod_path_backup", txtProdPfadBackup.Text, codename)
        init()
    End Sub

    Private Sub Button37_Click(sender As Object, e As EventArgs) Handles Button37.Click
        copyProgrammFiles(sender, txtSourcePfad.Text, txtProdPfadBackup.Text, cbxZip.Checked, "")
    End Sub
End Class