Public Class Form_FOCUS

    Dim FocusTime As Integer
    Dim FocusCount, FocusRealCount As Integer
    Dim TimeStart, TimeEnd, TimeStartF, TimeEndF As Single
    Dim ProgI, ProgO As Single

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_DBLCLKS As Int32 = &H8
            Const CS_NOCLOSE As Int32 = &H200
            cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
            Return cp
        End Get
    End Property


    Private Sub Form_FOCUS_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ' Make sure form is on the screen
        If Me.Location.X < 0 Or Me.Location.Y < 0 Then
            Me.Location = New Point(0, 0)
        End If

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            Me.Enabled = True
        End If

    End Sub

    Public Sub ButtonFocusIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFocusIn.Click
        If CheckBoxFocusTime.Checked = True Then
            FocusTime = NumericUpDownFocus.Value
            FocusRealCount = FocusCount - FocusTime
            Form1.AP_Command("FocusIn")
            TimerI.Interval = FocusTime
            TimerI.Enabled = True
            TimerO.Enabled = False
            ProgI = (FocusTime / TimerProgI.Interval) - 1
            TimerProgI.Enabled = True
        End If

    End Sub

    Public Sub ButtonFocusOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFocusOut.Click
        If CheckBoxFocusTime.Checked = True Then
            FocusTime = NumericUpDownFocus.Value
            FocusRealCount = FocusCount + FocusTime
            Form1.AP_Command("FocusOut")
            TimerO.Interval = FocusTime
            TimerO.Enabled = True
            TimerI.Enabled = False
            ProgO = (FocusTime / TimerProgO.Interval) - 1
            TimerProgO.Enabled = True
        End If

    End Sub

    Public Sub Update_Focus_Count()
        If FocusCount < 0 Then
            TextBoxFocusCount.Text = FocusCount & "  In"
            ButtonFocusRestore.Enabled = True
            'ButtonFocusRestore.ForeColor = Color.Black
        End If
        If FocusCount > 0 Then
            TextBoxFocusCount.Text = FocusCount & "  Out"
            ButtonFocusRestore.Enabled = True
            'ButtonFocusRestore.ForeColor = Color.Black
        End If
        If FocusCount = 0 Then
            TextBoxFocusCount.Text = FocusCount
            ButtonFocusRestore.Enabled = False
            'ButtonFocusRestore.ForeColor = Color.DarkGray
        End If

    End Sub

    Private Sub ButtonFocusRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFocusRestore.Click
        If FocusCount > 0 Then
            TimerHomeI.Interval = FocusCount
            Form1.AP_Command("FocusIn")
            TimerHomeI.Enabled = True
            ProgI = (TimerHomeI.Interval / TimerProgI.Interval) - 1
            TimerProgI.Enabled = True
        End If
        If FocusCount < 0 Then
            TimerHomeO.Interval = FocusCount * -1
            Form1.AP_Command("FocusOut")
            TimerHomeO.Enabled = True
            ProgO = (TimerHomeO.Interval / TimerProgO.Interval) - 1
            TimerProgO.Enabled = True
        End If

    End Sub

    Public Sub ButtonFocusStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFocusStop.Click
        TimerI.Enabled = False
        TimerO.Enabled = False
        TimerHomeI.Enabled = False
        TimerHomeO.Enabled = False
        TimerProgI.Enabled = False
        TimerProgO.Enabled = False
        Form1.AP_Command("FocusStop")

    End Sub

    Private Sub ButtonFocusIn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ButtonFocusIn.MouseDown
        If CheckBoxFocusTime.Checked = False Then
            ProgIstart()
        End If

    End Sub

    Private Sub ButtonFocusIn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ButtonFocusIn.MouseUp
        If CheckBoxFocusTime.Checked = False Then
            ProgIend()
        End If

    End Sub

    Private Sub ButtonFocusOut_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ButtonFocusOut.MouseDown
        If CheckBoxFocusTime.Checked = False Then
            ProgOstart()
        End If

    End Sub

    Private Sub ButtonFocusOut_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ButtonFocusOut.MouseUp
        If CheckBoxFocusTime.Checked = False Then
            ProgOend()
        End If

    End Sub

    Public Sub ProgIstart()
        Form1.AP_Command("FocusIn")
        TimeStartF = Microsoft.VisualBasic.DateAndTime.Timer
        FocusRealCount = FocusCount
        ProgI = 1000000
        TimerProgI.Enabled = True

    End Sub

    Public Sub ProgIend()
        Form1.AP_Command("FocusStop")
        TimeEndF = Microsoft.VisualBasic.DateAndTime.Timer
        FocusCount = FocusCount - (1000 * (TimeEndF - TimeStartF))
        TimerProgI.Enabled = False
        Update_Focus_Count()

    End Sub

    Public Sub ProgOstart()
        Form1.AP_Command("FocusOut")
        TimeStartF = Microsoft.VisualBasic.DateAndTime.Timer
        FocusRealCount = FocusCount
        ProgO = 1000000
        TimerProgO.Enabled = True

    End Sub

    Public Sub ProgOend()
        Form1.AP_Command("FocusStop")
        TimeEndF = Microsoft.VisualBasic.DateAndTime.Timer
        FocusCount = FocusRealCount + (1000 * (TimeEndF - TimeStartF))
        TimerProgO.Enabled = False
        Update_Focus_Count()

    End Sub

    Private Sub RadioButtonFocusFast_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonFocusFast.CheckedChanged
        If Form1.SerialPort1.IsOpen = False And Form1.SocketIsOpen = False And Form1.IsConnected = False Then Exit Sub
        If RadioButtonFocusFast.Checked = True Then
            Form1.AP_Command("FocusFast")
        End If

    End Sub

    Private Sub RadioButtonFocusSlow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonFocusSlow.CheckedChanged
        If Form1.SerialPort1.IsOpen = False And Form1.SocketIsOpen = False And Form1.IsConnected = False Then Exit Sub
        If RadioButtonFocusSlow.Checked = True Then
            Form1.AP_Command("FocusSlow")
        End If

    End Sub

    Private Sub LabelClearFocus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelClearFocus.Click
        ZeroCounterFocus()

    End Sub

    Private Sub LabelClearFocus_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles LabelClearFocus.MouseEnter
        Form1.SaveHoverColor = LabelClearFocus.BackColor
        LabelClearFocus.BackColor = Form1.ActiveColor

    End Sub

    Private Sub LabelClearFocus_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles LabelClearFocus.MouseLeave
        LabelClearFocus.BackColor = Form1.SaveHoverColor

    End Sub

    Public Sub ZeroCounterFocus()
        FocusCount = 0
        TextBoxFocusCount.Text = FocusCount

    End Sub

    Private Sub TimerI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerI.Tick
        TimerI.Enabled = False
        TimerProgI.Enabled = False
        Form1.AP_Command("FocusStop")
        FocusCount = FocusRealCount
        Update_Focus_Count()
        If Form1.Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerO.Tick
        TimerO.Enabled = False
        TimerProgO.Enabled = False
        Form1.AP_Command("FocusStop")
        FocusCount = FocusRealCount
        Update_Focus_Count()
        If Form1.Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerProgI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgI.Tick
        If ProgI <= 0 Then
            TimerProgI.Enabled = False
            FocusCount = FocusCount + FocusTime
        End If
        FocusCount = FocusCount - TimerProgI.Interval
        Update_Focus_Count()
        ProgI = ProgI - 1
    End Sub

    Private Sub TimerProgO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgO.Tick
        If ProgO <= 0 Then
            TimerProgO.Enabled = False
            FocusCount = FocusCount - FocusTime
        End If
        FocusCount = FocusCount + TimerProgO.Interval
        Update_Focus_Count()
        ProgO = ProgO - 1

    End Sub

    Private Sub TimerHomeI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHomeI.Tick
        TimerHomeI.Enabled = False
        TimerProgI.Enabled = False
        Form1.AP_Command("FocusStop")
        FocusCount = 0
        Update_Focus_Count()
        If Form1.Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerHomeO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHomeO.Tick
        TimerHomeO.Enabled = False
        TimerProgO.Enabled = False
        Form1.AP_Command("FocusStop")
        FocusCount = 0
        Update_Focus_Count()
        If Form1.Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClose.Click
        Me.Hide()

    End Sub

    Private Sub ButtonKeyboard_Click(sender As System.Object, e As System.EventArgs) Handles ButtonKeyboard.Click
        Form1.KeyControl.Focus()

    End Sub

    Private Sub LabelKeyHelp_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeyHelp.Click
        If Form_FocusHelp.Visible = True Then
            Form_FocusHelp.Close()
        Else
            Form_FocusHelp.Show()
        End If

    End Sub
End Class