﻿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 KDPA4000.BtScanLib
Imports BTClientLib
Imports System.IO
Imports System.Reflection
Imports Newtonsoft
Imports BtScan2

Public Class frm4010
    Inherits System.Windows.Forms.Form
    Private btScanLib As BtScanLib
    Public Shared _frm4010 As frm4010
    Private scanHandler As Boolean = False
    'Private dbClient As BtrieveDbClient
    Private tmpStr As List(Of Dictionary(Of String, String))
    Private affected_rows As Integer

    Public Enum M4010_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        SHUTDOWN = 2
        NOTT = 3
        NOTEXIST = 4
        NOTAKI = 5
        NOTLF = 6
        DONE = 7
    End Enum
    Public mint4010status As Integer

    Private Sub frm4010_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()
        End If
    End Sub
    Private Sub frm4010_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

        _frm4010 = Me

        Try
            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

            'MessageBox.Show(strIP & " | " & intPort.ToString() & " | " & strDatabase)

            dbClient = New BtrieveDbClient(strIP, intPort, strDatabase)

        Catch ex As Exception
            sErrLog(gAppName, "frm4010_Load", ex.ToString)
        End Try

    End Sub
    Private Sub btScanLib_OnScan(ByVal result As String)
        Dim buf As String = result
        Dim rtn As Integer = 0
        Try
            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_E4E15)
#End If
                Exit Sub
            End If

            rtn = fProcInput(buf)

            If rtn = 0 Then
                Select Case mint4010status
                    Case M4010_STATUS.INIT

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

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

                    Case M4010_STATUS.MENUBACK          'าฦญฐสฏธ

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

                        gintStatus = FREE_STATUS.NAME
                        Me.Close()
                        Exit Sub

                    Case M4010_STATUS.SHUTDOWN          'HTCTRL.ExeName๐มตฤI—น
                        'sFinishApp()
                        gintStatus = ALL_STATUS.QUIT
                        Me.Close()
                        Exit Sub

                    Case M4010_STATUS.NOTT              'TศOศฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                        sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA4E15", G_CONST_BC_LOC, buf)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E4E15)
#End If
                    Case M4010_STATUS.NOTEXIST              'Qฦรฐภษ–ณขฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E4E20)
#End If
                    Case M4010_STATUS.NOTAKI            '๓ซนฐผฎล–ณขฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E2E25)
#End If
                    Case M4010_STATUS.NOTLF             'ฬุฐนฐผฎล–ณขฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E2E50)
#End If
                    Case M4010_STATUS.DONE              'ณํษQฦลซฝ

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

                        gintStatus = FREE_STATUS.PART
                        Me.Close()
                        Exit Sub

                End Select

            Else
                'f0.WarpCtrl1.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.Show()
            End If
            Exit Sub
        Catch ex As Exception

        End Try
    End Sub
    Private Function fProcInput(ByVal pstrInput As String) As Integer

        Dim rtn As Integer
        Dim wstrBhk As String
        Dim wstrLctnStart As String
        Dim wstrAkiKB As String
        Dim wstrLF As String
        Dim wintCnt As Integer
        Dim wintRet As Integer
        Dim wstrWhere As String
        Dim wstrKey(3) As String
        Dim wstrKeyValue(3) As String

        fProcInput = -1

        Try

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

                mint4010status = M4010_STATUS.MENUBACK
                fProcInput = 0
                Exit Function

            ElseIf pstrInput = "OCSHUTDOWN" Then        'OCSHUTDOWNศ็ฮ
                mint4010status = M4010_STATUS.SHUTDOWN
                fProcInput = 0
                Exit Function

            ElseIf pstrInput.StartsWith("T") Then        'ๆ“ชชTศ็ฮ

                If pstrInput.Length() <> G_CONST_LLEN_LOC Then                     '9…ศOศ็ฮ
                    mint4010status = M4010_STATUS.NOTT
                    fProcInput = 0
                    Exit Function
                End If

            Else

                mint4010status = M4010_STATUS.NOTT
                fProcInput = 0
                Exit Function

            End If

            '๕๐Jnตท
            wstrBhk = pstrInput.Substring(1, 3)
            wstrLctnStart = pstrInput.Substring(4)
            wstrWhere = ""
            wstrWhere = "Bhk = '" & wstrBhk & "'"

            tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_RL2RL + " 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("Bhk", result) Then
                    gstrctP2000.strBhk = RTrim(result)
                End If
                If values.TryGetValue("CenterCode", result) Then
                    gstrctP2000.strCenterCode = RTrim(result)
                End If
                gstrctP2000.strLctnStart = wstrLctnStart

            ElseIf tmpStr.Count = 0 Then
                mint4010status = M4010_STATUS.NOTEXIST
                fProcInput = 0
                Exit Function

            Else
                Exit Function
            End If

            mint4010status = M4010_STATUS.DONE
            fProcInput = 0

            wstrKey(0) = "CenterCode"
            wstrKey(1) = "Bhk"
            wstrKey(2) = "Location"
            wstrKey(3) = "Product"
            wstrKeyValue(0) = gstrctP4000.strCenterCode
            wstrKeyValue(1) = gstrctP4000.strBhk
            wstrKeyValue(2) = gstrctP4000.strLctnStart
            wstrKeyValue(3) = "999999"

            wstrWhere = ""
            wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
            wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
            wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"
            wstrWhere &= " and " & wstrKey(3) & " = '" & wstrKeyValue(3) & "'"

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

            values = New Dictionary(Of String, String)

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                Dim sBhk As String = Nothing
                Dim sCenterCode As String = Nothing
                Dim sLocation As String = Nothing
                Dim sProduct As String = Nothing

                If values.TryGetValue("Bhk", result) Then
                    sBhk = RTrim(result)
                End If
                If values.TryGetValue("CenterCode", result) Then
                    sCenterCode = RTrim(result)
                End If
                If values.TryGetValue("Location", result) Then
                    sLocation = RTrim(result)
                End If
                If values.TryGetValue("Product", result) Then
                    sProduct = RTrim(result)
                End If


                If (gstrctP4000.strCenterCode = sCenterCode And gstrctP4000.strBhk = sBhk And gstrctP4000.strLctnStart = sLocation And "999999" = sProduct) Then

                    'wstrAkiKB = .Item("AkiKB")
                    'wstrLF = .Item("LF")
                    'If wstrAkiKB = 1 Then
                    '    mint4010status = M4010_STATUS.NOTAKI
                    '    fProcInput = 0
                    '    gclsDB.fCloseRecord("LOCATION1")
                    '    Exit Function
                    'ElseIf wstrLF <> "F" Then
                    '    mint4010status = M4010_STATUS.NOTLF
                    '    fProcInput = 0
                    '    gclsDB.fCloseRecord("LOCATION1")
                    '    Exit Function
                    'End If

                    gstrctP4000.strLctnStart = sLocation
                    mint4010status = M4010_STATUS.DONE

                Else

                    mint4010status = M4010_STATUS.NOTEXIST

                End If



            ElseIf tmpStr.Count = 0 Then
                mint4010status = M4010_STATUS.NOTEXIST
                fProcInput = 0
                Exit Function

            Else
                Exit Function
            End If


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



        '    wstrWhere = "@Bhk = " & wstrBhk

        '    wintCnt = gclsDB.fGetData(G_CONST_TBL_RL2RL, G_CONST_KEY_RL2RLKEY00, wstrWhere, "RL2RL1")
        '    If wintCnt > 0 Then

        '        With gclsDB.Extended("RL2RL1")
        '            gstrctP4000.strBhk = .Item("Bhk")
        '            gstrctP4000.strCenterCode = .Item("CenterCode")
        '            gstrctP4000.strLctnStart = wstrLctnStart

        '        End With

        '    ElseIf wintCnt = 0 Then

        '        mint4010status = M4010_STATUS.NOTEXIST
        '        fProcInput = 0
        '        gclsDB.fCloseRecord("RL2RL1")
        '        Exit Function

        '    ElseIf wintCnt = -1 Then

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

        '    End If
        '    gclsDB.fCloseRecord("RL2RL1")

        '    'ณ็ษ๕ตท
        '    wstrKey(0) = "CenterCode"
        '    wstrKey(1) = "Bhk"
        '    wstrKey(2) = "Location"
        '    wstrKey(3) = "Product"
        '    wstrKeyValue(0) = gstrctP4000.strCenterCode
        '    wstrKeyValue(1) = gstrctP4000.strBhk
        '    wstrKeyValue(2) = gstrctP4000.strLctnStart
        '    wstrKeyValue(3) = "999999"

        '    wintRet = gclsDB.fOpenRecord(G_CONST_TBL_LOCATION, G_CONST_KEY_LOCATIONKEY00, "LOCATION1", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)

        '    If wintRet = 0 Then

        '        With gclsDB.Records("LOCATION1")

        '            If (gstrctP4000.strCenterCode = .Item("CenterCode") And gstrctP4000.strBhk = .Item("Bhk") And gstrctP4000.strLctnStart = .Item("Location") And "999999" = .Item("Product")) Then

        '                'wstrAkiKB = .Item("AkiKB")
        '                'wstrLF = .Item("LF")
        '                'If wstrAkiKB = 1 Then
        '                '    mint4010status = M4010_STATUS.NOTAKI
        '                '    fProcInput = 0
        '                '    gclsDB.fCloseRecord("LOCATION1")
        '                '    Exit Function
        '                'ElseIf wstrLF <> "F" Then
        '                '    mint4010status = M4010_STATUS.NOTLF
        '                '    fProcInput = 0
        '                '    gclsDB.fCloseRecord("LOCATION1")
        '                '    Exit Function
        '                'End If

        '                gstrctP4000.strLctnStart = .Item("Location")
        '                mint4010status = M4010_STATUS.DONE

        '            Else

        '                mint4010status = M4010_STATUS.NOTEXIST

        '            End If

        '        End With

        '    ElseIf wintRet = 9 Then

        '        mint4010status = M4010_STATUS.NOTEXIST

        '    Else

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

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

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

        'End Try

    End Function

    Private Sub frm4010_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
End Class