Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports BTClientLib
Imports System.IO
Imports System.Reflection


Module MainModule
    Public errFlag As Boolean = False
    Public f0 As New frm2000
    Public strIP As String
    Public intPort As Integer
    Public strDatabase As String
    Public dbClient As BtrieveDbClient
    Public PIC_DIR As String = "\FlashDisk\BT_FILES\drv1\LAT\pic\"
    Public LOG_DIR As String = "\FlashDisk\BT_FILES\drv1\LAT\log\"


    Sub Main()

        Dim WKey As Short
        Dim buf As String
        Dim wstrFile As String
        Dim rtn As Integer
        'Dim f0 As frm2000
        Dim f5 As frm2005
        Dim f10 As frm2010
        Dim f15 As frm2015
        Dim f20 As frm2020

        gintStatus = ALL_STATUS.TITLE
        gstrctKDHT.initialize()
        gstrctP2000.initialize()
        gstrctKDHT.strPrgID = G_CONST_PGID_2000

        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"

        strIP = "192.168.1.1"
        intPort = 11433
        strDatabase = "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 sGetIni() Then
            'f0.WarpCtrl1.MessageBox(G_CONST_EMT_FILEIO, G_CONST_EM_FILEIO, MWA_DB_WARNING, MWA_DB_OK)
            sErrLog(gAppName, "Main", "No ini file")
            'f0.WarpCtrl1.Term()
            Exit Sub
        End If


        'DBOpen application
        'If Not sDBInit() Then
        '    'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)
        '    sErrLog(gAppName, "Main", "Cannot get connected to DB")
        '    sFdEnd()
        '    Exit Sub
        'End If

        Try

            Do While (True)

                Select Case gintStatus

                    Case ALL_STATUS.TITLE
                        'f0.Show()
                        f0.ShowDialog()
                    Case ALL_STATUS.NAME
                        f5 = New frm2005
                        'f5.Show()
                        f5.ShowDialog()
                        f5.Dispose()

                    Case ALL_STATUS.LOCATION
                        f10 = New frm2010

                        'f10.Show()
                        f10.ShowDialog()
                        f10.Dispose()

                    Case ALL_STATUS.PART
                        f15 = New frm2015
                        'f15.Show()
                        f15.ShowDialog()
                        f15.Dispose()
                    Case ALL_STATUS.DONE

                        f20 = New frm2020

                        'f20.Show()
                        f20.ShowDialog()
                        f20.Dispose()

                    Case ALL_STATUS.QUIT

                        Exit Do

                End Select

            Loop
            f0.Close()

            f0.Dispose()
            sFdEnd()

            Exit Sub

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

    End Sub
    Private Function sDBInit() As Boolean

        sDBInit = False
        gAppName = fGetAppName()

        'PICS
        If Not fOpenDB(gstrctPicsIni.strMain_PICS2004) Then Exit Function

        sDBInit = True

    End Function
    Private Sub sFdEnd()

        Dim rtn As Integer

        Try

            'If Not IsNothing(gclsDB) Then
            '    gclsDB = Nothing       'Pervasive database related file
            'End If

            'gobjMutex.Close()

            Application.Exit()

            Exit Sub

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

    End Sub
    Private Function sGetIni() As Boolean
        Dim wstrIniName As String = ""

        sGetIni = False

        'PICS.INI, PICSPARAM.INI File not found.
        If fGetIniData(fGetPrgID, wstrIniName) = False Then


#If Not Debug Then
            fFnc_WrpMsg("E", G_CONST_MID_E0010, "081")
#End If
            Exit Function
        End If

        sGetIni = True



    End Function
End Module
