Imports System.Threading
Imports System.IO
Imports System.Text

Public Class Form_Meridian

    Dim tickBegin As Long
    Dim tickEnd As Long
    Dim Status As String
    Dim SetMer As String = ""
    Public Mer As String

    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_Meridian_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.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

        FillMeridianOffset()

    End Sub

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

    End Sub

    Private Sub ButtonGetMD_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetMD.Click
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            Form1.AP_Command("GetMeridianOffset")
            Mer = Form1.MeridianOffset

            If Mer = "" Then
                ToolStripStatusLabel.Text = "No response or Rev. lower than S"
            Else
                ToolStripStatusLabel.Text = Mer
                Try
                    FillMeridianOffset()

                    Form1.UpdateStatusLine("Get Meridian Offset successful", Color.Black)

                    If Form_Monitor.Visible = True Then
                        Form1.Update_Scope_Pos()
                    End If
                Catch ex As Exception
                    If Form1.ErrorMsgBox = True Then
                        Dim M As String
                        M = ex.Message & vbCrLf & "Meridian Offset = " & Form1.MeridianOffset
                        MsgBox(M, MsgBoxStyle.OkOnly, "Get Meridian Offset Error")
                    End If
                    ToolStripStatusLabel.Text = "Invalid response: " & Form1.MeridianOffset
                    Form1.UpdateStatusLine("Get Meridian Offset failed", Color.DarkRed)
                End Try
            End If
        Else
            ToolStripStatusLabel.Text = "Not connected"
        End If

    End Sub

    Public Sub FillMeridianOffset()
        Try
            If Mid(Mer, 1, 1) = "-" Then
                MerWest.Checked = True
                MDh.Value = Mid(Mer, 2, 2)
                MDm.Value = Mid(Mer, 5, 2)
                MDs.Value = Mid(Mer, 8, 2)
                MDss.Value = Mid(Mer, 11, 1)
            Else
                MerEast.Checked = True
                MDh.Value = Mid(Mer, 1, 2)
                MDm.Value = Mid(Mer, 4, 2)
                MDs.Value = Mid(Mer, 7, 2)
                MDss.Value = Mid(Mer, 10, 1)
            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub ButtonSetMD_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetMD.Click
        SetMeridianOffset(False)

    End Sub

    Private Sub ButtonClear_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClear.Click
        SetMeridianOffset(True)

    End Sub

    Public Sub SaveMeridianOffset()
        SetMer = ""
        SetMer = SetMer & Format(MDh.Value, "00") & ":" & _
            Format(MDm.Value, "00") & ":" & _
            Format(MDs.Value, "00") & "." & _
            Format(MDss.Value, "0") & "#"

        If MerWest.Checked = True Then
            Form1.MeridianOffset = "-" & SetMer
            SetMer = ":SM -" & SetMer
        Else
            Form1.MeridianOffset = SetMer
            SetMer = ":SM " & SetMer
        End If

    End Sub

    Public Sub SetMeridianOffset(ByVal Clear As Boolean)
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            Resp = ""
            If Clear = False Then
                SaveMeridianOffset()
            Else
                SetMer = ":SM 00:00:00.0#"
            End If

            ToolStripStatusLabel.Text = SetMer

            If Form1.APnetwork = False Then
                ' serial port connection to AP mount
                Try
                    Form1.SerialPort1.Write(SetMer)
                    Form1.LogScope("Sent > ", Color.DarkBlue, SetMer)
                    tickBegin = Int(Date.Now.Ticks / 10000000)
                    Do Until Resp.Length >= 1
                        Resp = Resp & Form1.SerialPort1.ReadExisting()
                        tickEnd = Int(Date.Now.Ticks / 10000000)
                        If tickEnd - tickBegin > Form1.ReadTimeout Then
                            Status = "Timeout setting Meridian Offset"
                            Exit Do
                        End If
                    Loop
                    Form1.LogScope("Resp > ", Color.DarkGreen, Resp)
                    Form1.Rsp = Resp

                    If Resp = "1" Then
                        If Clear = False Then
                            Form1.UpdateStatusLine("Set Meridian Offset successful", Color.Black)
                            Status = "Successful."
                        Else
                            Form1.UpdateStatusLine("Clear Meridian Offset successful", Color.Black)
                            Status = "Clear successful."
                        End If
                    End If

                Catch ex As Exception
                    If Form1.ErrorMsgBox = True Then
                        Dim M As String
                        M = ex.Message & vbCrLf & "Meridian Offset = " & SetMer
                        MsgBox(M, MsgBoxStyle.OkOnly, "Set Meridian Offset Error")
                    End If
                    Form1.UpdateStatusLine("Set Meridian Offset failed", Color.DarkRed)
                End Try

            Else
                ' network connection to AP mount
                Try
                    Dim ApRec(50) As Byte
                    Dim Cmd As String = SetMer
                    ApRec = Encoding.ASCII.GetBytes(Cmd)
                    Form1.networkStream.Write(ApRec, 0, Cmd.Length)
                    Form1.LogScope("Sent > ", Color.DarkBlue, SetMer)

                    Do
                        Form1.NumBytesRead = Form1.networkStream.Read(Form1.MountRec, 0, Form1.MountRec.Length)
                        Resp = Resp & Encoding.ASCII.GetString(Form1.MountRec, 0, Form1.NumBytesRead)
                    Loop Until Microsoft.VisualBasic.Right(Resp, 2) = "1"

                    Form1.LogScope("Resp > ", Color.DarkGreen, Resp)
                    Form1.Rsp = Resp

                    If Resp = "1" Then
                        If Clear = False Then
                            Form1.UpdateStatusLine("Set Meridian Offset successful", Color.Black)
                            Status = "Successful."
                        Else
                            Form1.UpdateStatusLine("Clear Meridian Offset successful", Color.Black)
                            Status = "Clear successful."
                        End If
                    End If

                Catch ex As Exception
                    If Form1.ErrorMsgBox = True Then
                        Dim M As String
                        M = ex.Message & vbCrLf & "Meridian Offset = " & SetMer
                        MsgBox(M, MsgBoxStyle.OkOnly, "Set Meridian Offset Error")
                    End If
                    Form1.UpdateStatusLine("Set Meridian Offset failed", Color.DarkRed)
                End Try

            End If

            Form1.MeridianOffset = Mid(SetMer, 5, 20)
            ToolStripStatusLabel.Text = Status
        Else
            Form1.UpdateStatusLine("Not connected", Color.DarkRed)

        End If

    End Sub

    Private Sub ToolStripStatusLabel_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ToolStripStatusLabel.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ToolStripStatusLabel.Text = ""
        End If

    End Sub
End Class