Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms
Imports System.Runtime.InteropServices
Imports Bt.ScanLib
Imports Bt
Public Class BtScanLib
    Private Shared logger As LogWriter = LogWriter.getInstance()
    Public Property Debug() As Boolean
        Get

        End Get
        Set(ByVal value As Boolean)

        End Set
    End Property
    <DllImport("coredll.dll", EntryPoint:="DeleteObject")> _
    Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    End Function

    Public Event OnScan As ScanEventHandler

    Private Shared btScanLib As BtScanLib = Nothing
    Public Shared Function getInstance() As BtScanLib
        If btScanLib Is Nothing Then
            btScanLib = New BtScanLib()
        End If

        Return btScanLib
    End Function
    Private Sub New()
        ' Initial BT scan library
        Bt.ScanLib.Utility.btScanSetServiceEnable(1)
    End Sub
    Public Sub Enable()
        Bt.ScanLib.Control.btScanEnable()
    End Sub
    Public Sub Disable()
        Bt.ScanLib.Control.btScanDisable()
    End Sub

    Protected Sub WndProc(ByRef msg As Microsoft.WindowsCE.Forms.Message)
        Select Case msg.Msg
            Case CType(LibDef.WM_BT_SCAN, Integer)
                ' success scan
                If msg.WParam.ToInt32() = CType(LibDef.BTMSG_WPARAM.WP_SCN_SUCCESS, Integer) Then
                    ' scan once
                    ScanData_once()
                End If
        End Select
        'MyBase.WndProc(msg)
        WndProc(msg)
    End Sub

    Private Sub ScanData_once()

        Dim ret As Int32 = 0
        Dim disp As String = ""

        Dim resultCount As Int32 = 0
        Dim codedataGet() As Byte
        Dim codeLen As Int32 = 0
        Dim stReportGet As New LibDef.BT_SCAN_REPORT()
        Dim stQrReportGet As New LibDef.BT_SCAN_QR_REPORT()

        Try
            resultCount = Bt.ScanLib.Control.btScanGetResultCount()
            If resultCount <= 0 Then
                disp = "btScanGetResultCount error ret[" & resultCount & "]"
                ' MessageBox.Show(disp, "Error")
                GoTo L_END
            End If

            ' Code read event
            If resultCount > 0 Then
                '-----------------------------------------------------------
                ' Read (individual)
                '-----------------------------------------------------------
                For i As UInt32 = 0 To resultCount - 1
                    codeLen = Bt.ScanLib.Control.btScanGetDataSize(i)
                    If codeLen <= 0 Then
                        disp = "btScanGetDataSize error ret[" & codeLen & "]"
                        'MessageBox.Show(disp, "Error")
                        GoTo L_END
                    End If
                    ReDim codedataGet(codeLen - 1)

                    ' btScanGetData
                    stReportGet = New LibDef.BT_SCAN_REPORT()
                    stQrReportGet = New LibDef.BT_SCAN_QR_REPORT()
                    ret = Bt.ScanLib.Control.btScanGetData(i, codedataGet, stReportGet, stQrReportGet)
                    If ret <> LibDef.BT_OK Then
                        disp = "btScanGetData error ret[" & ret & "]"
                        'MessageBox.Show(disp, "Error")
                        GoTo L_END
                    End If
                    If stReportGet.codetype = LibDef.BT_SCAN_CODE_OCR Then
                        Dim objOcr As New LibDef.BT_SCAN_OCR_REPORT()
                        Dim objOcrImg As New LibDef.BT_SCAN_OCR_REPORT_IMAGE()
                        ret = Bt.ScanLib.Control.btScanGetOCRData(objOcr, objOcrImg)
                        If ret <> LibDef.BT_OK Then
                            disp = "btScanGetOCRData error"
                            'MessageBox.Show(disp, "Error")
                            'DeleteObject(objOcrImg.bitmap)
                            GoTo L_END
                        End If
                        Dim strAtt As String = ""
                        For j As UInt32 = 0 To objOcr.SourceDataLen - 1
                            strAtt = strAtt & objOcr.CharAttention(j)
                        Next
                        Dim result As String = System.Text.Encoding.ASCII.GetString(codedataGet, 0, codedataGet.Length)
                        disp = "OCR recognition pattern:" & objOcr.DataFormat & vbCrLf & _
                               "Misidentification alert information:" & objOcr.AlertType & vbCrLf & _
                               "Format registration number:" & objOcr.FormatNumber & vbCrLf & _
                               "Recognized string length:" & objOcr.SourceDataLen & vbCrLf & _
                               "Recognized string:" & System.Text.Encoding.ASCII.GetString(objOcr.SourceData, 0, objOcr.SourceDataLen) & vbCrLf & _
                               "Character credit information:" & strAtt & vbCrLf
                        'MessageBox.Show(disp, "Read (individual)")
                        If Debug Then logger.Log(disp)
                        'DeleteObject(objOcrImg.bitmap)

                        RaiseEvent OnScan(result)
                    Else
                        'Successful barcode reading
                        Dim result As String = System.Text.Encoding.ASCII.GetString(codedataGet, 0, codedataGet.Length)
                        disp = "Data size:" & codeLen & vbCrLf & _
                               "[Report]" & vbCrLf & _
                               "Number of digits:" & stReportGet.keta & vbCrLf & _
                               "result:" & result & vbCrLf & _
                               "Code type:" & stReportGet.codetype & vbCrLf & _
                               "Is it a composite:" & stReportGet.composite & vbCrLf & _
                               "Quality:" & stReportGet.quality & vbCrLf & _
                               "Details:" & stReportGet.extraType & vbCrLf & _
                               "Code synthesis:" & stReportGet.codelink & vbCrLf & _
                               "[QR]" & vbCrLf & _
                               "Read result position:" & stQrReportGet.pos & vbCrLf & _
                               "Parity:" & stQrReportGet.parity & vbCrLf & _
                               "Total number of concatenations:" & stQrReportGet.count & vbCrLf
                        'MessageBox.Show(disp, "Read (individual)")
                        If Debug Then logger.Log(disp)
                        RaiseEvent OnScan(result)
                    End If
                Next
            End If

L_END:
            ret = Bt.ScanLib.Control.btScanDisable()
            If ret <> LibDef.BT_OK Then
                disp = "btScanDisable error ret[" & ret & "]"
                'MessageBox.Show(disp, "Error")
                'MessageBox.Show("Please try again.", "Scan Error")
            End If

            ret = Bt.ScanLib.Control.btScanEnable()
            If ret <> LibDef.BT_OK Then
                disp = "btScanEnable error ret[" & ret & "]"
                'MessageBox.Show(disp, "Error")
                'MessageBox.Show("Please try again.", "Scan Error")
            End If
        Catch ex As MissingMethodException
            'MessageBox.Show("Contains APIs that cannot be used with this terminal.")
            'MessageBox.Show("Please try again.", "Scan Error")
        Catch e As Exception
            'MessageBox.Show(e.Message & ", Please try again.", "Scan Error")
            'MessageBox.Show(ex.ToString())

            'Call ErrorForm.cs here.
            'ErrorForm err = new ErrorForm();
            'err.Show(); 

        End Try
    End Sub


    Private Sub ScanData_bulk()
        Dim ret As Int32 = 0
        Dim disp As String = ""

        Dim codedataGet() As Byte
        Dim strCodedata As String = ""
        Dim codeLen As Int32 = 0
        Dim symbolGet As UInt16 = 0

        Try
            '-----------------------------------------------------------
            ' 読み取り(一括)
            '-----------------------------------------------------------
            codeLen = Bt.ScanLib.Control.btScanGetStringSize()
            If codeLen <= 0 Then
                disp = "btScanGetStringSize error ret[" & codeLen & "]"
                ' MessageBox.Show(disp, "Error")
                GoTo L_END
            End If
            ReDim codedataGet(codeLen - 1)

            ret = Bt.ScanLib.Control.btScanGetString(codedataGet, symbolGet)
            If ret <> LibDef.BT_OK Then
                disp = "btScanGetString error ret[" & ret & "]"
                ' MessageBox.Show(disp, "Error")
                GoTo L_END
            End If
            strCodedata = System.Text.Encoding.GetEncoding(932).GetString(codedataGet, 0, codeLen)
            disp = "Code length    :" & codeLen & vbCrLf & "Symbol     :" & symbolGet & vbCrLf & "Code data :" & strCodedata & vbCrLf
            ' MessageBox.Show(disp, "Read (Batch)")

L_END:
            ret = Bt.ScanLib.Control.btScanDisable()
            If ret <> LibDef.BT_OK Then
                disp = "btScanDisable error ret[" & ret & "]"
                ' MessageBox.Show(disp, "Error")
            End If
        Catch ex As Exception
            ' MessageBox.Show(e.ToString())
        End Try
    End Sub

    Public Delegate Sub ScanEventHandler(ByVal result As String)
End Class
