Imports System.Windows.Forms
Imports System.Threading

Public Class Form_SYNC
    Dim H, W As Integer
    Dim FormTitleHeight 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_SYNC_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

        If Form1.PosTopReCal = 0 Then
            Me.Top = Form1.Top + 80
            Me.Left = Form1.Left + 10
        Else
            Me.Top = Form1.PosTopReCal
            Me.Left = Form1.PosLeftReCal
        End If
        FormTitleHeight = Me.Height - (StatusStrip1.Top + StatusStrip1.Height)

        ' 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

    End Sub

    Private Sub Form_SYNC_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.PosTopReCal = Me.Top
        Form1.PosLeftReCal = Me.Left

    End Sub

    Private Sub CheckBoxHide_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxHide.CheckedChanged
        If CheckBoxHide.Checked = True Then
            LabelReCal.Visible = False
            RichTextBox1.Visible = False
            H = Me.Height
            W = Me.Width
            Me.Height = H - RichTextBox1.Height + LabelReCal.Height - FormTitleHeight
            Me.Width = CheckBoxHide.Left + 15
        Else
            LabelReCal.Visible = True
            RichTextBox1.Visible = True
            Me.Height = H
            Me.Width = W
        End If

    End Sub

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

    End Sub

    Private Sub ButtonReCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReCal.Click
        Dim AP_RESP As String
        Dim S As String
        Dim tickBegin As Long = 0
        Dim tickEnd As Long = 0

        S = "Not connected"
        Resp = ""
        Form1.Cmd = ":CMR#"

        If Form1.AscomConnect = False Then
            If Form1.APnetwork = False Then
                Form1.SendTheCommand()
                ' get response from ap mount via com port
                tickBegin = Int(Date.Now.Ticks / 10000000)
                Do Until Resp.Length >= 33
                    Resp = Resp & Form1.SerialPort1.ReadExisting()
                    tickEnd = Int(Date.Now.Ticks / 10000000)
                    If tickEnd - tickBegin > Form1.ReadTimeout Then
                        Form1.MountReadError()
                        Exit Do
                    End If
                Loop
            Else
                Form1.SendTheCommand()
                ' get response from ap mount via network connection
                Try
                    Resp = Form1.GetTheResponse()
                Catch ex As Exception
                    Form1.LogScope("TCP < ", Color.DarkRed, ex.Message)
                    Form1.MountReadError()
                End Try
            End If

            AP_RESP = Resp
            If AP_RESP = "" Then
                S = "No response to (Re-Cal)"
            Else
                S = AP_RESP & " (Re-Cal)"
                My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
            End If
            ToolStripStatusLabel1.Text = S
        Else
            Try
                Form1.SendTheCommand()
                ToolStripStatusLabel1.Text = "ASCOM (Re-Cal)"
                My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
            Catch ex As Exception
                ToolStripStatusLabel1.Text = ex.Message
            End Try
        End If


    End Sub
End Class
