﻿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 frm2010
    Inherits System.Windows.Forms.Form
    Private btScanLib As BtScanLib
    Public Shared _frm2010 As frm2010
    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 M2010_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        SHUTDOWN = 2
        NOTT = 3
        NOTEXIST = 4
        NOTAKI = 5
        NOTLF = 6
        DONE = 7
    End Enum
    Public mint2010status As Integer
    Private Sub frm2010()
        InitializeComponent()
    End Sub

    Private Sub frm2010_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

        Dim wstrImage As String = ""
        Dim rtn As Integer = 0

        _frm2010 = Me
        Try
            mint2010status = M2010_STATUS.INIT

            '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_H2010, WarpBox1)
#End If
            sDisp2010()
            'Me.Close()

            'Exit Sub
        Catch ex As Exception
            gintStatus = ALL_STATUS.QUIT
            sErrLog(gAppName, "frm2010_Load", ex.ToString)
            Me.Close()
        End Try
    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 frm2010_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 btScanLib_OnScan(ByVal result As String)
        Dim buf As String = result
        Dim rtn As Integer = 0

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

                fFnc_WrpMsg("E", G_CONST_MID_E2E15)
#End If
                Exit Sub
            End If

            rtn = fProcInput(buf)

            If rtn = 0 Then
                Select Case mint2010status
                    Case M2010_STATUS.INIT

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

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

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

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

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

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

                    Case M2010_STATUS.NOTT              'TศOศฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                        sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E15", G_CONST_BC_LOC, buf)
#If Not Debug Then
                        fFnc_WrpMsg("E", G_CONST_MID_E2E15)
#End If
                    Case M2010_STATUS.NOTEXIST              'Qฦรฐภษ–ณขฬลดืฐาฏพฐผ
                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                        fFnc_WrpMsg("E", G_CONST_MID_E2E20)
#End If
                        '                            Case M2010_STATUS.NOTAKI            '๓ซนฐผฎล–ณขฬลดืฐาฏพฐผ
                        '                                sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E25", "Scanned location code should be available", "not available")
                        '#If Not Debug Then
                        '                                fFnc_WrpMsg("E", G_CONST_MID_E2E25)
                        '#End If
                        '                            Case M2010_STATUS.NOTLF             'ฬุฐนฐผฎล–ณขฬลดืฐาฏพฐผ
                        '                                sOutputLog(gstrctPicsIni.strOutput_KDPA, "KDPA2E05", "Scanned location code should be free location", "not free location")
                        '#If Not Debug Then
                        '                                fFnc_WrpMsg("E", G_CONST_MID_E2E50)
                        '#End If
                    Case M2010_STATUS.DONE              'ณํษQฦลซฝ

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

                        gintStatus = ALL_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
            sErrLog(gAppName, "sMain", ex.ToString)
        End Try
    End Sub
    '/*
    '|| Function Name: fProcInput
    '|| Version: 
    '*/
    Private Function fProcInput(ByVal pstrInput As String) As Integer

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

        fProcInput = -1


        Try

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

                mint2010status = M2010_STATUS.MENUBACK
                fProcInput = 0
                Exit Function

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

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

                rtn = pstrInput.Length
                If rtn <> 9 Then                    '9…ศOศ็ฮ
                    mint2010status = M2010_STATUS.NOTT
                    fProcInput = 0
                    Exit Function
                End If

            Else

                mint2010status = M2010_STATUS.NOTT
                fProcInput = 0
                Exit Function

            End If

            '๕๐Jnตท
            wstrBhk = pstrInput.Substring(1, 3)
            wstrLctnStart = RTrim(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
                mint2010status = M2010_STATUS.NOTEXIST
                fProcInput = 0
                Exit Function

            Else
                Exit Function
            End If

            mint2010status = M2010_STATUS.DONE
            fProcInput = 0


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



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

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

        'ElseIf wintCnt = 0 Then

        '    mint2010status = M2010_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) = gstrctP2000.strCenterCode
        'wstrKeyValue(1) = gstrctP2000.strBhk
        'wstrKeyValue(2) = gstrctP2000.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 (gstrctP2000.strCenterCode = .Item("CenterCode") And gstrctP2000.strBhk = .Item("Bhk") And gstrctP2000.strLctnStart = .Item("Location") And "999999" = .Item("Product")) Then

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

        '            gstrctP2000.strLctnStart = .Item("Location")
        '            mint2010status = M2010_STATUS.DONE

        '        Else

        '            mint2010status = M2010_STATUS.NOTEXIST

        '        End If

        '    End With
        'ElseIf wintRet = 9 Then

        '    mint2010status = M2010_STATUS.NOTEXIST

        'Else

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

        'gclsDB.fCloseRecord("LOCATION1")
        'mint2010status = M2010_STATUS.DONE
        'fProcInput = 0

        'Catch ex As Exception
        '    'gintStatus = ALL_STATUS.QUIT
        '    sErrLog(gAppName, "fProcInput", ex.ToString)
        'End Try
    End Function
    '/*
    '|| Product Name: FsDisp2010
    '|| Version: 2010
    '*/
    Private Sub sDisp2010()
        wfLocation.Text = ""
        wfTitle.Text = gstrctKDHT.strTitle
        wfName.Text = gstrctKDHT.strUserName
    End Sub

    Private Sub frm2010_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