Imports System.Threading
Imports Microsoft.VisualBasic

Public Class Form_DATE_TIME
    Dim tickBegin As Long
    Dim tickEnd As Long
    Dim Status 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 ButtonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClose.Click
        Me.Hide()

    End Sub

    Private Sub ButtonSetDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetDate.Click
        Dim AP_Date As String

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Or Form1.IsConnected = True Then
            AP_Date = ":SC" & Format(DateMonth.Value, "00") & "/" _
                    & Format(DateDay.Value, "00") & "/" _
                    & Format(DateYear.Value, "00") & "#"

            Status = ""
            RespGood = True
            Resp = ""
            ' Set Date
            SendReceiveAP(AP_Date, "Set Date", True, 66)
            If RespGood = False Then Exit Sub
            Form1.Rsp = Resp

            If Microsoft.VisualBasic.Right(Resp, 1) = "#" Then
                Status = "Successful."
            Else
                If Resp = "" Then
                    Status = "No response"
                Else
                    Status = "Invalid - " & Resp
                End If
            End If
            ToolStripStatusLabel1.Text = Status
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonSetTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetTime.Click
        Dim AP_Time As String

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            AP_Time = ":SL" & Format(TimeHour.Value, "00") & ":" _
                    & Format(TimeMinute.Value, "00") & ":" _
                    & Format(TimeSecond.Value, "00") & "#"

            Status = ""
            RespGood = True
            Resp = ""
            ' Set Time
            SendReceiveAP(AP_Time, "Set Time", True, 1)
            If RespGood = False Then Exit Sub
            Form1.Rsp = Resp

            If Resp = "1" Then
                Status = "Successful."
            Else
                If Resp = "" Then
                    Status = "No response"
                Else
                    Status = "Invalid " & Resp
                End If
            End If
            ToolStripStatusLabel1.Text = Status
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonGetDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGetDate.Click
        Dim AP_DATE As String
        Dim ADate() As String

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            Try
                RespGood = True
                Resp = ""
                ' Get Date
                SendReceiveAP(":GC#", "Date", True, 0)
                If RespGood = False Then Exit Sub
                AP_DATE = Resp
                Form1.Rsp = AP_DATE
                ToolStripStatusLabel1.Text = "Date from mount " & AP_DATE

                If AP_DATE = "" Then
                    ToolStripStatusLabel1.Text = "GTOCP2 or no response"
                Else
                    ADate = Split(AP_DATE, "/")
                    DateMonth.Value = ADate(0)
                    DateDay.Value = ADate(1)
                    DateYear.Value = Mid(ADate(2), 1, 2)
                End If
            Catch ex As Exception
                Form1.LogScope("Error < ", Color.Black, Resp)
                ToolStripStatusLabel1.Text = "Date error from mount " & Resp
            End Try
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonGetTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGetTime.Click
        Dim AP_TIME As String

        If Form1.SerialPort1.IsOpen = True Or Form1.SocketIsOpen = True Then
            RespGood = True
            Resp = ""
            ' Get Time
            SendReceiveAP(":GL#", "Time", True, 0)
            If RespGood = False Then Exit Sub
            AP_TIME = Resp
            Form1.Rsp = AP_TIME
            ToolStripStatusLabel1.Text = "Time from mount " & AP_TIME

            If AP_TIME = "" Then
                ToolStripStatusLabel1.Text = "No response"
            Else
                TimeHour.Value = Mid(AP_TIME, 1, 2)
                TimeMinute.Value = Mid(AP_TIME, 4, 2)
                TimeSecond.Value = Mid(AP_TIME, 7, 2)
            End If
        Else
            ToolStripStatusLabel1.Text = "Not connected"
        End If

    End Sub

    Private Sub ButtonCurDateTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCurDateTime.Click
        Dim TimeString As String
        Dim DateString As String
        Dim DT() As String
        Dim TM() As String

        TimeString = Format(Now(), "HH:mm:ss")
        TM = Split(TimeString, ":")
        DateString = Format(Now(), "Short Date")
        DT = Split(DateString, "/")

        TimeHour.Value = TM(0)
        TimeMinute.Value = TM(1)
        TimeSecond.Value = TM(2)

        DateMonth.Value = DT(0)
        DateDay.Value = DT(1)
        If DT(2).Length = 4 Then
            DateYear.Value = Mid(DT(2), 3, 2)
        Else
            DateYear.Value = DT(2)
        End If

        ToolStripStatusLabel1.Text = "Initialized to PC current date/time"

    End Sub

    Private Sub Form_DATE_TIME_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

    End Sub
End Class