Option Strict Off
Option Explicit On
Friend Class frmTCFinger
	Inherits System.Windows.Forms.Form
	
	Private Const PT_GUIMSG_GOOD_IMAGE As Short = 0 ' Scanned good image
	Private Const PT_GUIMSG_NO_FINGER As Short = 1 ' No finger detected
	Private Const PT_GUIMSG_TOO_LIGHT As Short = 2 ' Finger image is too light
	Private Const PT_GUIMSG_TOO_DRY As Short = 3 ' Finger is too dry
	Private Const PT_GUIMSG_TOO_DARK As Short = 4 ' Finger image is too dark
	Private Const PT_GUIMSG_TOO_HIGH As Short = 5 ' Finger is too high
	Private Const PT_GUIMSG_TOO_LOW As Short = 6 ' Finger is too low
	Private Const PT_GUIMSG_TOO_LEFT As Short = 7 ' Finger is too left
	Private Const PT_GUIMSG_TOO_RIGHT As Short = 8 ' Finger is too right
	Private Const PT_GUIMSG_TOO_SMALL As Short = 9 ' Finger image is too small
	Private Const PT_GUIMSG_TOO_STRANGE As Short = 10 ' Finger image is too strange
	Private Const PT_GUIMSG_BAD_QUALITY As Short = 11 ' Finger has bad quality
	Private Const PT_GUIMSG_PUT_FINGER As Short = 12 ' Put finger 1st time
	Private Const PT_GUIMSG_PUT_FINGER2 As Short = 13 ' Put finger 2nd time
	Private Const PT_GUIMSG_PUT_FINGER3 As Short = 14 ' Put finger 3rd time
	Private Const PT_GUIMSG_REMOVE_FINGER As Short = 15 ' Remove finger
	Private Const PT_GUIMSG_CONSOLIDATION_FAIL As Short = 16 ' Consolidation failed
	Private Const PT_GUIMSG_CONSOLIDATION_SUCCEED As Short = 17 ' Consolidation succeed
	Private Const PT_GUIMSG_CLEAN_SENSOR As Short = 18 ' Clean the sensor
	Private Const PT_GUIMSG_KEEP_FINGER As Short = 19 ' Keep finger on the sensor
	Private Const PT_GUIMSG_START As Short = 20 ' Non specific GUI start (not used now)
	Private Const PT_GUIMSG_VERIFY_START As Short = 21 ' GUI start for verification
	Private Const PT_GUIMSG_ENROLL_START As Short = 22 ' GUI start for enrollment
	Private Const PT_GUIMSG_FINGER_DETECT_START As Short = 23 ' GUI start for finger detection
	Private Const PT_GUIMSG_GUI_FINISH As Short = 24 ' GUI finished without specification of success of biometric operation
	Private Const PT_GUIMSG_GUI_FINISH_SUCCEED As Short = 25 ' GUI finished signaling success of biometric operation
	Private Const PT_GUIMSG_GUI_FINISH_FAIL As Short = 26 ' GUI finished signaling failure of bioetric operation
	Private Const PT_GUIMSG_CALIB_START As Short = 27 ' GUI start for sensor calibration
	Private Const PT_GUIMSG_TOO_FAST As Short = 28 ' Finger was swiped too fast
	Private Const PT_GUIMSG_TOO_SKEWED As Short = 29 ' Finger was too skewed during swipe
	Private Const PT_GUIMSG_TOO_SHORT As Short = 30 ' Finger swipe was too short
	Private Const PT_GUIMSG_TOUCH_SENSOR As Short = 31 ' Touch sensor with finger
	Private Const PT_GUIMSG_PROCESSING_IMAGE As Short = 32 ' Image processing started
	Private Const PT_GUIMSG_SWIPE_IN_PROGRESS As Short = 33 ' Finger swipe is in progress
	
    Private Declare Function Beep_Renamed Lib "kernel32" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer
	Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
	Private Declare Function GetTickCount Lib "kernel32" () As Integer
	
    Private WithEvents m_oSF500x As GIGATMS.SFReader
	Private WithEvents m_oResult As clsResult
	
	Private m_bFingerBuffer() As Byte
	Private m_lPutFingerTime As Integer
	Private m_lFlashIndex As Integer
	Private m_lStopFlashIndex As Integer
	Private m_szResult As String
	Private m_bShowResult As Boolean
	Private m_bVerify As Boolean
	
	Private m_bCancel As Boolean
	Public m_oParent As System.Windows.Forms.Form
	Private m_unloadTime As String
	
	Private Function BytesSize(ByRef bBuffer() As Byte) As Integer
		On Error GoTo Err_Proc
		BytesSize = UBound(bBuffer) - LBound(bBuffer) + 1
Err_Exit: 
		Exit Function
Err_Proc: 
		BytesSize = 0
		Resume Err_Exit
	End Function
	
	Private Sub SoundC(ByVal lIndex As Integer, ByVal dwDuration As Integer)
        Dim I As Integer = 0, dwFreq As Integer = 0
		Exit Sub
		lIndex = lIndex + 8
		I = (lIndex - 1) Mod 7
		If I >= 0 Then
			Select Case I
				Case 0 : dwFreq = 264
				Case 1 : dwFreq = 297
				Case 2 : dwFreq = 330
				Case 3 : dwFreq = 352
				Case 4 : dwFreq = 396
				Case 5 : dwFreq = 440
				Case 6 : dwFreq = 495
			End Select
			dwFreq = dwFreq * (Int(lIndex / 7) + 1)
			Beep_Renamed(dwFreq, dwDuration)
		End If
	End Sub
	
	Private Sub BeepOK()
		SoundC(12, 350)
	End Sub
	
	Private Sub BeepNG()
		SoundC(5, 50)
		Sleep(10)
		SoundC(4, 60)
		Sleep(10)
		SoundC(3, 70)
		Sleep(10)
		SoundC(2, 80)
		Sleep(10)
		SoundC(1, 150)
	End Sub
	
	Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
		m_oResult.Result = False
		m_oResult.RaiseResult()
		m_oSF500x.SF500Cancel()
		Me.Close()
    End Sub

    Private Sub frmTCFinger_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    End Sub

    Private Sub frmTCFinger_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim bBusy As Boolean
        If m_unloadTime = "" Then m_unloadTime = CStr(Now)
        m_bCancel = True
        If m_oSF500x Is Nothing Then
            bBusy = False
        Else
            bBusy = m_oSF500x.IsBusy
        End If
        If bBusy And DateDiff(DateInterval.Second, CDate(m_unloadTime), Now) > 30 Then
            m_oSF500x.PortOpen = False
            e.Cancel = True
            TimerCancel.Enabled = True
        Else
            m_oSF500x = Nothing
            m_oResult = Nothing
        End If
    End Sub

    Private Sub frmTCFinger_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Me.Width = Microsoft.VisualBasic.Compatibility.VB6.TwipsToPixelsX(3640)
        Me.Height = Microsoft.VisualBasic.Compatibility.VB6.TwipsToPixelsY(1860)
        Me.Left = m_oParent.Left + (m_oParent.Width - Me.Width) / 2
        Me.Top = m_oParent.Top + (m_oParent.Height - Me.Height) / 2
        m_bCancel = False
        m_unloadTime = ""
        chLightPos((m_oSF500x.MachineModal))
    End Sub

    Private Sub chLightPos(ByRef iReaderType As GIGATMS.SFReader.ReaderModal)
        Dim I As Short
        Select Case iReaderType
            Case GIGATMS.SFReader.ReaderModal.SF500
                'Three Light
            Case GIGATMS.SFReader.ReaderModal.SF600
                'One Light
                For I = 0 To 3
                    ImageLight(0 + I * 3).Left = ImageLight(1 + I * 3).Left
                    ImageLight(1 + I * 3).Visible = False
                    ImageLight(2 + I * 3).Visible = False
                    ImageLight(1 + I * 3).SendToBack()
                    ImageLight(2 + I * 3).SendToBack()
                Next
        End Select
    End Sub

    Private Sub ShowMsg(ByVal szMsg As String, Optional ByRef szAction As String = "")
        labMessage.Text = szMsg
        If szAction <> "" Then labMessage0.Text = szAction
    End Sub

    Public Sub ShowFinger(ByRef oSF500x As GIGATMS.SFReader, ByRef oResult As clsResult, Optional ByVal bVerify As Boolean = False)
        m_oSF500x = oSF500x
        m_oResult = oResult
        m_bVerify = bVerify
        TimerEnroll.Enabled = True
        m_lPutFingerTime = 0
        m_bShowResult = False
        Me.ShowDialog()
    End Sub

    Public Sub VerifyFinger(ByRef oSF500x As GIGATMS.SFReader, ByRef oResult As clsResult, ByRef bVerifyBuffer() As Byte)

        m_oSF500x = oSF500x
        m_oResult = oResult
        Array.Copy(bVerifyBuffer, m_bFingerBuffer, bVerifyBuffer.Length)
        TimerVerify.Enabled = True
        m_lPutFingerTime = 0
        m_bShowResult = False
        Me.ShowDialog()
    End Sub

    Public Sub Identify(ByRef oSF500x As GIGATMS.SFReader, ByRef oResult As clsResult)
        m_oSF500x = oSF500x
        m_oResult = oResult
        TimerIdentify.Enabled = True
        m_lPutFingerTime = 0
        m_bShowResult = False
        Me.ShowDialog()
    End Sub

    Private Sub frmTCFinger_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    End Sub

    Private Sub m_oSF500x_OnMessage(ByVal dwMessage As Long, ByVal szMessage As String) Handles m_oSF500x.OnMessage
        Dim I As Integer
        Dim bFlash As Boolean
        bFlash = False
        TimerClear.Enabled = False
        ShowMsg(szMessage)
        Select Case dwMessage
            Case PT_GUIMSG_ENROLL_START
            Case PT_GUIMSG_PUT_FINGER
                If m_lPutFingerTime <> 1 Then
                    For I = 3 To 11
                        ImageLight(I).Visible = False
                        ImageLight(I).SendToBack()
                    Next I
                    m_lPutFingerTime = 1
                    ImageLight(5 + m_lPutFingerTime).Visible = True
                    ImageLight(5 + m_lPutFingerTime).BringToFront()
                Else
                    If m_oSF500x.MachineModal = GIGATMS.SFReader.ReaderModal.SF500 Then bFlash = True
                End If
                SoundC(7, 300)
            Case PT_GUIMSG_PUT_FINGER2
                If m_lPutFingerTime <> 2 Then
                    m_lPutFingerTime = 2
                    ImageLight(5 + m_lPutFingerTime).Visible = True
                    ImageLight(5 + m_lPutFingerTime).BringToFront()
                Else
                    bFlash = True
                End If
            Case PT_GUIMSG_PUT_FINGER3
                If m_lPutFingerTime <> 3 Then
                    m_lPutFingerTime = 3
                    ImageLight(5 + m_lPutFingerTime).Visible = True
                    ImageLight(5 + m_lPutFingerTime).BringToFront()
                Else
                    bFlash = True
                End If
            Case PT_GUIMSG_REMOVE_FINGER
                If m_lPutFingerTime > 0 Then
                    For I = 9 To 11
                        ImageLight(I).Visible = False
                        ImageLight(I).SendToBack()
                    Next I
                    SoundC(7 + m_lPutFingerTime, 300)
                    m_lPutFingerTime = 0
                End If
            Case PT_GUIMSG_KEEP_FINGER
                ImageLight(2 + m_lPutFingerTime).Visible = True
                ImageLight(2 + m_lPutFingerTime).BringToFront()
            Case PT_GUIMSG_NO_FINGER
                bFlash = True
            Case PT_GUIMSG_CLEAN_SENSOR
                bFlash = True
            Case PT_GUIMSG_TOO_SMALL, PT_GUIMSG_TOO_RIGHT, PT_GUIMSG_TOO_LEFT
                bFlash = True
            Case PT_GUIMSG_TOO_LOW, PT_GUIMSG_TOO_HIGH
                bFlash = True
            Case PT_GUIMSG_TOO_DRY, PT_GUIMSG_TOO_LIGHT
                bFlash = True
            Case PT_GUIMSG_GOOD_IMAGE, PT_GUIMSG_GUI_FINISH_SUCCEED
                m_lPutFingerTime = 0
                BeepOK()
            Case PT_GUIMSG_BAD_QUALITY, PT_GUIMSG_GUI_FINISH_FAIL
                BeepNG()
            Case Else
        End Select
        If bFlash Then
            Select Case m_lPutFingerTime
                Case 0 To 2
                    With ImageLight(5 + m_lPutFingerTime)
                        .Visible = Not .Visible
                        If .Visible Then
                            .BringToFront()
                        Else
                            .SendToBack()
                        End If
                    End With
            End Select
        End If
        Select Case dwMessage
            Case 0 To 26
                picState.Image = picMessage(dwMessage).Image
        End Select
    End Sub

    Private Sub m_oSF500x_OnResult(ByVal szSource As String, ByVal iResult As Integer, ByVal szResult As String) Handles m_oSF500x.OnResult
        If iResult = 0 Then
            m_szResult = vbNullString
        Else
            m_szResult = "Error Code: " & iResult & ", " & szResult
        End If
        If iResult = -1050 Or iResult = -1041 Then
            picState.Image = ImageTimeout.Image
            ShowMsg(szResult)
        ElseIf m_bShowResult Then
            ShowMsg(szResult)
        End If
    End Sub

    Private Sub TimerCancel_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TimerCancel.Tick
        TimerCancel.Enabled = False
        Me.Close()
    End Sub

    Private Sub TimerClear_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TimerClear.Tick
        TimerClear.Enabled = False
        labMessage.Text = modLanguage.GetString("M0067", "Processing, please wait ...")
        picState.Image = Nothing
    End Sub

    Private Sub TimerEnroll_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TimerEnroll.Tick
        Dim I As Integer
        Dim iFID As Short
        Dim bExitSub As Boolean
        bExitSub = False
        TimerEnroll.Enabled = False
        If m_oSF500x Is Nothing Then
        ElseIf m_oSF500x.PortOpen = False Then
            m_bShowResult = True
            m_oSF500x.AutoScan()
            m_bShowResult = False
        End If
        If m_oSF500x Is Nothing Then
            bExitSub = True
        ElseIf m_oSF500x.PortOpen Then
            TimerClear.Enabled = True
            m_szResult = vbNullString
            ShowMsg("", modLanguage.GetString("M0068", "Enroll Finger"))
            If m_oSF500x.EnrollFinger(iFID, m_bFingerBuffer, False) Then
                If Not m_bCancel Then
                    If m_bVerify Then
                        ShowMsg(modLanguage.GetString("M0069", "Wait..."), modLanguage.GetString("M0070", "Verify Finger"))
                        If m_oSF500x.VerifyFinger(m_bFingerBuffer) = False Then
                            BeepNG()
                            ShowMsg(modLanguage.GetString("M0071", "Fail! Re-try Enroll Finger!"))
                            For I = 1 To 300 Step 1
                                Sleep(5)
                                System.Windows.Forms.Application.DoEvents()
                            Next I
                            TimerEnroll.Enabled = True
                            bExitSub = True
                        End If
                    End If
                    If bExitSub = False Then
                        With m_oResult
                            .AddValue(0, "FingerID")
                            .AddValue(BytesSize(m_bFingerBuffer), "FingerBufferCount")
                            .AddValue(m_bFingerBuffer, "FingerBuffers")
                            .AddValue((m_oSF500x.MachineModal), "IssueMachine")
                            .Result = True
                            m_oResult.RaiseResult()
                        End With
                        Me.Close()
                    End If
                End If
            Else
                If Not m_bCancel Then
                    BeepNG()
                    'For I = 1 To 100 Step 10
                    Sleep(10)
                    System.Windows.Forms.Application.DoEvents()
                    'Next I
                    TimerEnroll.Enabled = True
                End If
            End If
        Else
            If Not m_bCancel Then
                'm_oSF500x.PortOpen = False
                ShowMsg(modLanguage.GetString("M0072", "Re-try open SmaFinger Reader"))
                'For I = 1 To 100 Step 10
                Sleep(10)
                System.Windows.Forms.Application.DoEvents()
                'Next I
                TimerEnroll.Enabled = True
            End If
        End If
    End Sub

    Private Sub TimerIdentify_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TimerIdentify.Tick
        Dim I As Integer
        Dim iFID As Short
        TimerIdentify.Enabled = False
        If m_oSF500x Is Nothing Then
        ElseIf m_oSF500x.PortOpen = False Then
            m_bShowResult = True
            m_oSF500x.AutoScan()
            m_bShowResult = False
        End If
        If m_oSF500x Is Nothing Then
            'Do Nothing
        ElseIf m_oSF500x.PortOpen Then
            TimerClear.Enabled = True
            m_szResult = vbNullString

            ShowMsg(modLanguage.GetString("M0069", "Wait..."), modLanguage.GetString("M0073", "Identify"))
            iFID = m_oSF500x.Identify(False)
            If iFID = -1 Then
                If Not m_oResult Is Nothing Then
                    With m_oResult
                        .AddValue(False, "IdentifyResult")
                        .AddValue(0, "FingerID")
                        .Result = True
                        m_oResult.RaiseResult()
                    End With
                End If
                Me.Close()
            Else
                With m_oResult
                    .AddValue(True, "IdentifyResult")
                    .AddValue(iFID, "FingerID")
                    .Result = True
                    m_oResult.RaiseResult()
                End With
                Me.Close()
            End If
        Else
            If Not m_bCancel Then
                m_oSF500x.PortOpen = False
                ShowMsg(modLanguage.GetString("M0074", "Re-try open ") & IIf(m_oSF500x.MachineModal = GIGATMS.SFReader.ReaderModal.SF500, "SF500", "SF600"))
                For I = 1 To 100 Step 10
                    Sleep(10)
                    System.Windows.Forms.Application.DoEvents()
                Next I
                TimerEnroll.Enabled = True
            End If
        End If
    End Sub

    Private Sub TimerVerify_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TimerVerify.Tick
        Dim I As Integer
        TimerVerify.Enabled = False
        If m_oSF500x Is Nothing Then
        ElseIf m_oSF500x.PortOpen = False Then
            m_bShowResult = True
            m_oSF500x.AutoScan()
            m_bShowResult = False
        End If
        If m_oSF500x Is Nothing Then
            'Do Nothing
        ElseIf m_oSF500x.PortOpen Then
            TimerClear.Enabled = True
            m_szResult = vbNullString

            ShowMsg(modLanguage.GetString("M0069", "Wait..."), modLanguage.GetString("M0070", "Verify Finger"))
            If m_oSF500x.VerifyFinger(m_bFingerBuffer) = False Then
                If Not m_oResult Is Nothing Then
                    With m_oResult
                        .AddValue(False, "VerifyResult")
                        .Result = True
                        m_oResult.RaiseResult()
                    End With
                End If
                Me.Close()
            Else
                With m_oResult
                    .AddValue(True, "VerifyResult")
                    .Result = True
                    m_oResult.RaiseResult()
                End With
                Me.Close()
            End If
        Else
            If Not m_bCancel Then
                m_oSF500x.PortOpen = False
                ShowMsg(modLanguage.GetString("M0074", "Re-try open ") & IIf(m_oSF500x.MachineModal = GIGATMS.SFReader.ReaderModal.SF500, "SF500", "SF600"))
                For I = 1 To 100 Step 10
                    Sleep(10)
                    System.Windows.Forms.Application.DoEvents()
                Next I
                TimerEnroll.Enabled = True
            End If
        End If
    End Sub
End Class