Imports System.IO

Public Class Form_Save_Restore
    Dim Cur As Integer
    Dim Good As Boolean = True
    Dim KeyIsDown As Boolean = False
    Dim DoKeyStop As Boolean = False

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

        LabelStatus.Text = ""

    End Sub

    Private Sub Form_Save_Restore_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.SaveRestoreW = Me.Width
        Form1.SaveRestoreH = Me.Height

    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Form1.SaveRestoreW = Me.Width
        Form1.SaveRestoreH = Me.Height
        Me.Hide()

    End Sub

    Private Sub RemoveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveToolStripMenuItem.Click
        RemoveEntry(False)

    End Sub

    Private Sub GotoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GotoToolStripMenuItem.Click
        GotoRADEC()

    End Sub

    Private Sub GotoRADECToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GotoRADECToolStripMenuItem.Click
        GotoRADEC()

    End Sub

    Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click
        EditEntry()

    End Sub

    Private Sub UpdateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateToolStripMenuItem.Click
        UpdateCurEntry()

    End Sub

    Private Sub UpdateToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles UpdateToolStripMenuItem1.Click
        UpdateSelectedMenuItem.PerformClick()

    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        Clipboard.SetText(ListBoxSR.SelectedItem)

    End Sub

    Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click
        AddEntry(True)

    End Sub

    Private Sub ListBoxSR_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxSR.DoubleClick
        EditEntry()

    End Sub

    Private Sub GetListFromFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetListFromFileToolStripMenuItem.Click
        OpenFileDialog1.Filter = "Save/Restore RA/DEC|*.oap;"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ReadSaveRestoreRADECfile(OpenFileDialog1.FileName)
        End If

    End Sub

    Public Sub ReadSaveRestoreRADECfile(ByVal Listfile As String)
        Dim Rec As String

        Try
            Using sr As StreamReader = New System.IO.StreamReader(Listfile, System.Text.Encoding.Default)
                ListBoxSR.Items.Clear()
                Do Until sr.EndOfStream = True
                    Rec = sr.ReadLine
                    ListBoxSR.Items.Add(Rec)
                Loop
                sr.Close()
                Form1.SaveRestoreFilename = Listfile
            End Using
            LabelStatus.Text = Listfile

        Catch ex As Exception
            LabelStatus.Text = ex.Message

        End Try

    End Sub

    Private Sub SaveListToFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveListToFileToolStripMenuItem.Click
        Dim ListFile As String
        Dim sw As StreamWriter = Nothing

        SaveFileDialog1.Filter = "Save/Restore RA/DEC file oap|*.oap;"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ListFile = SaveFileDialog1.FileName
            Dim I As Integer
            sw = New System.IO.StreamWriter(ListFile, False, System.Text.Encoding.UTF8)

            For I = 0 To ListBoxSR.Items.Count - 1
                sw.WriteLine(ListBoxSR.Items(I))
            Next

            sw.Close()
            LabelStatus.Text = "Saved: " & ListFile
            Form1.SaveRestoreFilename = ListFile
        End If

    End Sub

    Private Sub EditFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFileToolStripMenuItem.Click
        Dim Scmd As String = "Notepad " & Form1.SaveRestoreFilename
        Shell(Scmd, AppWinStyle.NormalFocus)

    End Sub

    ' Common routines

    Private Sub UpdateCurEntry()
        ValidateEntry()
        If Good = True Then
            ListBoxSR.Items(Cur) = TextBoxRA.Text & " " & TextBoxDEC.Text & " " & TextBoxName.Text
            LabelStatus.Text = "Selected entry updated"
        End If

    End Sub

    Private Sub AddEntry(ByVal Scope As Boolean)
        Dim Entry As String = ""
        If Scope = True Then
            If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
                GetScopeRaDec()
                Dim RA As String = Form1.AP_RA.Remove(Form1.AP_RA.Length - 1, 1)
                Dim DEC As String = Form1.AP_DEC.Remove(Form1.AP_DEC.Length - 1, 1)
                Dim Name As String = TextBoxName.Text
                If Name.Length = 0 Then
                    Name = "Pos " & ListBoxSR.Items.Count + 1
                End If
                If CheckBoxJD.Checked = True Then
                    RaIn = RA
                    DecIn = DEC
                    RaDecJ2000()
                    Entry = NewRA & " " & NewDEC & " " & TextBoxName.Text & " J" & Yr
                Else
                    Entry = RA & " " & DEC & " " & Name
                End If
                ListBoxSR.Items.Add(Entry)
                LabelStatus.Text = "RA/DEC from AP mount entry added"
            Else
                LabelStatus.Text = "Not connect to AP mount"
            End If

        Else

            If CheckBoxJD.Checked = True Then
                RaIn = TextBoxRA.Text
                DecIn = TextBoxDEC.Text
                RaDecJ2000()
                Entry = NewRA & " " & NewDEC & " " & TextBoxName.Text & " J" & Yr
            Else
                Entry = TextBoxRA.Text & " " & TextBoxDEC.Text & " " & TextBoxName.Text
            End If

            ValidateEntry()
            If Good = True Then
                ListBoxSR.Items.Add(Entry)
                LabelStatus.Text = "Entry added"
            End If
        End If

    End Sub

    Private Sub ValidateEntry()
        Good = True
        Dim ErrorMsg As String = ""
        If TextBoxRA.Text.Length <> 10 Then
            Good = False
            ErrorMsg = "RA invalid "
        End If
        If TextBoxDEC.Text.Length <> 9 Then
            Good = False
            ErrorMsg = ErrorMsg & "DEC invalid "
        End If
        If TextBoxName.Text.Length < 1 Then
            Good = False
            ErrorMsg = ErrorMsg & "No Name "
        End If
        If Good = False Then
            LabelStatus.Text = "Entry format error, " & ErrorMsg
        End If


    End Sub

    Private Sub EditEntry()
        Dim Line As String = ListBoxSR.SelectedItem
        TextBoxRA.Text = Mid(Line, 1, 10)
        TextBoxDEC.Text = Mid(Line, 12, 9)
        TextBoxName.Text = Mid(Line, 22)
        Cur = ListBoxSR.SelectedIndex
        LabelStatus.Text = "RA, DEC & Name text updated with selected entry"
        TextBoxName.Focus()

    End Sub

    Private Sub RemoveEntry(ByVal All As Boolean)
        If All = True Then
            ListBoxSR.Items.Clear()
        Else
            Dim I As Integer
            For I = 0 To ListBoxSR.Items.Count - 1
                If ListBoxSR.SelectedIndex = I Then
                    ListBoxSR.Items.RemoveAt(I)
                    LabelStatus.Text = "Entry " & I + 1 & " removed"
                End If
            Next I
        End If

    End Sub

    Private Sub GotoRADEC()
        Dim Entry As String = ListBoxSR.SelectedItem
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            If Entry <> "" Then
                Form_RA_DEC.AP_RA = ":Sr " & Mid(Entry, 1, 10) & "#"
                Form_RA_DEC.AP_DEC = ":Sd " & Mid(Entry, 12, 9) & "#"
                Form_RA_DEC.GotoRaDec()
                LabelStatus.Text = "Goto RA/DEC sent to mount"
            End If
        Else
            LabelStatus.Text = "Not connected to AP mount"
        End If

    End Sub

    Private Sub StopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopToolStripMenuItem.Click
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            Form1.ButtonSlewStop.PerformClick()
            LabelStatus.Text = "Stop slew sent to mount"
        Else
            LabelStatus.Text = "Not connected to AP mount"
        End If

    End Sub


    Private Sub AddUsingMountsCurrentRADECToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddUsingMountsCurrentRADECToolStripMenuItem.Click
        AddEntry(True)

    End Sub

    Private Sub AddUsingRADECNameTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddUsingRADECNameTextToolStripMenuItem.Click
        AddEntry(False)

    End Sub

    Private Sub EditSelectedEntryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditSelectedEntryToolStripMenuItem.Click
        EditEntry()

    End Sub

    Private Sub UpdateSelectedEntryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateSelectedEntryToolStripMenuItem.Click
        UpdateCurEntry()

    End Sub

    Private Sub RemoveSelectedEntrysToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveSelectedEntrysToolStripMenuItem.Click
        RemoveEntry(False)

    End Sub

    Private Sub RemoveAllEntriesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveAllEntriesToolStripMenuItem.Click
        RemoveEntry(True)

    End Sub

    Private Sub ClearListFilenameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearListFilenameToolStripMenuItem.Click
        Form1.SaveRestoreFilename = ""
        LabelStatus.Text = "SaveRestore RA/DEC filename cleared"

    End Sub

    Private Sub ShowCurrentListFilenameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowCurrentListFilenameToolStripMenuItem.Click
        LabelStatus.Text = Form1.SaveRestoreFilename

    End Sub

    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        AddEntry(False)

    End Sub

    Private Sub ButtonAddAP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddAP.Click
        AddEntry(True)

    End Sub

    Private Sub CheckBoxMselect_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxMselect.CheckedChanged
        If CheckBoxMselect.Checked = True Then
            ListBoxSR.SelectionMode = SelectionMode.MultiSimple
        Else
            ListBoxSR.SelectionMode = SelectionMode.One
        End If

    End Sub

    Private Sub GetRADECToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GetRADECToolStripMenuItem.Click
        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            GetScopeRaDec()
            Dim RA As String = Form1.AP_RA.Remove(Form1.AP_RA.Length - 1, 1)
            Dim DEC As String = Form1.AP_DEC.Remove(Form1.AP_DEC.Length - 1, 1)
            TextBoxRA.Text = RA
            TextBoxDEC.Text = DEC
            LabelStatus.Text = "Got RA/DEC from AP mount"
        Else
            LabelStatus.Text = "Not connected to AP mount"
        End If

    End Sub

    Private Sub UpdateSelectedMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UpdateSelectedMenuItem.Click
        Dim SelectedEntry As Integer = ListBoxSR.SelectedIndex
        If SelectedEntry >= 0 Then
            Dim Line As String = ListBoxSR.SelectedItem
            Dim Name As String = Mid(Line, 22)
            If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
                GetScopeRaDec()
                Dim RA As String = Form1.AP_RA.Remove(Form1.AP_RA.Length - 1, 1)
                Dim DEC As String = Form1.AP_DEC.Remove(Form1.AP_DEC.Length - 1, 1)
                ListBoxSR.Items(SelectedEntry) = RA & " " & DEC & " " & Name
                LabelStatus.Text = "RA/DEC of selected entry updated"
            Else
                LabelStatus.Text = "Not connected to AP mount"
            End If
        Else
            LabelStatus.Text = "No entry to update"
        End If

    End Sub

    Private Sub UpdateSelectedEntryWithCurrentRADECToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UpdateSelectedEntryWithCurrentRADECToolStripMenuItem.Click
        UpdateSelectedMenuItem.PerformClick()

    End Sub


    Private Sub ListBoxSR_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles ListBoxSR.KeyPress
        Dim I As Integer = ListBoxSR.SelectedIndex
        Dim C As Integer = ListBoxSR.Items.Count - 1

        e.Handled = True
        Select Case e.KeyChar
            Case ChrW(Keys.Enter)
                GotoRADEC()
            Case ChrW(Keys.Escape)
                UpdateSelectedMenuItem.PerformClick()
            Case "+"
                If I = C Then
                    ListBoxSR.SelectedIndex = 0
                Else
                    ListBoxSR.SelectedIndex = I + 1
                End If
            Case "-"
                If I = 0 Then
                    ListBoxSR.SelectedIndex = C
                Else
                    ListBoxSR.SelectedIndex = I - 1
                End If
            Case "1"
                If ListBoxSR.Items.Count >= 1 Then
                    ListBoxSR.SelectedIndex = 0
                    GotoRADEC()
                End If
            Case "2"
                If ListBoxSR.Items.Count >= 2 Then
                    ListBoxSR.SelectedIndex = 1
                    GotoRADEC()
                End If
            Case "3"
                If ListBoxSR.Items.Count >= 3 Then
                    ListBoxSR.SelectedIndex = 2
                    GotoRADEC()
                End If
            Case "4"
                If ListBoxSR.Items.Count >= 4 Then
                    ListBoxSR.SelectedIndex = 3
                    GotoRADEC()
                End If
            Case "5"
                If ListBoxSR.Items.Count >= 5 Then
                    ListBoxSR.SelectedIndex = 4
                    GotoRADEC()
                End If
            Case "6"
                If ListBoxSR.Items.Count >= 6 Then
                    ListBoxSR.SelectedIndex = 5
                    GotoRADEC()
                End If
            Case "7"
                If ListBoxSR.Items.Count >= 7 Then
                    ListBoxSR.SelectedIndex = 6
                    GotoRADEC()
                End If
            Case "8"
                If ListBoxSR.Items.Count >= 8 Then
                    ListBoxSR.SelectedIndex = 7
                    GotoRADEC()
                End If
            Case "9"
                If ListBoxSR.Items.Count >= 9 Then
                    ListBoxSR.SelectedIndex = 8
                    GotoRADEC()
                End If
            Case Else
                If DoKeyStop = False Then
                    StopToolStripMenuItem.PerformClick()
                End If
                e.Handled = False
        End Select

    End Sub

    Private Sub ListBoxSR_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ListBoxSR.KeyDown
        If KeyIsDown = False Then
            KeyIsDown = True
            DoKeyStop = True
            Select Case e.KeyCode
                Case Keys.U, Keys.I, Keys.O, Keys.P
                    Form1.AP_Command("N")
                    LabelStatus.Text = "Moving North"
                Case Keys.N, Keys.M, Keys.OemPeriod, Keys.Oemcomma
                    Form1.AP_Command("S")
                    LabelStatus.Text = "Moving South"
                Case Keys.K, Keys.L, Keys.Oem1, Keys.OemSemicolon
                    Form1.AP_Command("E")
                    LabelStatus.Text = "Moving East"
                Case Keys.G, Keys.H, Keys.J
                    Form1.AP_Command("W")
                    LabelStatus.Text = "Moving West"
                Case Else
                    DoKeyStop = False
            End Select
        End If
    End Sub

    Private Sub ListBoxSR_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ListBoxSR.KeyUp
        KeyIsDown = False
        If DoKeyStop = True Then
            StopToolStripMenuItem.PerformClick()
            LabelStatus.Text = "Stop move sent to mount"
        End If

    End Sub

    Private Sub LabelKeyboard_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeyboard.Click
        LabelKeys.Visible = True

    End Sub

    Private Sub LabelKeys_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeys.Click
        LabelKeys.Visible = False

    End Sub
End Class