﻿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 frm4000
    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 frm4000
        Dim f5 As frm4005
        Dim f10 As frm4010
        Dim f15 As frm4015
        Dim f20 As frm4020

        gintStatus = FREE_STATUS.TITLE
        gstrctKDHT.initialize()
        gstrctP4000.initialize()
        gstrctKDHT.strPrgID = G_CONST_PGID_4000

        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)

        '' ƒRƒ“ƒgƒ[ƒ‹E‰Šúˆ—
        'f0.WarpCtrl1.Init()

        '' ƒo[ƒR[ƒhEƒŠ[ƒ_[“™‚Ì‰ŠúÝ’è‚ð‹Lq‚µ‚Ü‚·B
        'f0.WarpHWBcr1.Setup()


        'INIŽæ“¾ˆ—
        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() 'PowerOff
            Exit Sub
        End If


        'DBOpenƒ`ƒFƒbƒN
        '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")
        '    'f0.WarpCtrl1.Term() 'PowerOff
        '    sFdEnd()
        '    Exit Sub
        'End If

        Try

            ' ƒƒCƒ“ƒ‹[ƒv
            Do While (True)

                Select Case gintStatus

                    Case FREE_STATUS.TITLE
                        ' ƒtƒH[ƒ€Eƒ[ƒh
                        'f0.Show()
                        f0.ShowDialog()

                    Case FREE_STATUS.NAME
                        ' ƒtƒH[ƒ€ì¬
                        f5 = New frm4005

                        ' ƒtƒH[ƒ€Eƒ[ƒh
                        'f5.Show()
                        f5.ShowDialog()
                        f5.Dispose()

                    Case FREE_STATUS.LOCATION
                        ' ƒtƒH[ƒ€ì¬
                        f10 = New frm4010

                        ' ƒtƒH[ƒ€Eƒ[ƒh
                        'f10.Show()
                        f10.ShowDialog()
                        f10.Dispose()

                    Case FREE_STATUS.PART
                        ' ƒtƒH[ƒ€ì¬
                        f15 = New frm4015

                        ' ƒtƒH[ƒ€Eƒ[ƒh
                        'f15.Show()
                        f15.ShowDialog()
                        f15.Dispose()

                    Case FREE_STATUS.DONE
                        ' ƒtƒH[ƒ€ì¬
                        f20 = New frm4020

                        ' ƒtƒH[ƒ€Eƒ[ƒh
                        'f20.Show()
                        f20.ShowDialog()
                        f20.Dispose()

                    Case FREE_STATUS.QUIT
                        Exit Do

                End Select

            Loop

            '' ƒtƒŒ[ƒ€‚ÌƒNƒ[ƒY
            'f0.WarpFrame1.Close()

            'f0.WarpCtrl1.Term()

            '' ƒtƒH[ƒ€EƒNƒ[ƒY
            f0.Close()

            f0.Dispose()

            ' ƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚ÌI—¹
            sFdEnd()

            Exit Sub

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

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

        sGetIni = False

        'PICS.INI, PICSPARAM.INI Ì§²Ù“Ç‚Ýž‚Ý
        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

    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       'PerbasiveÚ‘±—p‚ÌƒCƒ“ƒXƒ^ƒ“ƒX”jŠü
            'End If

            'gobjMutex.Close()

            Application.Exit()

            Exit Sub

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

    End Sub
End Module
