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 Bt.SysLib
Imports Bt

Module modPublicHT
    Public Function fFnc_WrpMsg(ByVal pStatus As String, ByVal pMID As String, Optional ByVal pCountry As String = "ND", Optional ByVal wintSelect As Integer = 1) As Integer
        Dim wstrImgFile As String = ""
        Dim wMsg As New clsWrpMsg

        If pCountry = "ND" Then
            pCountry = gstrctKDHT.strCountry
        End If

        Select Case pStatus
            Case "H"
                wstrImgFile = G_CONST_IMG_DIR & "H" & pCountry & "-" & pMID & G_CONST_IMG_EXT
            Case "E"
                wstrImgFile = G_CONST_IMG_DIR & "E" & pCountry & "-" & pMID & G_CONST_IMG_EXT
            Case "S"
                wstrImgFile = G_CONST_IMG_DIR & "S" & pCountry & "-" & pMID & G_CONST_IMG_EXT
        End Select

        With wMsg
            .pImgFile = wstrImgFile
            .pSelection = wintSelect
            .sMsgBox()
            Return .pRtnCode
        End With
    End Function

    Public Sub sSetImgFName(ByVal pMID As String, ByRef pWarpBox As PictureBox, Optional ByVal pintFlag As Integer = 0)
        Dim wstrImgFile As String
        Dim wMsg As New clsWrpMsg

        If pintFlag = 0 Then
            wstrImgFile = G_CONST_IMG_DIR & "H" & gstrctKDHT.strCountry & "-" & pMID & G_CONST_IMG_EXT
        Else
            wstrImgFile = G_CONST_IMG_DIR & "E" & gstrctKDHT.strCountry & "-" & pMID & G_CONST_IMG_EXT
        End If

        pWarpBox.Image = Image.FromHbitmap(wstrImgFile)

    End Sub

    Public Function fCheckCode(ByRef pstrBarCode As String) As Boolean
        Dim i As Integer
        Dim wintChr() As Integer
        Dim wintSum As Integer
        Dim wintAmari As Integer
        Dim wstrBarCode As String
        Dim wstrDigit As String

        fCheckCode = False

        If Len(pstrBarCode) = 0 Then Exit Function

        wstrBarCode = Mid(pstrBarCode, 2, Len(pstrBarCode) - 2)
        wstrDigit = Mid(pstrBarCode, Len(pstrBarCode), 1)

        '// Áª¯¸ÃÞ¼Þ¯Ä(M43)‚Ì®‡«‚ðŠm‚©‚ß‚é
        ReDim wintChr(Len(wstrBarCode) - 1)
        For i = 1 To Len(wstrBarCode)
            wintChr(i - 1) = wfGetCode(Mid(wstrBarCode, i, 1))
        Next

        wintSum = 0
        For i = 0 To UBound(wintChr)
            wintSum = wintSum + wintChr(i)
        Next

        wintAmari = wintSum Mod 43

        If wfGetValue(wintAmari) <> wstrDigit Then
            'Áª¯¸ÃÞ¼Þ¯Ä‚ª•s³‚Å‚·
            'fFnc_Msg("0304", "3")
            Exit Function
        End If

        pstrBarCode = wstrBarCode
        fCheckCode = True

    End Function

    Private Function wfGetCode(ByVal pstrValue As String) As Integer
        Dim i As Integer

        wfGetCode = -1

        For i = 0 To UBound(mstrChr)
            If mstrChr(i) = pstrValue Then
                wfGetCode = mintChr(i)
                Exit Function
            End If
        Next

    End Function

    Private Function wfGetValue(ByVal pintValue As Integer) As String
        Dim i As Integer

        wfGetValue = -1

        For i = 0 To UBound(mintChr)
            If mintChr(i) = pintValue Then
                wfGetValue = mstrChr(i)
                Exit Function
            End If
        Next

    End Function
    Public Function fstrDivision(ByVal pintCut As Integer, ByVal pstrData As String) As String
        Dim wstrValue As String
        Dim wstrFront As String
        Dim wstrBack As String
        Dim wintData As String
        Dim rut As Integer

        fstrDivision = " "

        Try

            wstrFront = pstrData.Substring(0, pintCut)
            wstrBack = pstrData.Substring(pintCut)

            wstrValue = wstrFront & "-" & wstrBack

            fstrDivision = wstrValue

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

    End Function
    'Public Sub sFinishApp()

    '    Dim rut As Integer
    '    Dim wintCnt As Integer
    '    Dim wstrWhere As String
    '    Dim wstrKey(0) As String, wstrKeyValue(0) As String

    '    Try

    '        BtLib.Transaction.BeginConCurrent()

    '        wstrWhere = "@HTNumber = " & gstrctKDHT.strHdNo
    '        wintCnt = gclsDB.fGetData(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrWhere, "CTRL2")

    '        If wintCnt > 0 Then
    '            gclsDB.fCloseRecord("CTRL2")
    '        ElseIf wintCnt = 0 Then
    '            gclsDB.fCloseRecord("CTRL2")
    '            BtLib.Transaction.Abort()
    '            fFnc_WrpMsg("S", G_CONST_MID_S0010, "001")
    '            Exit Sub
    '        ElseIf wintCnt = -1 Then
    '            gclsDB.fCloseRecord("CTRL2")
    '            BtLib.Transaction.Abort()
    '            fFnc_WrpMsg("S", G_CONST_MID_S0010, "001")
    '            Exit Sub
    '        End If


    '        gclsDB.Setcolumn = ("ExeName,SearchKey")                 '“o˜^/XV‘ÎÛ—ñ–¼
    '        gclsDB.SetData = Space(8) & "," & Space(30)                       '“o˜^/XVƒf[ƒ^

    '        wstrKey(0) = "HTNumber"
    '        wstrKeyValue(0) = gstrctKDHT.strHdNo

    '        If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
    '            BtLib.Transaction.Abort()

    '        End If

    '        BtLib.Transaction.End()


    '    Catch ex As Exception
    '        BtLib.Transaction.Abort()
    '        sErrLog(gAppName, "sFinishApp", ex.ToString)
    '    End Try

    'End Sub
    Function fFTPUpload(ByVal pstrIPAddress As String, ByVal pstrFilePath As String) As Integer
        'Create an instance of the FTP class that is created.
        Dim ff As clsFTP

        fFTPUpload = -1

        Try

            ff = New clsFTP(pstrIPAddress, _
                            "/PICS2004/", _
                            "anonymous", _
                            "", _
                            21)

            'Try to log on to the FTP server.
            If (ff.Login() = True) Then

                'Upload a file from your local hard disk to the FTP site.
                ff.UploadFile(pstrFilePath)

            End If

        Catch ex As System.Exception
            sErrLog(gAppName, "TestFTP", ex.ToString)
        Finally
            'Always close the connection to make sure that there are not any not-in-use FTP connections.
            'Check if you are logged on to the FTP server and then close the connection.
            If ff.flag_bool = True Then
                ff.CloseConnection()
                fFTPUpload = 0
            End If
        End Try
    End Function
    Public Function fstrZeroput(ByVal pintNum As Integer, ByVal pstrData As String) As String
        Dim wstrValue As String = ""
        Dim wintData As String = ""
        Dim rut As Integer

        rut = pstrData.Length

        If pintNum <> rut Then

            wstrValue = pstrData.PadLeft(pintNum, "0")

        ElseIf pintNum = rut Then

            wstrValue = pstrData

        End If

        fstrZeroput = wstrValue

    End Function

    Public Enum BEEP_TYPE
        A = 1
        B = 2
        C = 3
        D = 4
        E = 5
    End Enum

    Public Enum BEEP_SELECTION
        OK = 1
        NG = 2
    End Enum
    Public Sub sBeepSound(ByVal pType As BEEP_TYPE, ByVal pSelection As BEEP_SELECTION)

        Select Case pSelection

            Case BEEP_SELECTION.OK

                Select Case pType

                    Case BEEP_TYPE.A
                        'f0.WarpCtrl1.BeepEx("<-200>")
                        'Bt.SysLib.Device.btSetKeySound()
                        'setSoundOK()

                    Case BEEP_TYPE.B
                        'f0.WarpCtrl1.BeepEx("<-100>-50<-100>")
                        'setSoundOK()

                    Case BEEP_TYPE.C
                        'f0.WarpCtrl1.BeepEx("<-200>-200<-200>")
                        'setSoundOK()

                    Case BEEP_TYPE.D
                        'f0.WarpCtrl1.BeepEx("<LB+200>-200<LB+200>")
                        'setSoundOK()

                    Case BEEP_TYPE.E
                        'f0.WarpCtrl1.BeepEx("<-200><-200>")
                        'setSoundOK()

                End Select

            Case BEEP_SELECTION.NG

                Select Case pType
                    Case BEEP_TYPE.A
                        'f0.WarpCtrl1.BeepEx("<LB+2000>")
                        'setSoundNG()

                    Case BEEP_TYPE.B
                        'f0.WarpCtrl1.BeepEx("LB+200-100LB+200-100LB+200-100LB+200-100LB+200")
                        'setSoundNG()

                    Case BEEP_TYPE.C
                        'f0.WarpCtrl1.BeepEx("<LB+300><LB+300><LB+300><LB+300><LB+300>")
                        'setSoundNG()

                    Case BEEP_TYPE.D
                        'f0.WarpCtrl1.BeepEx("LB+1000")
                        'f0.WarpCtrl1.BeepEx("<-200><-200><-200><-200><-200><-200><-200>")
                        'setSoundNG()

                    Case BEEP_TYPE.E
                        'f0.WarpCtrl1.BeepEx("<LB+1000>")
                        'setSoundNG()

                End Select

        End Select

    End Sub

    Public Sub setSoundNG()

        Dim ret As Int32 = 0
        Dim disp As [String] = ""

        Dim stBuzzerSet As New LibDef.BT_BUZZER_PARAM()
        ' Buzzer control structure (Set)
        ' Set to repeat "500 ms on, 500 ms off" 3 times
        stBuzzerSet.dwOn = 500
        ' Rumble time [ms] (1 to 5000)
        stBuzzerSet.dwOff = 500
        ' Stop time [ms] (0 to 5000)
        stBuzzerSet.dwCount = 3
        ' Rumble count [times] (0 to 100)
        stBuzzerSet.bTone = 16
        ' Musical scale (1 to 16)
        stBuzzerSet.bVolume = 3
        ' Buzzer volume (1 to 3)
        Try
            ' btBuzzer Rumble
            ret = Device.btBuzzer(1, stBuzzerSet)
            If ret <> LibDef.BT_OK Then
                disp = "btBuzzer error ret[" & ret & "]"
                MessageBox.Show(disp, "Error")
                Return
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try


    End Sub

    Public Sub setSoundOK()

        Dim ret As Int32 = 0
        Dim disp As [String] = ""

        Dim stBuzzerSet As New LibDef.BT_BUZZER_PARAM()
        ' Buzzer control structure (Set)
        ' Set to repeat "500 ms on, 500 ms off" 3 times
        stBuzzerSet.dwOn = 100
        ' Rumble time [ms] (1 to 5000)
        stBuzzerSet.dwOff = 100
        ' Stop time [ms] (0 to 5000)
        stBuzzerSet.dwCount = 1
        ' Rumble count [times] (0 to 100)
        stBuzzerSet.bTone = 16
        ' Musical scale (1 to 16)
        stBuzzerSet.bVolume = 3
        ' Buzzer volume (1 to 3)
        Try
            ' btBuzzer Rumble
            ret = Device.btBuzzer(1, stBuzzerSet)
            If ret <> LibDef.BT_OK Then
                disp = "btBuzzer error ret[" & ret & "]"
                MessageBox.Show(disp, "Error")
                Return
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try


    End Sub
End Module
