Imports System.IO

Public Class Form_TrackingRate
    Dim Trate As Single = 0.0
    Dim dRate As Single
    Dim sRate As Single
    Dim TipRate As String
    Dim H, M, S, SS As Integer

    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_TrackingRate_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

        TextBoxRA.Text = Form1.RArate
        TextBoxDEC.Text = Form1.DECrate
        TextBoxRAInc.Text = Form1.RArateInc
        TextBoxDECInc.Text = Form1.DECrateInc
        ButtonRAplusA.Text = "+" & TextBoxRAInc.Text
        ButtonRAminusA.Text = "-" & TextBoxRAInc.Text
        ButtonDECplusA.Text = "+" & TextBoxDECInc.Text
        ButtonDECminusA.Text = "-" & TextBoxDECInc.Text
        TextBoxRAdrift.Text = Form1.RAdrift
        TextBoxDECdrift.Text = Form1.DECdrift
        TextBoxRaHMSdrift.Text = Form1.RAdriftHMS
        TextBoxDecHMSdrift.Text = Form1.DECdriftHMS

    End Sub

    Private Sub Form_TrackingRate_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveValues()

    End Sub

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        SaveValues()
        Me.Hide()

    End Sub

    Private Sub SaveValues()
        Form1.RArate = TextBoxRA.Text
        Form1.DECrate = TextBoxDEC.Text
        Form1.RArateInc = TextBoxRAInc.Text
        Form1.DECrateInc = TextBoxDECInc.Text
        Form1.RAdrift = TextBoxRAdrift.Text
        Form1.DECdrift = TextBoxDECdrift.Text
        Form1.RAdriftHMS = TextBoxRaHMSdrift.Text
        Form1.DECdriftHMS = TextBoxDecHMSdrift.Text
        Form1.AdjustdRA = CheckBoxAdjustSC.Checked

    End Sub

    Private Sub ButtonRAset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRAset.Click
        Dim RAvalue As String = TextBoxRA.Text

        If Mid(RAvalue, 1, 1) = "+" Then
            Mid(RAvalue, 1, 1) = "-"
        Else
            Mid(RAvalue, 1, 1) = "+"
        End If

        SetTrackingRate(":RR", "RA", RAvalue)

    End Sub

    Private Sub ButtonSetDEC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetDEC.Click
        SendReceiveAP(":pS#", "Side of Pier", True, 0)
        Dim SideOfPier As String = Form1.Rsp
        Dim DECrate As String = TextBoxDEC.Text

        If SideOfPier = "West#" Then
            If Mid(DECrate, 1, 1) = "+" Then
                Mid(DECrate, 1, 1) = "-"
            Else
                If Mid(DECrate, 1, 1) = "-" Then
                    Mid(DECrate, 1, 1) = "+"
                End If
            End If
        End If

        SetTrackingRate(":RD", "DEC", DECrate)

    End Sub

    Private Sub SetTrackingRate(ByVal Cmd As String, ByVal Axis As String, ByVal Rate As String)
        Dim Status As String = ""
        Dim TCommand As String = ""

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            Try
                Dim Trate As Single = Rate
                If Trate >= 0 Then
                    TCommand = Cmd & Format(Trate, "+##0.00000") & "#"
                Else
                    TCommand = Cmd & Format(Trate, "##0.00000") & "#"
                End If
                SendReceiveAP(TCommand, "Tracking Rate", True, 1)
                Form1.Rsp = Resp

                If Resp = "1" Then
                    Form1.RadioButtonSideral.Checked = False
                    Form1.RadioButtonLunar.Checked = False
                    Form1.RadioButtonSolar.Checked = False
                    Form1.RadioButtonStop.Checked = False
                    Form1.UpdateStatusLine(Axis & " tracking rate set to " & Format(Trate, "##0.00000"), Color.Black)
                ElseIf Resp <> "" Then
                    Form1.UpdateStatusLine("Invalid response '" & Resp & "' from mount", Color.DarkRed)
                End If

            Catch ex As Exception
                If Form1.ErrorMsgBox = True Then
                    Dim M As String
                    M = ex.Message & vbCrLf & Axis & " tracking rate error, command = " & TCommand
                    MsgBox(M, MsgBoxStyle.OkOnly, "Set Tracking Rate Error")
                End If
                Form1.UpdateStatusLine("Set " & Axis & " tracking rate failed", Color.DarkRed)
            End Try

        Else
            Form1.UpdateStatusLine("Not connected", Color.DarkRed)

        End If

    End Sub

    Private Sub ButtonRAzero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRAzero.Click
        TextBoxRA.Text = "+000.00000"

    End Sub

    Private Sub ButtonDECzero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDECzero.Click
        TextBoxDEC.Text = "+000.00000"

    End Sub

    Private Sub ButtonSid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSid.Click
        ResetRate()
        Form1.UpdateStatusLine("RA/DEC tracking rate reset", Color.Black)
        Form1.RadioButtonSideral.Checked = True

    End Sub

    'Private Sub ButtonLunar_Click(sender As System.Object, e As System.EventArgs) Handles ButtonLunar.Click
    '    ' Lunar rate from Astro-Physics
    '    ' Solar rate is -000.00274
    '    TextBoxRA.Text = "-000.0366"        ' from Astro-Physics

    'End Sub

    Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
        ResetRate()
        Form1.RadioButtonStop.PerformClick()

    End Sub

    Private Sub ResetRate()
        SetTrackingRate(":RR", "RA", "+000.00000")
        SetTrackingRate(":RD", "DEC", "+000.00000")

    End Sub

    Private Sub ButtonStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStatus.Click
        Form1.ScopePositionToolStripMenuItem.PerformClick()

    End Sub

    Private Sub ValueAdjust(ByVal Adj As Single, ByRef TxBox As MaskedTextBox)
        Trate = TxBox.Text
        Trate = Trate + Adj
        If Trate > -1000.0 And Trate < 1000.0 Then
            If Trate >= 0 Then
                TxBox.Text = Format(Trate, "+000.00000")
            Else
                TxBox.Text = Format(Trate, "-000.00000")
            End If
        End If

    End Sub

    Private Sub ButtonRAplusA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRAplusA.Click
        ValueAdjust(TextBoxRAInc.Text, TextBoxRA)

    End Sub

    Private Sub ButtonRAminusA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRAminusA.Click
        Dim NegVal As Single = TextBoxRAInc.Text * -1
        ValueAdjust(NegVal, TextBoxRA)

    End Sub

    Private Sub ButtonDECplusA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDECplusA.Click
        ValueAdjust(TextBoxDECInc.Text, TextBoxDEC)

    End Sub

    Private Sub ButtonDECminusA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDECminusA.Click
        Dim NegVal As Single = TextBoxDECInc.Text * -1
        ValueAdjust(NegVal, TextBoxDEC)

    End Sub

    Private Sub TextBoxRAInc_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxRAInc.TextChanged
        ButtonRAplusA.Text = "+" & TextBoxRAInc.Text
        ButtonRAminusA.Text = "-" & TextBoxRAInc.Text

    End Sub

    Private Sub TextBoxDECInc_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxDECInc.TextChanged
        ButtonDECplusA.Text = "+" & TextBoxDECInc.Text
        ButtonDECminusA.Text = "-" & TextBoxDECInc.Text

    End Sub

    Private Sub GetTrackingRatesFromFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetTrackingRatesFromFileToolStripMenuItem.Click
        Dim Ratefile As String

        OpenFileDialog1.Filter = "GTOCP3 Tracking Rate file tr3|*.tr3;"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Ratefile = OpenFileDialog1.FileName
            Form1.TrackingRateFilename = Ratefile

            Try
                Using sr As StreamReader = New System.IO.StreamReader(Ratefile, System.Text.Encoding.Default)
                    Dim Rec(100) As String
                    Dim I As Integer = 0
                    Do Until sr.EndOfStream = True
                        Rec(I) = sr.ReadLine
                        'If Rec(I).Length >= 9 Then
                        '    TextBoxRA.Text = Mid(Rec(I), 1, 9)
                        'End If
                        I = I + 1
                    Loop
                    sr.Close()
                    TextBoxRA.Text = Rec(0)
                    TextBoxDEC.Text = Rec(1)
                    TextBoxRAdrift.Text = Rec(2)
                    TextBoxDECdrift.Text = Rec(3)
                    TextBoxRaHMSdrift.Text = Rec(2)
                    TextBoxDecHMSdrift.Text = Rec(3)
                End Using

            Catch ex As Exception
                If Form1.ErrorMsgBox = True Then
                    Dim M As String
                    M = ex.Message & vbCrLf & "Error opening rate file" & vbCrLf & Ratefile
                    MsgBox(M, MsgBoxStyle.OkOnly, "Rate File Open Error")
                End If

            End Try
        End If

    End Sub

    Private Sub SaveRatesToFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveRatesToFileToolStripMenuItem.Click
        Dim RateFile As String
        Dim sw As StreamWriter = Nothing

        SaveFileDialog1.Filter = "GTOCP3 Tracking Rate file tr3|*.tr3;"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RateFile = SaveFileDialog1.FileName
            sw = New System.IO.StreamWriter(RateFile, False, System.Text.Encoding.UTF8)

            sw.WriteLine(TextBoxRA.Text & " RA tracking rate")
            sw.WriteLine(TextBoxDEC.Text & " DEC tracking rate")
            sw.WriteLine(TextBoxRAdrift.Text & " RA drift rate seconds")
            sw.WriteLine(TextBoxDECdrift.Text & " DEC drfit rate seconds")
            sw.WriteLine(TextBoxRaHMSdrift.Text & " RA drift rate hh:mm:ss")
            sw.WriteLine(TextBoxDecHMSdrift.Text & " DEC drfit rate hh:mm:ss")

            sw.Close()
            Form1.UpdateStatusLine("Tracking rates saved to: " & RateFile, Color.Black)
            Form1.TrackingRateFilename = RateFile
        End If

    End Sub

    Private Sub ButtonCalcRA_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCalcRA.Click
        dRate = TextBoxRAdrift.Text
        sRate = dRate / 54000
        If sRate > 0.0 Then
            TextBoxRA.Text = Format(sRate, "+000.00000")
        Else
            TextBoxRA.Text = Format(sRate, "-000.00000")
        End If
        ToolTip1.SetToolTip(TextBoxRAdrift, "RA rate calc. = " & Format(sRate, "#000.000000"))

    End Sub

    Private Sub ButtonCalcDEC_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCalcDEC.Click
        dRate = TextBoxDECdrift.Text
        sRate = dRate / 54000
        If sRate > 0.0 Then
            TextBoxDEC.Text = Format(sRate, "+000.00000")
        Else
            TextBoxDEC.Text = Format(sRate, "-000.00000")
        End If
        ToolTip1.SetToolTip(TextBoxDECdrift, "DEC rate calc. = " & Format(sRate, "#000.000000"))

    End Sub

    Private Sub ButtonCalcRaHMS_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCalcRaHMS.Click
        H = Mid(TextBoxRaHMSdrift.Text, 2, 2)
        M = Mid(TextBoxRaHMSdrift.Text, 5, 2)
        S = Mid(TextBoxRaHMSdrift.Text, 8, 2)
        SS = Mid(TextBoxRaHMSdrift.Text, 11, 1)
        dRate = (H * 3600) + (M * 60) + S + (SS / 10)
        If CheckBoxAdjustSC.Checked = True Then
            dRate = dRate * 15
        End If
        If Mid(TextBoxRaHMSdrift.Text, 1, 1) = "-" Then
            dRate = dRate * -1
        End If
        sRate = dRate / 54000
        If sRate > 0.0 Then
            TextBoxRA.Text = Format(sRate, "+000.00000")
        Else
            TextBoxRA.Text = Format(sRate, "-000.00000")
        End If
        ToolTip1.SetToolTip(TextBoxRAdrift, "RA rate calc. = " & Format(sRate, "#000.000000"))

    End Sub



    Private Sub ButtonCalcDecHMS_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCalcDecHMS.Click
        H = Mid(TextBoxDecHMSdrift.Text, 2, 2)
        M = Mid(TextBoxDecHMSdrift.Text, 5, 2)
        S = Mid(TextBoxDecHMSdrift.Text, 8, 2)
        SS = Mid(TextBoxDecHMSdrift.Text, 11, 1)
        dRate = (H * 3600) + (M * 60) + S + (SS / 10)
        If Mid(TextBoxDecHMSdrift.Text, 1, 1) = "-" Then
            dRate = dRate * -1
        End If
        sRate = dRate / 54000
        If sRate > 0.0 Then
            TextBoxDEC.Text = Format(sRate, "+000.00000")
        Else
            TextBoxDEC.Text = Format(sRate, "-000.00000")
        End If
        ToolTip1.SetToolTip(TextBoxDECdrift, "DEC rate calc. = " & Format(sRate, "#000.000000"))

    End Sub

    Private Sub LabelDECnote_Click(sender As System.Object, e As System.EventArgs) Handles LabelDECnote.Click
        Dim Note As String

        Note = "When DEC tracking is set the telescope side of pier" & vbCrLf & _
                "will determine the how the command + or - is set." & vbCrLf & _
                "If the side of pier the telescope is on changes," & vbCrLf & _
                "you must Set the DEC again."

        MsgBox(Note, MsgBoxStyle.Information, "AP Mini DEC tracking rate")

    End Sub

    Private Sub LabelRAnote_Click(sender As System.Object, e As System.EventArgs) Handles LabelRAnote.Click
        Dim Note As String

        Note = "The sign of the :RR command will be reversed" & vbCrLf & _
                "from the RA Rate shown.  This is how the" & vbCrLf & _
                ":RR command works."

        MsgBox(Note, MsgBoxStyle.Information, "AP Mini RA tracking rate")

    End Sub

End Class