Imports System.IO
Imports VERAG_PROG_ALLGEMEIN.TESTJSON

Public Class frmAsfinag_NachrichtenVerabeitung
    Dim cntDatenEingelesen As Integer = 0
    Dim dsNichtErkannt As Integer = 0
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim allowClose = False
    Dim PARTNERSYSTEM = "ASFINAG"
    Dim FileType As String
    Dim testFTP As Boolean
    Dim asfinag As New cAsfinag()
    Dim Dateiname = ""
    Dim empfangen As Boolean = False
    Dim eingelesen As Boolean = False

    Sub New(FileType, testFTP)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.FileType = FileType
        Me.testFTP = testFTP
    End Sub
    Sub New(FileType, testFTP, allowClose) 'autostart

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        Me.FileType = FileType
        Me.testFTP = testFTP
        Me.allowClose = allowClose
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub frmDYNachrichtenVerarbeitung_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblTestsystem.Visible = VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM
        lblFTP.Text = IIf(testFTP, "TEST-FTP", "PROD-FTP")
        asfinag.initImportPfade()

        If FileType <> "" Then

            Label2.Text &= " " & FileType.ToUpper

            For Each d In System.IO.Directory.GetFiles(asfinag.VERARBEITUNG_PFAD)
                Dim filename = frmStartOptions.cut_file(d)
                If filename.ToUpper.Contains(FileType) Then
                    Dim targetsystem As String = "nicht definiert"
                    If filename.Substring(0, 1) = "T" Then
                        targetsystem = "Test"
                    ElseIf filename.Substring(0, 1) = "L" Then
                        targetsystem = "Produktiv"
                    End If
                    ListBox3.Items.Add(filename)
                End If

                Me.Refresh()
            Next
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Enabled = False
        Try
            Dim stringLIst As New List(Of String)
            Label9.Visible = True
            Dim cnt = asfinag.getNachrichtenDatenFromFTP(stringLIst, FileType, testFTP)
            For Each s In stringLIst
                Dim targetsystem As String = "nicht definiert"
                If s.Substring(0, 1) = "T" Then
                    targetsystem = "Test"
                ElseIf s.Substring(0, 1) = "L" Then
                    targetsystem = "Produktiv"
                End If
                ListBox3.Items.Add(s)
            Next

            Label9.Text = cnt & " Datensätze empfangen."
            empfangen = True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Enabled = True

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Enabled = False
        Try
            Label5.Visible = True

            DatenEinlesen(eingelesen) '<<<<------------

            If dsNichtErkannt > 0 Then

                Label6.Text = dsNichtErkannt & " Datensätze nicht " & vbNewLine & "erkannt."
                Label6.Visible = True
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Enabled = True
        Label5.Text = cntDatenEingelesen & " Datensätze verarbeitet."

    End Sub


    Sub DatenEinlesen(eingelesen As Boolean)

        VERAG_PROG_ALLGEMEIN.cAllgemein.ERR_OP_GLOBAL = VERAG_PROG_ALLGEMEIN.ERROR_OP.MAIL

        If ListBox3.Items.Count = 0 Then
            Label6.Text = "Keine Daten zum Einlesen vorhanden."
            Label6.Visible = True
            eingelesen = True
            Exit Sub
        End If


        For Each d In System.IO.Directory.GetFiles(asfinag.VERARBEITUNG_PFAD)

            Try
                Dim correctFile As Boolean = False
                For Each i In ListBox3.Items
                    Dim fileName As FileInfo = New FileInfo(d)
                    If Equals(fileName.Name, i) Then
                        correctFile = True
                        Exit For
                    End If
                Next


                Dim found = False
                Dateiname = d
                If System.IO.File.Exists(d) And correctFile Then


                    Dim nr_id As Integer = -1

                    Dim fi As FileInfo = New FileInfo(d)
                    If Not fi.Name.StartsWith(".") Then

                        Dim alreadyMoved = False

                        Dim objFileRead As StreamReader = New StreamReader(d)

                        If objFileRead.Peek() = -1 Then

                            MsgBox("Datei hat keinen Inhalt!",
                                       MsgBoxStyle.Information,
                                       MsgBoxStyle.DefaultButton2 = MsgBoxStyle.OkOnly)
                            Exit Sub

                        ElseIf FileType = "CLF" Then

                            If asfinag.readAndSaveCLF(objFileRead, fi) Then
                                addDGVEinarbeitung(fi.Name, "OK")
                                found = True
                            Else
                                addDGVEinarbeitung(fi.Name, "Fehler")
                            End If


                        ElseIf FileType = "TTR" Then

                            If asfinag.readAndSaveTTR(objFileRead, fi) Then
                                addDGVEinarbeitung(fi.Name, "OK")
                                found = True
                            Else
                                addDGVEinarbeitung(fi.Name, "Fehler")
                            End If

                        End If

                    End If

                    If found Then
                        If Not System.IO.Directory.Exists(asfinag.ARCHIV_PFAD & "\" & Now.Year & "\" & FileType) Then System.IO.Directory.CreateDirectory(asfinag.ARCHIV_PFAD & "\" & Now.Year & "\" & FileType & "\")
                        frmStartOptions.moveFile(d, asfinag.ARCHIV_PFAD & "\" & Now.Year & "\" & FileType & "\", asfinag.VERARBEITUNG_PFAD)
                        cntDatenEingelesen += 1
                    Else
                        If Not found Then
                            If Not System.IO.Directory.Exists(asfinag.ERROR_PFAD & "\" & Now.Year & "\" & FileType & "\") Then System.IO.Directory.CreateDirectory(asfinag.ERROR_PFAD & "\" & Now.Year & "\" & FileType & "\")
                            frmStartOptions.moveFile(d, asfinag.ERROR_PFAD & "\" & Now.Year & "\" & FileType & "\", asfinag.VERARBEITUNG_PFAD)
                            ' nciht gefunden, oder durch ERROR nicht eingearbt,.
                            dsNichtErkannt += 1
                        End If
                    End If
                    Me.Refresh()

                End If


            Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine & "Datei: " & Dateiname, System.Reflection.MethodInfo.GetCurrentMethod.Name)
                eingelesen = False
            End Try
        Next
        eingelesen = True

    End Sub

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

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



    Sub addDGVEinarbeitung(art, BezugsNr)
        dgvEinarbeitung.Rows.Add(art, BezugsNr)
        Try
            dgvEinarbeitung.FirstDisplayedScrollingRowIndex = dgvEinarbeitung.RowCount - 1
        Catch ex As Exception
        End Try
    End Sub


    Private Sub dgvEinarbeitung_SelectionChanged(sender As Object, e As EventArgs) Handles dgvEinarbeitung.SelectionChanged
        dgvEinarbeitung.ClearSelection()

    End Sub

    Private Sub frmTCNachrichtenVerarbeitung_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If allowClose Then
            Me.Refresh()
            Button2.PerformClick()
            For i As Integer = 0 To 5
                If Not empfangen Then System.Threading.Thread.Sleep(2000)
                i = i + 1
            Next
            Button1.PerformClick()
            For j As Integer = 0 To 10
                If Not eingelesen Then System.Threading.Thread.Sleep(20000)
                j = j + 1
            Next
            Me.Close()
        End If
    End Sub


    Private Sub pnl_Paint(sender As Object, e As PaintEventArgs) Handles pnl.Paint

    End Sub


    Public Shadows Function Right(ByVal sText As String,
    ByVal nLen As Integer) As String

        If nLen > sText.Length Then nLen = sText.Length
        Return (sText.Substring(sText.Length - nLen))
    End Function

    Public Shadows Function Left(ByVal sText As String,
    ByVal nLen As Integer) As String

        If nLen > sText.Length Then nLen = sText.Length
        Return (sText.Substring(0, nLen))
    End Function
End Class

