Imports System.Threading

Public Class Form_SEARCH
    Dim FirstLoad As Boolean = True
    Dim Rate_BC As Color
    Dim RateSet_BC As Color = My.Settings.MiniActivateColor
    Dim AP_DEC, AP_RA As String
    Dim StopSearch As Boolean = False
    Dim MoveDir As String
    Dim SlewDur As Integer
    Dim tickBegin As Long
    Dim tickEnd As Long
    Dim RunColor As Color = Color.White
    Dim RunBackColor As Color = Color.Red
    Dim NoRunColor As Color = Color.Black
    Dim NoRunBackColor As Color = SystemColors.Control

    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 ButtonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClose.Click
        If StopSearch = True Then
            ButtonStart.PerformClick()
        End If
        Me.Hide()

    End Sub

    Private Sub ButtonVar_Click(sender As System.Object, e As System.EventArgs) Handles ButtonVar.Click
        Form1.SetVarCenterRate()
        Button1x.BackColor = Rate_BC
        Button12x.BackColor = Rate_BC
        Button64x.BackColor = Rate_BC
        Button600x.BackColor = Rate_BC
        ButtonVar.BackColor = RateSet_BC

    End Sub

    Private Sub Button1x_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1x.Click
        Form1.SetSlewRate(3)
        Button1x.BackColor = RateSet_BC
        Button12x.BackColor = Rate_BC
        Button64x.BackColor = Rate_BC
        Button600x.BackColor = Rate_BC
        ButtonVar.BackColor = Rate_BC

    End Sub

    Private Sub Button12x_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12x.Click
        Form1.SetSlewRate(4)
        Button1x.BackColor = Rate_BC
        Button12x.BackColor = RateSet_BC
        Button64x.BackColor = Rate_BC
        Button600x.BackColor = Rate_BC
        ButtonVar.BackColor = Rate_BC

    End Sub

    Private Sub Button64x_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button64x.Click
        Form1.SetSlewRate(5)
        Button1x.BackColor = Rate_BC
        Button12x.BackColor = Rate_BC
        Button64x.BackColor = RateSet_BC
        Button600x.BackColor = Rate_BC
        ButtonVar.BackColor = Rate_BC

    End Sub

    Private Sub Button600x_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button600x.Click
        Form1.SetSlewRate(6)
        Button1x.BackColor = Rate_BC
        Button12x.BackColor = Rate_BC
        Button64x.BackColor = Rate_BC
        Button600x.BackColor = RateSet_BC
        ButtonVar.BackColor = Rate_BC

    End Sub

    Private Sub Form_SEARCH_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.PosTopS = Me.Top
        Form1.PosLeftS = Me.Left

    End Sub

    Private Sub Form_SEARCH_Load(ByVal sender As Object, ByVal 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

        ' Make sure window is not off of screen
        If Me.Left + Me.Width > Form1.ScreenWidth Then
            Me.Left = Form1.ScreenWidth - Me.Width - 100
        End If
        If Me.Top + Me.Height > Form1.ScreenHeight Then
            Me.Top = Form1.ScreenHeight - Me.Height - 100
        End If

        Rate_BC = Button1x.BackColor
        LabelRADEC.Text = "None saved"

    End Sub

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            GetScopeRaDec()
            AP_RA = Form1.AP_RA
            AP_DEC = Form1.AP_DEC
            If AP_RA = "" Then
                ToolStripStatusLabel1.Text = "No response"
            Else
                ToolStripStatusLabel1.Text = "Position saved"
                ButtonRestore.Enabled = True
                LabelRADEC.Text = Mid(AP_RA, 1, Len(AP_RA) - 1) _
                    & "  " & Mid(AP_DEC, 1, Len(AP_DEC) - 1)
            End If
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRestore.Click
        Dim Resp As String
        Dim Status As String = ""
        Dim AP_RA_send As String = ""
        Dim AP_DEC_send As String = ""

        If StopSearch = True Then
            ButtonStart.PerformClick()
        End If

        ToolStripStatusLabel1.Text = "Restoring saved position"
        Application.DoEvents()

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            Form_RA_DEC.AP_RA = ":Sr " & AP_RA
            Form_RA_DEC.AP_DEC = ":Sd " & AP_DEC
            Status = ""
            Resp = ""
            Form_RA_DEC.GotoRaDec()
            If Resp = "0" Then
                Form1.StatusLabel.Text = "Restore RA/DEC successful"
                Status = "Restore successful."
                Form1.RAtext = Mid(AP_RA_send, 5, 2) & "h " & _
                               Mid(AP_RA_send, 8, 2) & "m " & _
                               Mid(AP_RA_send, 11, 4) & "s"
                Form1.DECtext = Mid(AP_DEC_send, 5, 1) & _
                          Mid(AP_DEC_send, 6, 2) & "° " & _
                          Mid(AP_DEC_send, 9, 2) & "' " & _
                          Mid(AP_DEC_send, 12, 2) & """"
            End If

            ToolStripStatusLabel1.Text = Status
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
        If StopSearch = True Then
            Timer1.Enabled = False
            Timer2.Enabled = False
            Timer3.Enabled = False
            Timer4.Enabled = False
            SendReceiveAP(":Q#", "Search", False, 0)
            LabelN.ForeColor = NoRunColor
            LabelN.BackColor = NoRunBackColor
            LabelS.ForeColor = NoRunColor
            LabelS.BackColor = NoRunBackColor
            LabelE.ForeColor = NoRunColor
            LabelE.BackColor = NoRunBackColor
            LabelW.ForeColor = NoRunColor
            LabelW.BackColor = NoRunBackColor
            StopSearch = False
            ButtonStart.Text = "Begin"
            ToolStripStatusLabel1.Text = "Search stopped"
        Else
            StopSearch = True
            ButtonStart.Text = "End"
            SlewDur = SlewMs.Value
            MoveDir = ":Mn#"
            SendReceiveAP(MoveDir, "Search", False, 0)
            LabelN.ForeColor = RunColor
            LabelN.BackColor = RunBackColor
            ToolStripStatusLabel1.Text = "Search started"
            Timer1.Interval = SlewDur
            Timer1.Enabled = True
            ToolStripStatusLabel1.Text = MoveDir & " " & SlewDur
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        ToolStripStatusLabel1.Text = MoveDir & " Delay " & DelayMS.Value
        SendReceiveAP(":Qn#", "Search", False, 0)
        LabelN.ForeColor = NoRunColor
        LabelN.BackColor = NoRunBackColor
        Application.DoEvents()
        If StopSearch = True Then
            Me.Cursor = Cursors.WaitCursor
            Thread.Sleep(DelayMS.Value)
            Me.Cursor = Cursors.Default
            MoveDir = ":Me#"
            ToolStripStatusLabel1.Text = MoveDir & " " & SlewDur
            SendReceiveAP(MoveDir, "Search", False, 0)
            LabelE.ForeColor = RunColor
            LabelE.BackColor = RunBackColor
            Timer2.Interval = SlewDur
            Timer2.Enabled = True
        End If

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer2.Enabled = False
        ToolStripStatusLabel1.Text = MoveDir & " Delay " & DelayMS.Value
        SendReceiveAP(":Qe#", "Search", False, 0)
        LabelE.ForeColor = NoRunColor
        LabelE.BackColor = NoRunBackColor
        Application.DoEvents()
        If StopSearch = True Then
            Me.Cursor = Cursors.WaitCursor
            Thread.Sleep(DelayMS.Value)
            Me.Cursor = Cursors.Default
            MoveDir = ":Ms#"
            SlewDur = SlewDur + SlewMs.Value
            ToolStripStatusLabel1.Text = MoveDir & " " & SlewDur
            SendReceiveAP(MoveDir, "Search", False, 0)
            LabelS.ForeColor = RunColor
            LabelS.BackColor = RunBackColor
            Timer3.Interval = SlewDur
            Timer3.Enabled = True
        End If

    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        Timer3.Enabled = False
        ToolStripStatusLabel1.Text = MoveDir & " Delay " & DelayMS.Value
        SendReceiveAP(":Qs#", "Search", False, 0)
        LabelS.ForeColor = NoRunColor
        LabelS.BackColor = NoRunBackColor
        Application.DoEvents()
        If StopSearch = True Then
            Me.Cursor = Cursors.WaitCursor
            Thread.Sleep(DelayMS.Value)
            Me.Cursor = Cursors.Default
            MoveDir = ":Mw#"
            ToolStripStatusLabel1.Text = MoveDir & " " & SlewDur
            SendReceiveAP(MoveDir, "Search", False, 0)
            LabelW.ForeColor = RunColor
            LabelW.BackColor = RunBackColor
            Timer4.Interval = SlewDur
            Timer4.Enabled = True
        End If

    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        Timer4.Enabled = False
        ToolStripStatusLabel1.Text = MoveDir & " Delay " & DelayMS.Value
        SendReceiveAP(":Qw#", "Search", False, 0)
        LabelW.ForeColor = NoRunColor
        LabelW.BackColor = NoRunBackColor
        Application.DoEvents()
        If StopSearch = True Then
            Me.Cursor = Cursors.WaitCursor
            Thread.Sleep(DelayMS.Value)
            Me.Cursor = Cursors.Default
            MoveDir = ":Mn#"
            SlewDur = SlewDur + SlewMs.Value
            ToolStripStatusLabel1.Text = MoveDir & " " & SlewDur
            SendReceiveAP(MoveDir, "Search", False, 0)
            LabelN.ForeColor = RunColor
            LabelN.BackColor = RunBackColor
            Timer1.Interval = SlewDur
            Timer1.Enabled = True
        End If

    End Sub
End Class