Imports System.Threading
Imports System.Security

Public Class frmStatusFSSAtlas
    ' Public verarbeitungskennzeichen As String = ""
    Public artikel As List(Of cArtikel)
    Dim SQLZoll As New SQLZoll
    WithEvents Timer1 As New System.Windows.Forms.Timer
    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub frmStatusFSSAtlas_Load(sender As Object, e As EventArgs) Handles Me.Load
        Timer1.Interval = 1000
        Timer1.Enabled = False
    End Sub

    Private Sub frmStatusFSSAtlas_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        ' frmAddArtikelATLAS.setStatus("TEST")
        Dim st As New Thread(AddressOf doWorkEinzeln)
        st.IsBackground = True
        st.Start()

    End Sub
    Sub doWorkEinzeln()
        Me.BringToFront()
        Thread.Sleep(1000)
        Dim FSS As New cATLAS_FSS
        Dim SC As New cServerClient
        Dim boolSUB As Boolean = False
        Dim boolSBG As Boolean = False
        Dim subOUT As String = ""
        Dim sbgOUT As String = ""


        Dim schnittstelleEinlesen As Boolean = True
        ' Netzlaufwerk verbinden
        ' Dim sDrive As String = "T:"
        ' Dim NET As New cNetworkDrive
        Try
            'SUBEN 
            
            If frmAddArtikelATLAS.cbxSUB.Checked Then

                '  Try
                'System.Diagnostics.Process.Start("net use t: " & FSS.serverpath_SUB & " /user:VERAGOST\Administrator BmWr501956")
                '  Catch ex As Exception
                'MsgBox(ex.Message)
                ' End Try



                '  System.Diagnostics.Process.Start("\\192.168.133.98\G\atlas\atlas\", "Administrator", securePwd, "VERAGSBG")
                ' If Not NET.AddNetworkDrive(sDrive, FSS.serverpath_SUB, "VERASGOST\Administrator", "BmWr501956") Then MsgBox("FEHLER beim Verbinden mit dem Netzwerklaufwerk ATLAS-SUB") : Exit Sub
                ' Netzlaufwerk verbinden

                ' If NET.MapDrive(sDrive, FSS.serverpath_SUB) = True Then
                ' MsgBox("Laufwerk verbunden (" & sDrive & ")")
                'End If

                txtSuben.Text = ""
                setVisible(lblStat_1, True)
                setVisible(picStat_1, True)
                For Each a In artikel
                    a.tmp_rueckgabeFileSUB = FSS.sendArtikelToATLAS("SUB", a)

                    If a.tmp_rueckgabeFileSUB = "" Then
                        setVisible(lblStat_ERROR, True)
                        Exit Sub
                    ElseIf a.tmp_rueckgabeFileSUB.Contains("ERROR") Then
                        setVisible(lblStat_ERROR, True)
                    End If
                Next
                setVisible(lblStat_2, True)
                setVisible(picStat_2, True)
                setVisible(lblStat_3, True)
                setVisible(picStat_3, True)

                If schnittstelleEinlesen Then
                    SC.begin("192.168.0.95", "FSSAtlasStart") '192.168.0.95 = atlas suben
                    setVisible(lblStat_4, True)
                    setVisible(picStat_4, True)

                    SC.waitTillFinished()
                    Thread.Sleep(1000)

                    If SC.status = "TaskSuccess" Then
                        '    MsgBox("OK" & vbNewLine & ev.Message)
                        setVisible(lblStat_5, True)
                        setVisible(picStat_5, True)

                        For Each a In artikel
                            Try : a.tmp_rueckgabetextSUB = System.IO.File.ReadAllText(a.tmp_rueckgabeFileSUB)
                            Catch ex As Exception
                                a.tmp_rueckgabetextSUB = ex.Message
                            End Try
                            a.tmp_rueckgabeSUB = EinarbeitungSuccessful(a.tmp_rueckgabetextSUB)
                            txtSuben.Text &= "<b>" & a.tmp_rueckgabeFileSUB & "</b>" & vbNewLine & vbNewLine & a.tmp_rueckgabetextSUB & vbNewLine & vbNewLine
                            If a.tmp_rueckgabeSUB Then
                                SQLZoll.setStatusArtikel(a.zollArt_id, "ATLAS OK")
                            Else
                                SQLZoll.setStatusArtikel(a.zollArt_id, "ATLAS ERR")
                            End If
                        Next
                    Else
                        setVisible(lblStat_ERROR, True)
                        '  MsgBox(SC.ev.Message)
                        Exit Sub
                    End If

                End If

                'AddHandler SC.FunctionFinished, Sub(se As Object, ev As FunctionFinishedArgs)
                'If ev.Result = FunctionFinishedArgs.EventResult.OK Then
                'lblStat_5, True)
                'picStat_5, True)
                'Else
                'lblStat_ERROR, True)
                '           MsgBox(ev.Message)
                '          Exit Sub
                '         End If
                'End Sub
                setVisible(lblStat_6, True) ' noch keine Function
                setVisible(picStat_6, True)
                '  boolSUB = True
                '  NET.RemoveNetworkDrive(sDrive)
            Else
                txtSuben.Text = "Einarbeitung SUBEN deaktiviert."
            End If

            'SALZBURG
            If frmAddArtikelATLAS.cbxSBG.Checked Then

                '  System.Diagnostics.Process.Start("net use t: " & FSS.serverpath_SBG & " /user:VERAGSBG\Administrator wassermann")


                ' If Not NET.AddNetworkDrive(sDrive, FSS.serverpath_SBG, "veragsbg\Administrator", "wassermann") Then MsgBox("FEHLER beim Verbinden mit dem Netzwerklaufwerk ATLAS-SBG") : Exit Sub
                txtSalzburg.Text = ""
                For Each a In artikel
                    a.tmp_rueckgabeFileSBG = FSS.sendArtikelToATLAS("SBG", a)
                    If a.tmp_rueckgabeFileSBG = "" Then
                        setVisible(lblStat_ERROR, True)
                        Exit Sub
                    ElseIf a.tmp_rueckgabeFileSBG.Contains("ERROR") Then
                        setVisible(lblStat_ERROR, True)
                    End If
                Next
                setVisible(lblStat_7, True)
                setVisible(picStat_7, True)
                setVisible(lblStat_8, True)
                setVisible(picStat_8, True)

                If schnittstelleEinlesen Then
                    SC.begin("192.168.133.98", "FSSAtlasStart") '192.168.0.95 = atlas suben
                    setVisible(lblStat_9, True)
                    setVisible(picStat_9, True)

                    SC.waitTillFinished()
                    Thread.Sleep(1000)
                    If SC.status = "TaskSuccess" Then
                        '    MsgBox("OK" & vbNewLine & ev.Message)
                        setVisible(lblStat_10, True)
                        setVisible(picStat_10, True)

                        For Each a In artikel
                            Try : a.tmp_rueckgabetextSBG = System.IO.File.ReadAllText(a.tmp_rueckgabeFileSBG)
                            Catch ex As Exception
                                a.tmp_rueckgabetextSBG = ex.Message
                            End Try
                            a.tmp_rueckgabeSBG = EinarbeitungSuccessful(a.tmp_rueckgabetextSBG)
                            txtSalzburg.Text &= "<b>" & a.tmp_rueckgabeFileSBG & "</b>" & vbNewLine & vbNewLine & a.tmp_rueckgabetextSBG & vbNewLine & vbNewLine
                            If a.tmp_rueckgabeSBG Then
                                SQLZoll.setStatusArtikel(a.zollArt_id, "ATLAS OK")
                            Else
                                SQLZoll.setStatusArtikel(a.zollArt_id, "ATLAS ERR")
                            End If
                        Next
                    Else
                        setVisible(lblStat_ERROR, True)
                        '  MsgBox(SC.ev.Message)
                        Exit Sub
                    End If

                End If
                '   AddHandler SC.FunctionFinished, Sub(se As Object, ev As FunctionFinishedArgs)
                'If ev.Result = FunctionFinishedArgs.EventResult.OK Then
                'lblStat_10, True)
                ' picStat_10, True)
                ' Else
                ' lblStat_ERROR, True)
                ' MsgBox(ev.Message)
                ' Exit Sub
                ' End If
                ' End Sub
                setVisible(lblStat_11, True) ' noch keine Function
                setVisible(picStat_11, True)
                'boolSBG = True
                '  NET.RemoveNetworkDrive(sDrive)
            Else
                txtSalzburg.Text = "Einarbeitung SALZBURG deaktiviert."
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try





        If checkErrors(artikel, frmAddArtikelATLAS.cbxSUB.Checked, frmAddArtikelATLAS.cbxSBG.Checked) Then ' Fehler aufgetreten?
            setVisible(lblStat_FERTIG, True)
            setVisible(lblStat_Uebertragen, True)
            Timer1.Enabled = True
            ' frmAddArtikelATLAS.setStatus("ATLAS OK")
        Else
            '   frmAddArtikelATLAS.setStatus("ATLAS ERR")
            setVisible(lblStat_ERROR, True)
        End If
    End Sub

    Function checkErrors(ByVal artikel As List(Of cArtikel), ByVal suben As Boolean, ByVal sbg As Boolean) As Boolean
        For Each a In artikel
            If suben And Not a.tmp_rueckgabeSUB Then Return False
            If sbg And Not a.tmp_rueckgabeSBG Then Return False
        Next
        Return True
    End Function




    'threadsicherer Aufruf
    Delegate Sub setStatusCallback(s As String)
    Private Sub setStatus(s As String)
        If Me.InvokeRequired Then
            Dim d As New setStatusCallback(AddressOf setStatus)
            Me.Invoke(d, New Object() {s})
        Else
            frmAddArtikelATLAS.lblStatus.Text = s
            frmAddArtikelATLAS.btnSave.PerformClick()
        End If

    End Sub

    Function EinarbeitungSuccessful(txt As String) As Boolean
        If txt.Contains("BAR-Nachricht fehlerfrei eingearbeitet") Then Return True
        If txt.Contains("BAR-Nachricht mit") And txt.Contains("Warnungen eingearbeitet") Then Return True
        Return False
    End Function


    'threadsicherer Aufruf
    Delegate Sub setVisibleCallback(o As Object, visible As Boolean)
    Private Sub setVisible(o As Object, visible As Boolean)
        If Me.InvokeRequired Then
            Dim d As New setVisibleCallback(AddressOf setVisible)
            Me.Invoke(d, New Object() {o, visible})
        Else
            o.visible = visible
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        lblStat_Uebertragen.Visible = False
    End Sub

    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        Me.Close()
    End Sub
End Class