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
Imports Bt.ScanLib
'Imports KDPA2000.BtScanLib
Imports BTClientLib
Imports System.IO
Imports System.Reflection
Imports BtScan2

Public Class frm2005
    Inherits System.Windows.Forms.Form
    Private btScanLib As BtScanLib
    Public Shared _frm2005 As frm2005
    Private scanHandler As Boolean = False
    'Private dbClient As BtrieveDbClient
    Private tmpStr As List(Of Dictionary(Of String, String))
    Private affected_rows As Integer
#Region "Original Code"
    'Public Sub New()
    '    MyBase.New()
    '    InitializeComponent()
    'End Sub
    'Public Enum M2005_STATUS As Integer
    '    INIT = 0
    '    MENUBACK = 1
    '    NOTJ = 2
    '    NOT7 = 3
    '    NOTEXIST = 4
    '    DONE = 5
    'End Enum
    'Public mint2005status As Integer
    'Private Sub frm2005_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '    'Dim wstrImage As String = ""
    '    'Dim rtn As Integer

    '    _frm2005 = Me

    '    '        Try

    '    '            mint2005status = M2005_STATUS.INIT

    '    '            'rtn = WarpFrame1.Open()

    '    '            If (rtn <> 0) Then
    '    '                Err.Raise(vbObjectError + 1, gAppName, "WarpError")
    '    '            End If

    '    '#If Not Debug Then
    '    '            sSetImgFName(G_CONST_MID_H2005, WarpBox1)
    '    '#End If
    '    '            sDisp2005()

    '    '            sMain()

    '    '            'rtn = WarpFrame1.Close()
    '    '            If (rtn <> 0) Then
    '    '                Err.Raise(vbObjectError + 1, gAppName, "WarpError")
    '    '            End If

    '    '            Me.Close()

    '    '            Exit Sub

    '    '        Catch ex As Exception
    '    '            gintStatus = ALL_STATUS.QUIT
    '    '            sErrLog(gAppName, "frm2005_Load", ex.ToString)
    '    '        End Try
    'End Sub
    'Private Sub btScanLib_OnScan(ByVal result As String)
    '    Dim location_id As String = result

    '    wfKey.Text = location_id
    'End Sub
    'Public Sub sMain()

    '    '        Dim WKey As Short
    '    '        Dim buf As String
    '    '        Dim rtn As Integer

    '    '        Try

    '    '            ' ƒtƒŒ[ƒ€‚Ìˆ—‚ð‚±‚±‚É‹Lq‚µ‚Ü‚·B
    '    '            Do While (True)

    '    '                ' ƒR[ƒh“ü—Í
    '    '                WKey = WarpFrame1.Input(wfKey)

    '    '                If WKey = WKY_ENT Then

    '    '                    'fSakusei2()

    '    '                ElseIf WKey = WKY_BCR Then

    '    '                    ''ÊÞ°º°ÄÞ“ü—Í‚ÌŽæ“¾
    '    '                    'rtn = WarpHWBcr1.Get(buf)

    '    '                    Bt.SysLib.Device.btSetTouchpanelEnable(1)

    '    '                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.OK)
    '    '                    If (rtn <> 0) Then
    '    '                        Err.Raise(vbObjectError + 1, gAppName, "WarpError")
    '    '                    End If

    '    '                    'Áª¯¸ÃÞ¨¼Þ¯Ä
    '    '                    If (Not fCheckCode(buf)) Then
    '    '                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
    '    '#If Not Debug Then
    '    '                        fFnc_WrpMsg("E", G_CONST_MID_E2E05)
    '    '#End If
    '    '                        Exit Do
    '    '                    End If

    '    '                    '“ü—Íˆ—
    '    '                    rtn = fProcInput(buf)

    '    '                    If rtn = 0 Then
    '    '                        Select Case mint2005status
    '    '                            Case M2005_STATUS.INIT

    '    '                                gintStatus = ALL_STATUS.LOCATION

    '    '                            Case M2005_STATUS.MENUBACK          'HTCTRL.ExeName‚ðÁ‹Ž‚µ‚ÄI—¹

    '    '                                sFinishApp()
    '    '                                gintStatus = ALL_STATUS.QUIT

    '    '                            Case M2005_STATUS.NOT7              '7Œ…ˆÈŠO‚È‚Ì‚Å´×°Ò¯¾°¼Þ
    '    '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
    '    '                                sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E05", G_CONST_BC_USER, buf)
    '    '#If Not Debug Then
    '    '                                rtn = fFnc_WrpMsg("E", G_CONST_MID_E2E05)
    '    '#End If
    '    '                            Case M2005_STATUS.NOTJ              'JˆÈŠO‚È‚Ì‚Å´×°Ò¯¾°¼Þ
    '    '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
    '    '                                sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E05", G_CONST_BC_USER, buf)
    '    '#If Not Debug Then
    '    '                                fFnc_WrpMsg("E", G_CONST_MID_E2E05)
    '    '#End If
    '    '                            Case M2005_STATUS.NOTEXIST          'ŽQÆÃÞ°À‚É–³‚¢‚Ì‚Å´×°Ò¯¾°¼Þ
    '    '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
    '    '#If Not Debug Then
    '    '                                fFnc_WrpMsg("E", G_CONST_MID_E2E10)
    '    '#End If
    '    '                            Case M2005_STATUS.DONE              '³í‚ÉŽQÆ‚Å‚«‚½
    '    '                                gintStatus = ALL_STATUS.LOCATION

    '    '                        End Select

    '    '                    Else

    '    '                        f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)

    '    '                    End If
    '    '                    Exit Do

    '    '                ElseIf WKey = WKY_F1 Then


    '    '                ElseIf WKey = WKY_F2 Then
    '    '                    gintStatus = ALL_STATUS.QUIT
    '    '                    Exit Do

    '    '                ElseIf WKey = WKY_ERROR Then

    '    '                    f0.WarpCtrl1.MessageBox(G_CONST_EMT_WARP, G_CONST_EM_WARP, MWA_DB_WARNING, MWA_DB_OK)


    '    '                End If

    '    '            Loop

    '    '            Exit Sub

    '    '        Catch ex As Exception
    '    '            'gintStatus = ALL_STATUS.QUIT
    '    '            sErrLog(gAppName, "sMain", ex.ToString)
    '    '        End Try

    'End Sub
    'Private Sub sDisp2005()

    '    ''wfName.Puts(gstrctKDHT.strUserName)
    '    'wfName.Puts("")
    '    wfName.Text = ""
    'End Sub
    'Private Function fProcInput(ByVal pstrInput As String) As Integer

    '    Dim rtn As Integer = 0
    '    Dim wstrEmplCode As String = ""
    '    Dim wintCnt As Integer = 0
    '    Dim wintCnt2 As Integer = 0
    '    Dim wstrWhere As String = ""
    '    Dim wstrKey(0) As String, wstrKeyValue(0) As String

    '    fProcInput = -1

    '    Try

    '        If RTrim(pstrInput) = "OCMENUBACK" Then             'OCMENUBACK‚È‚ç‚Î

    '            mint2005status = M2005_STATUS.MENUBACK
    '            fProcInput = 0
    '            Exit Function

    '        ElseIf pstrInput.StartsWith("J") Then        'æ“ª‚ªJ‚È‚ç‚Î

    '            rtn = pstrInput.Length
    '            If rtn <> 7 Then                    '7Œ…ˆÈŠO‚È‚ç‚Î
    '                mint2005status = M2005_STATUS.NOT7
    '                fProcInput = 0
    '                Exit Function
    '            End If

    '        Else

    '            mint2005status = M2005_STATUS.NOTJ
    '            fProcInput = 0
    '            Exit Function

    '        End If

    '        'ŒŸõ‚ðŠJŽn‚µ‚Ü‚·
    '        wstrEmplCode = Mid(pstrInput, 2)
    '        wstrWhere = ""
    '        wstrWhere = "@EmplCode = " & wstrEmplCode

    '        wintCnt = gclsDB.fGetData(G_CONST_TBL_PICSUSER, G_CONST_KEY_PICSUSERKEY00, wstrWhere, "USER1")
    '        If wintCnt > 0 Then

    '            With gclsDB.Extended("USER1")
    '                gstrctKDHT.strUserNo = .Item("EmplCode")
    '                gstrctKDHT.strUserName = .Item("EmplName")

    '                gclsDB.Setcolumn = ("EmplCode")                 '“o˜^/XV‘ÎÛ—ñ–¼
    '                gclsDB.SetData = (gstrctKDHT.strUserNo)     '“o˜^/XVƒf[ƒ^

    '                wstrKey(0) = "HTNumber"
    '                'wstrKey(1) = "Country"
    '                'wstrKey(2) = "ExeName"
    '                wstrKeyValue(0) = gstrctKDHT.strHdNo
    '                'wstrKeyValue(1) = gstrctKDHT.strCountry
    '                'wstrKeyValue(2) = gstrctKDHT.strExeName

    '                If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
    '                    BtLib.Transaction.Abort()

    '                End If

    '                fProcInput = 0
    '                mint2005status = M2005_STATUS.DONE

    '            End With

    '        ElseIf wintCnt = 0 Then

    '            mint2005status = M2005_STATUS.NOTEXIST
    '            fProcInput = 0
    '            gclsDB.fCloseRecord("USER1")
    '            Exit Function

    '        ElseIf wintCnt = -1 Then

    '            gclsDB.fCloseRecord("USER1")
    '            Exit Function

    '        End If

    '        gclsDB.fCloseRecord("USER1")
    '        fProcInput = 0

    '        Exit Function

    '    Catch ex As Exception
    '        'gintStatus = ALL_STATUS.QUIT
    '        sErrLog(gAppName, "fProcInput", ex.ToString)
    '    End Try

    'End Function
    'Public Sub sSetImgFName(ByVal pMID As String, ByRef pWarpBox As PictureBox, Optional ByVal pintFlag As Integer = 0)
    '    Dim wstrImgFile As String
    '    Dim wMsg As New clsWrpMsg

    '    If pintFlag = 0 Then
    '        wstrImgFile = G_CONST_IMG_DIR & "H" & gstrctKDHT.strCountry & "-" & pMID & G_CONST_IMG_EXT
    '    Else
    '        wstrImgFile = G_CONST_IMG_DIR & "E" & gstrctKDHT.strCountry & "-" & pMID & G_CONST_IMG_EXT
    '    End If

    '    'pWarpBox.SetImage(wstrImgFile)
    '    pWarpBox.Image = Image.FromHbitmap(wstrImgFile)

    'End Sub
#End Region
    Public Enum M2005_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        NOTJ = 2
        NOT7 = 3
        NOTEXIST = 4
        DONE = 5
    End Enum
    Public mint2005status As Integer
    Private Sub frm2005()
        InitializeComponent()
    End Sub
    Private Sub frm2005_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '// Case QVGA -> resolution to 1/2
        If System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width = 240 Then
            Call resolution.VGAtoQVGA(Me)
        End If


        _frm2005 = Me


        Dim wstrImage As String
        Dim rtn As Integer

        Try

            mint2005status = M2005_STATUS.INIT

            ' t[€ฬI[v“
            'rtn = WarpFrame1.Open()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If

            Dim fileloc As String = "Setting.ini"

            Dim appPath As String = fgetAppPath() ' Assuming fgetAppPath() retrieves the application directory
            Dim filePath As String = Path.Combine(appPath, fileloc)

            Dim i As Integer = 0
            Dim strIP As String = "192.168.1.1"
            Dim intPort As Integer = 11433
            Dim strDatabase As String = "demodata"

            If File.Exists(filePath) Then

                Dim sr As StreamReader = New StreamReader(filePath)
                Dim s As String = sr.ReadToEnd()
                Dim words As String() = s.Split(New Char() {","c})

                Dim word As String

                For Each word In words

                    If i = 0 Then
                        strIP = word
                    ElseIf i = 1 Then

                        If IsNumeric(word) Then
                            intPort = Convert.ToInt32(word)
                        End If

                    ElseIf i = 2 Then
                        strDatabase = word
                    End If

                    i = i + 1

                Next

            End If

            dbClient = New BtrieveDbClient(strIP, intPort, strDatabase)


#If Not Debug Then
            'sSetImgFName(G_CONST_MID_H2005, WarpBox1)
#End If
            sDisp2005()
            ' tH[€EC“ ภs
            sMain()

            ' t[€ฬN[Y
            'rtn = WarpFrame1.Close()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If

            ' tH[€EN[Y
            'Me.Close()

            Exit Sub

        Catch ex As Exception
            gintStatus = ALL_STATUS.QUIT
            sErrLog(gAppName, "frm2005_Load", ex.ToString)
            Me.Close()
        End Try

    End Sub
    Private Sub frm2005_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated


        ' Enable BTScanLib
        If Not scanHandler Then
            btScanLib = btScanLib.getInstance()
            AddHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
            scanHandler = True
            btScanLib.Debug = False
            btScanLib.Enable()
            'MessageBox.Show("1")
        End If


    End Sub


    Public Function fgetAppPath() As String
        ' Get the directory of the currently executing assembly (the application)
        Return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
    End Function

    Private Sub btScanLib_OnScan(ByVal result As String)
        Dim buf As String = result
        Dim rtn As Integer

        If (Not fCheckCode(buf)) Then
            sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)

#If Not Debug Then
            fFnc_WrpMsg("E", G_CONST_MID_E2E05)
#End If
            Exit Sub
        End If

        'MessageBox.Show(buf)

        'Invalid input value.
        rtn = fProcInput(buf)

        If rtn = 0 Then
            Select Case mint2005status
                Case M2005_STATUS.INIT

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = ALL_STATUS.LOCATION
                    Me.Close()
                    Exit Sub

                Case M2005_STATUS.MENUBACK          'HTCTRL.ExeName Parameter is incorrect

                    'sFinishApp()
                    gintStatus = ALL_STATUS.QUIT
                    Me.Close()
                    Exit Sub

                Case M2005_STATUS.NOT7
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E05", G_CONST_BC_USER, buf)
#If Not Debug Then
                    rtn = fFnc_WrpMsg("E", G_CONST_MID_E2E05)
#End If
                    Exit Sub
                Case M2005_STATUS.NOTJ

                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E05", G_CONST_BC_USER, buf)
#If Not Debug Then
                    fFnc_WrpMsg("E", G_CONST_MID_E2E05)
#End If
                    Exit Sub
                Case M2005_STATUS.NOTEXIST
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                    fFnc_WrpMsg("E", G_CONST_MID_E2E10)
#End If
                    Exit Sub
                Case M2005_STATUS.DONE

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = ALL_STATUS.LOCATION
                    Me.Close()
                    Exit Sub
            End Select

        Else
            'f0.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)
            MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            'f0.ShowDialog()
        End If

    End Sub
    '/*
    '|| Function Name: fProcInput
    '|| Version      : Invalid input value
    '*/
    Private Function fProcInput(ByVal pstrInput As String) As Integer

        Dim rtn As Integer
        Dim wstrEmplCode As String
        Dim wintCnt As Integer
        Dim wintCnt2 As Integer
        Dim wstrWhere As String
        Dim wstrKey(0) As String, wstrKeyValue(0) As String

        fProcInput = -1

        Try

            If RTrim(pstrInput) = "OCMENUBACK" Then             'OCMENUBACKศ็ฮ

                mint2005status = M2005_STATUS.MENUBACK
                fProcInput = 0
                Exit Function

                'ElseIf pstrInput.StartsWith("J") Then 'ๆ“ชชJศ็ฮ
            ElseIf Mid(pstrInput, 1, 1) = "J" Then

                rtn = pstrInput.Length
                If rtn <> 7 Then                    '7…ศOศ็ฮ
                    mint2005status = M2005_STATUS.NOT7
                    fProcInput = 0
                    Exit Function
                End If


            Else

                mint2005status = M2005_STATUS.NOTJ
                fProcInput = 0
                Exit Function

            End If

            '๕๐Jnตท
            wstrEmplCode = Mid(pstrInput, 2)
            wstrWhere = ""
            wstrWhere = "EmplCode = '" & wstrEmplCode & "'"

            'MessageBox.Show("SELECT * FROM " + G_CONST_TBL_PICSUSER + " WHERE " + wstrWhere)

            tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_PICSUSER + " WHERE " + wstrWhere)

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                If values.TryGetValue("EmplCode", result) Then
                    gstrctKDHT.strUserNo = result
                End If
                If values.TryGetValue("EmplName", result) Then
                    gstrctKDHT.strUserName = result
                End If

                wstrKey(0) = "HTNumber"
                'wstrKey(1) = "Country"
                'wstrKey(2) = "ExeName"
                wstrKeyValue(0) = gstrctKDHT.strHdNo
                'wstrKeyValue(1) = gstrctKDHT.strCountry
                'wstrKeyValue(2) = gstrctKDHT.strExeName


                affected_rows = dbClient.Execute("UPDATE " + G_CONST_TBL_HTCTRL + " SET EmplCode='" + gstrctKDHT.strUserNo + "' WHERE " + wstrKey(0) + "=" + wstrKeyValue(0))

                fProcInput = 0
                mint2005status = M2005_STATUS.DONE

            ElseIf tmpStr.Count = 0 Then
                mint2005status = M2005_STATUS.NOTEXIST
                fProcInput = 0
                Exit Function

            Else
                Exit Function
            End If
            fProcInput = 0

            Exit Function

        Catch ex As Exception
            'gintStatus = ALL_STATUS.QUIT
            sErrLog(gAppName, "fProcInput", ex.ToString)
        End Try


        'wintCnt = gclsDB.fGetData(G_CONST_TBL_PICSUSER, G_CONST_KEY_PICSUSERKEY00, wstrWhere, "USER1")
        'If wintCnt > 0 Then

        '    With gclsDB.Extended("USER1")
        '        gstrctKDHT.strUserNo = .Item("EmplCode")
        '        gstrctKDHT.strUserName = .Item("EmplName")

        '        gclsDB.Setcolumn = ("EmplCode")                 '“o^/XV‘ฮ—๑–ผ
        '        gclsDB.SetData = (gstrctKDHT.strUserNo)     '“o^/XVf[^

        '        wstrKey(0) = "HTNumber"
        '        'wstrKey(1) = "Country"
        '        'wstrKey(2) = "ExeName"
        '        wstrKeyValue(0) = gstrctKDHT.strHdNo
        '        'wstrKeyValue(1) = gstrctKDHT.strCountry
        '        'wstrKeyValue(2) = gstrctKDHT.strExeName

        '        If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
        '            BtLib.Transaction.Abort()

        '        End If

        '        fProcInput = 0
        '        mint2005status = M2005_STATUS.DONE

        '    End With

        'ElseIf wintCnt = 0 Then

        '    mint2005status = M2005_STATUS.NOTEXIST
        '    fProcInput = 0
        '    gclsDB.fCloseRecord("USER1")
        '    Exit Function

        'ElseIf wintCnt = -1 Then

        '    gclsDB.fCloseRecord("USER1")
        '    Exit Function
        'End If

        'gclsDB.fCloseRecord("USER1")
        'fProcInput = 0

        'Exit Function

        'Catch ex As Exception
        '    'gintStatus = ALL_STATUS.QUIT
        '    sErrLog(gAppName, "fProcInput", ex.ToString)
        'End Try

    End Function

    '/*
    '|| Product Name: FsDisp2005
    '|| Version: 2005
    '*/
    Private Sub sDisp2005()
        'wfName.Puts(gstrctKDHT.strUserName)
        wfName.Text = ""
    End Sub
    Public Sub sMain()

    End Sub

    Private Sub frm2005_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode.ToString()
            Case "F1"
            Case "F2"
                gintStatus = ALL_STATUS.QUIT
                Me.Close()
                Exit Sub
        End Select
    End Sub

    Private Sub frm2005_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    End Sub
End Class