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 Module1

    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\"

    ' フラグ
    Public errFlag As Boolean = False

    ' フォーム・インスタンス
    Public f0 As New frm6000

    ' プロジェクト・メイン
    Sub Main()

        Dim WKey As Short
        Dim buf As String
        Dim wstrFile As String
        Dim rtn As Integer
        ' Dim f0 As New frm2000
        Dim f5 As frm6005
        Dim f10 As frm6010
        Dim f15 As frm6015
        Dim f20 As frm6020



        gintStatus = RETRY_STATUS.TITLE
        'gintStatus = ALL_STATUS.TITLE

        gstrctKDHT.initialize()
        gstrctKDHT.strPrgID = G_CONST_PGID_6000

        ' コントロール・初期処理
        'f0.WarpCtrl1.Init()

        ' バーコード・リーダー等の初期設定を記述します。
        'f0.WarpHWBcr1.Setup()

        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)


        '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()
            Exit Sub
        End If

        'DBOpenチェック
        '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()
        '    sFdEnd()
        '    Exit Sub
        'End If

        Try

            ' メインループ
            Do While (True)

                Select Case gintStatus


                    Case RETRY_STATUS.TITLE
                        ' フォーム・ロード
                        'f0.Show()
                        f0.ShowDialog()

                    Case RETRY_STATUS.NAME

                        ' フォーム作成
                        f5 = New frm6005

                        ' フォーム・ロード
                        'f5.Show()
                        f5.ShowDialog()
                        f5.Dispose()

                    Case RETRY_STATUS.CARTON
                        ' フォーム作成
                        f10 = New frm6010

                        ' フォーム・ロード
                        'f10.Show()
                        f10.ShowDialog()
                        f10.Dispose()
                    Case RETRY_STATUS.RETRY1
                        ' フォーム作成
                        f15 = New frm6015

                        ' フォーム・ロード
                        'f15.Show()
                        f15.ShowDialog()
                        f15.Dispose()

                    Case RETRY_STATUS.RETRY2
                        ' フォーム作成
                        f20 = New frm6020

                        ' フォーム・ロード
                        'f20.Show()
                        f20.ShowDialog()
                        f20.Dispose()

                    Case RETRY_STATUS.QUIT
                        Exit Do

                End Select

            Loop

            ' フレームのクローズ
            'f0.WarpFrame1.Close()

            'f0.WarpCtrl1.Term()

            ' フォーム・クローズ
            f0.Close()

            f0.Dispose()

            ' アプリケーションの終了
            sFdEnd()

            Exit Sub

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

    End Sub

    '/*
    '|| 関数名：sGetIni
    '|| 概要  ：INI取得処理
    '*/
    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

    '/*
    '|| 関数名：sDBInit
    '|| 概要  ：DBオープン処理
    '*/
    Private Function sDBInit() As Boolean

        sDBInit = False
        gAppName = fGetAppName()

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

        sDBInit = True

    End Function

    '/*
    '|| 関数名：sFdEnd
    '|| 概要  ：終了処理
    '*/
    Private Sub sFdEnd()

        Dim rtn As Integer

        Try

            'If Not IsNothing(gclsDB) Then
            '    gclsDB = Nothing       'Perbasive接続用のインスタンス破棄
            'End If

            'gobjMutex.Close()

            Application.Exit()

            Exit Sub

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

    End Sub


End Module
