Imports System.Threading
Imports IntraSell_DLL
Imports IntraSell_Net_Base

''' <summary>
''' Customer Pole Display  
''' 8035 SERIES 
''' Version: 1.0
''' </summary>
''' <remarks></remarks>
Public Class CustomerDisplayJARLTECH

    Dim printThread
    Shared previewForm As IntraSell_Net_CustomerDisplay.Display
    Friend vorgangBar As AbstractVorgangBar

    Public Const LF As String = Chr(&HD) & Chr(&HA)   ' Chr(13) & Chr(10)
    Public Const CR As String = Chr(&HD)
    Public Const RS As String = Chr(&H1E)
    Public Const ESC As String = Chr(&H1B)
    Public Const GS As String = Chr(&H1D)
    Public Const NUL As String = Chr(0)
    Const INIT As String = ESC & "@" 'initialize 
    Const STRING_MODE = Chr(27)
    Public Const CURSOR_HOME As String = Chr(&HB) 'Cursor HOME
    Public Const CURSOR_LEFT As String = Chr(9)
    Public Const CURSOR_RIGHT As String = Chr(8)
    Public Const CURSOR_DOWN As String = Chr(&HA)

    Const STRING_DISPLAY_UPPER_LINE As String = ESC & "QA"
    Const STRING_DISPLAY_SECOND_LINE As String = ESC & "QB"

    Const RESET = Chr(27) + Chr(40) 'RESET
    Const VERTICAL_SCROLL = Chr(&H1B) + Chr(&H12) 'VERT SCROLL

    Dim _CUSTOMER_DISPLAY_PRINT_PORT, _CUSTOMER_DISPLAY_BAUD_RATE, _CUSTOMER_DISPLAY_DATA_BITS, _CUSTOMER_DISPLAY_PARITY, _CUSTOMER_DISPLAY_STOP_BITS
    Friend _CUSTOMER_DISPLAY_ANZEIGE_AM_BILDSCHIRM As Boolean
    Friend _CUSTOMER_DISPLAY_PRINT As Boolean = True

    Public Sub InitSettings()
        _CUSTOMER_DISPLAY_PRINT_PORT = VarValue_Default(CUSTOMER_DISPLAY_PRINT_PORT, "com3")
        _CUSTOMER_DISPLAY_BAUD_RATE = VarValue_Default("CUSTOMER_DISPLAY_BAUD_RATE", "9600")
        _CUSTOMER_DISPLAY_DATA_BITS = VarValue_Default("CUSTOMER_DISPLAY_DATA_BITS", "8")
        _CUSTOMER_DISPLAY_PARITY = VarValue_Default("CUSTOMER_DISPLAY_PARITY", "0").Substring(0, 1)
        _CUSTOMER_DISPLAY_STOP_BITS = VarValue_Default("CUSTOMER_DISPLAY_STOP_BITS", "1").Substring(0, 1)
        _CUSTOMER_DISPLAY_ANZEIGE_AM_BILDSCHIRM = VarValue_Default(CUSTOMER_DISPLAY_ANZEIGE_AM_BILDSCHIRM, True)
    End Sub

    Public Sub SetVorgangBar(form As AbstractVorgangBar)
        InitSettings()
        vorgangBar = form
        If ExistsVarValue(CUSTOMER_DISPLAY_PRINT_PORT) Then
            PrintOnCustomerDisplay(VarValue_Default("CUSTOMER_DISPLAY_WILLKOMMEN1", "HERZLICH"), _
                                   VarValue_Default("CUSTOMER_DISPLAY_WILLKOMMEN2", "WILLKOMMEN"), True)
            'add events 
            AddHandler vorgangBar.ArtikelAenderung, AddressOf ArtikelErfasst
            AddHandler ModuleBuchVorgang.VorgangAbgeschlossen, AddressOf VorgangAbgeschlossen
        End If
    End Sub

    Friend Sub ArtikelErfasst(ArtNr As Integer, Preis As Decimal, Total As Decimal, vorgang As dsVorgaenge.buchvorgangRow)
        Dim artbez As String = ArtikelBezeichnung(ArtNr)
        If artbez.Length > 10 Then
            artbez = artbez.Substring(0, 10)
        End If
        Dim lin1 = artbez + ": " + Format(Preis, "0.00")
        Dim lin2 = "TOTAL: " + Format(Total, "0.00")
        PrintOnCustomerDisplay(lin1, lin2, True)
    End Sub

    Friend Sub VorgangAbgeschlossen(Endsume As Decimal)
        Dim lin1 = "TOTAL: " + Format(Endsume, "C2")
        Dim lin2 = "AUF WIEDERSEHEN!"
        PrintOnCustomerDisplay(lin1, lin2, True)
    End Sub

    ''' <summary>
    ''' Write to com port
    ''' </summary>
    ''' <param name="_line1"></param>
    ''' <param name="_line2"></param>
    ''' <param name="_useCom"></param>
    ''' <remarks></remarks>
    Friend Sub PrintOnCustomerDisplay(_line1 As String, _line2 As String, _useCom As Boolean)
        line1 = _line1.PadLeft(20).Substring(0, 20)
        line2 = _line2.PadLeft(20).Substring(0, 20)


        If _CUSTOMER_DISPLAY_ANZEIGE_AM_BILDSCHIRM Then
            If previewForm Is Nothing Then
                previewForm = New IntraSell_Net_CustomerDisplay.Display
                previewForm.Show()
            End If
            previewForm.Display(line1, line2)
        End If

        line1 = _line1
        line2 = _line2
        useCom = _useCom
        If _CUSTOMER_DISPLAY_PRINT Then
            printThread = New Thread(AddressOf PrintOnCustomerDisplayThread) With {
                .IsBackground = True
            }
            printThread.Start()
        End If
    End Sub

    Dim line1 As String
    Dim line2 As String
    Dim useCom As String

    Sub PrintOnCustomerDisplayThread()
        If useCom Then
            'SendSerialData(RESET + VERTICAL_SCROLL + _
            ' CURSOR_HOME + _
            ' Chr(&H1B) + Chr(&H51) + Chr(&H41) + line1 + Chr(&HD) + _
            ' Chr(&H1B) + Chr(&H51) + Chr(&H42) + line2 + Chr(&HD))

            SendSerialData(Chr(27) + Chr(40)) 'RESET
            SendSerialData(Chr(&HC)) 'CLEAR
            SendSerialData(Chr(&HD)) 'HOME
            SendSerialData(Chr(&H1B) + Chr(&H12)) 'VERT SCROLL
            SendSerialData(line1.PadLeft(19)) 'Zeile 1 
            SendSerialData(line2.PadLeft(20)) 'Zeile 2 

        Else
            Dim lines As String
            lines = RESET + VERTICAL_SCROLL
            lines += line1
            lines += CURSOR_DOWN
            lines += line2
            WriteFile("custdisp.pos", lines)
            PrintFile("custdisp.pos")
        End If
    End Sub


    ''' <summary>
    ''' Print a file to port 
    ''' Default port is /d:lpt3
    ''' </summary>
    ''' <param name="filename"></param>
    ''' <remarks></remarks>
    Sub PrintFile(filename As String)
        Dim port = VarValue_Default(CUSTOMER_DISPLAY_PRINT_PORT, "/d:lpt2")
        Shell("print " & port & " " & filename, AppWinStyle.Hide, False)
    End Sub

    Sub WriteFile(filename As String, content As String)
        Try
            FileOpen(1, filename, OpenMode.Output, OpenAccess.Write, OpenShare.LockReadWrite)
            Print(1, content)
        Catch
        Finally
            FileClose(1)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="data"></param>
    ''' <remarks></remarks>
    Sub SendSerialData(data As String)
        Try
            ' Send strings to a serial port.
            Using com1 As IO.Ports.SerialPort =
            My.Computer.Ports.OpenSerialPort(portName:=_CUSTOMER_DISPLAY_PRINT_PORT, _
                                             baudRate:=_CUSTOMER_DISPLAY_BAUD_RATE, _
                                             dataBits:=_CUSTOMER_DISPLAY_DATA_BITS, _
                                             parity:=_CUSTOMER_DISPLAY_PARITY, _
                                             stopBits:=_CUSTOMER_DISPLAY_STOP_BITS) 'Default settings of Glancetron 8035
                'com1.Handshake = 
                Dim log As String = com1.BaudRate + com1.DataBits + com1.Parity + com1.StopBits
                'Label1.Text = log
                com1.Write(data)
                com1.Close()
            End Using
        Catch ex As Exception
            'Label1.Text = Err.Description
        End Try
    End Sub

End Class
