Public Class mainForm
    Dim secondi As Int32
    Dim risp As Byte
    Dim command As String
    Dim processName As String



    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        inputText.Visible = False
        inputText.ShowInTaskbar = False
        If nupH.Text = ("") Or NupM.Text = ("") Then
            MsgBox("Il tempo inserito non è valido", 16, "ERRORE!")
            nupH.Text = 0
            NupM.Text = 0
            Exit Sub
        End If
        If inputText.txtMessage.Text = ("Inserisci qui il testo da visualizzare prima dello spegnimento...") And cmdVisualText.Checked Then
            MsgBox("Hai selezionato l'opzione di visualizzare un messaggio senza inserire il messaggio!", MsgBoxStyle.Exclamation, "Avviso OffTime")
            Exit Sub
        End If
        If txtcomp.Text = ("") Or txtcomp.Text = (" ") Or txtcomp.Text = ("Nome o IP computer") And rdbrmt.Checked Then
            MsgBox("Hai selezionato l'opzione per eseguire l'operazione in un computer remoto senza inserire il nome del computer!", MsgBoxStyle.Exclamation, "Avviso OffTime")
            Exit Sub
        End If
        If txtProcess.Text = "" And rdbEndProc.Checked Then
            MsgBox("Hai selezionato l'opzione di interruzione processo senza selezionarlo!", MsgBoxStyle.Exclamation, "Avviso OffTime")
            Exit Sub
        End If
        If nupH.Text = (0) And NupM.Text = (0) And (rdbExecImm.Checked = False) And (rdbExecTime.Checked = False) Then
            risp = MsgBox("Con il tempo impostato il computer eseguirà l'operazione immediatamente. Continuo?", 52, "Avviso OffTime")
            If risp = (7) Then
                Exit Sub
            End If
        End If
        If rdbExecTime.Checked Then
            Dim time_set As DateTime
            Try 'Permette compatibilità con windows 7 e i precedenti formati orario
                time_set = DateTime.Parse(DateTime.Now.Date & " " & nupH.Value & ":" & NupM.Value & ":00")
            Catch ex As Exception
                Try
                    time_set = DateTime.Parse(DateTime.Now.Date & " " & nupH.Value & "." & NupM.Value & ".00")
                Catch
                    MsgBox("Errore lettura formato data sistema. Utilizzare un formato compatibile.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Errore lettura data")
                End Try
            End Try
            Dim time_now As DateTime = DateTime.Now
            Dim time_to_event As TimeSpan = time_set.Subtract(time_now)
            secondi = time_to_event.TotalSeconds
            If secondi < 0 Then
                secondi = secondi + 86400 'se l'ora è antecedente aggiungo un giorno ai secondi
            End If
        ElseIf rdbExecTimer.Checked Then
            secondi = nupH.Text * 3600
            secondi = secondi + (NupM.Text * 60)
        ElseIf rdbExecImm.Checked Then
            execute_operation()
            Exit Sub
        End If
        grpTimeExec.Enabled = False
        grpSelectComputer.Enabled = False
        btnOk.Enabled = False
        btnClear.Enabled = True
        btnPausa.Enabled = Not rdbExecTime.Checked
        prgBarSeconds.Maximum = secondi
        prgBarSeconds.Value = secondi
        lblSeconds.Text = secondi
        Me.Size = New System.Drawing.Size(538, 452)
        Me.MaximumSize = New System.Drawing.Size(538, 452)
        Me.MinimumSize = New System.Drawing.Size(538, 452)
        grpSecondsRem.Visible = True
        tmrSeconds.Start()
    End Sub


    Private Sub tmrSeconds_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSeconds.Tick
        prgBarSeconds.Value = secondi
        lblSeconds.Text = secondi
        If secondi < 1 Then
            btnClear.PerformClick()
            execute_operation()
        End If
        If (secondi = 5) Then
            processSelect.Close()
            RedIcon.Visible = False

            grpOperetionExec.Enabled = False
            Me.Visible = True
            Me.ShowInTaskbar = True
        End If
        If secondi < (nupFirstOperation.Value + 1) Then
            If (secondi = nupFirstOperation.Value) Then
                If chkAlert.Checked Then
                    alertForm.Show()
                End If
                If cmdVisualText.Checked Then
                    If inputText.correct_message Then
                        inputText.Visible = True
                        inputText.ShowInTaskbar = True
                        inputText.Text = "Messaggio OffTime"
                        inputText.txtMessage.ReadOnly = True
                        inputText.btnClear.Enabled = False
                    Else
                        inputText.Close()
                    End If
                End If
            End If
            If grpFirstOperation.Enabled Then
                grpFirstOperation.Enabled = False
            End If
            If chkBeep.Checked Then
                Console.Beep()
            End If
            If chkSuono.Checked Then
                Beep()
            End If
        End If
        secondi = secondi - 1
    End Sub

    Private Sub execute_operation()
        command = "shutdown.exe"
        If rdbShutdown.Checked = True Then
            command = command & " /s /t 0"
        ElseIf rebRestart.Checked Then
            command = command & " /r /t 0"
        ElseIf rdbSusp.Checked Then
            command = "RUNDLL32.EXE powrprof.dll,SetSuspendState"
        ElseIf rdbDisconnect.Checked Then
            command = command & " /l"
        ElseIf rdbEndProc.Checked Then
            endProcess()
        ElseIf rdbSysBlock.Checked Then
            command = "RUNDLL32.EXE user32.dll,LockWorkStation"
        End If
        If chkForceOperation.Checked Then
            command = command & " /f"
        End If
        If rdbrmt.Checked Then
            command = command & " /m \\" & txtcomp.Text
        End If
        If rdbEndProc.Checked = (False) Then
            Shell(command)
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        tmrSeconds.Stop()
        alertForm.trmLamp.Stop()
        alertForm.Close()
        processSelect.Visible = False
        prgBarSeconds.Value = 0
        lblSeconds.Text = 0
        grpTimeExec.Enabled = True
        grpSelectComputer.Enabled = True
        grpOperetionExec.Enabled = True
        grpFirstOperation.Enabled = True
        btnOk.Enabled = True
        btnClear.Enabled = False
        prgBarSeconds.Style = ProgressBarStyle.Blocks
        grpSecondsRem.Visible = False
        Me.Size = New System.Drawing.Size(538, 370)
        Me.MaximumSize = New System.Drawing.Size(538, 370)
        Me.MinimumSize = New System.Drawing.Size(538, 370)
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        AboutBox1.Show()
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RedIcon.MouseDoubleClick
        Me.ShowInTaskbar = True
        Me.Visible = True
        RedIcon.Visible = False
    End Sub

    Private Sub btnRedIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRedIcon.Click
        Me.ShowInTaskbar = False
        Me.Visible = False
        RedIcon.Visible = True
        RedIcon.BalloonTipTitle = "OffTime"
        RedIcon.BalloonTipText = "OffTime è ancora in esecuzione. Fai doppio click qui per visualizzare la finestra."
        RedIcon.BalloonTipIcon = ToolTipIcon.Info
        RedIcon.ShowBalloonTip(1000)
    End Sub




    Private Sub rdbrmt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbrmt.CheckedChanged
        If rdbrmt.Checked Then
            txtcomp.Enabled = True
            rdbDisconnect.Enabled = False
            rdbSusp.Enabled = False
            rdbEndProc.Enabled = False
            processSelect.Visible = False
            rdbSysBlock.Enabled = False
            rdbShutdown.Checked = True
            lblCloseProc.Enabled = False
        Else
            txtcomp.Enabled = False
            txtcomp.Text = "Nome o IP computer"
            rdbDisconnect.Enabled = True
            rdbSusp.Enabled = True
            rdbSysBlock.Enabled = True
            rdbEndProc.Enabled = True
            lblCloseProc.Enabled = True
        End If
    End Sub


    Private Sub txtcomp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcomp.Click
        If txtcomp.Text = "Nome o IP computer" Then
            txtcomp.Text = String.Empty
        End If
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbSusp.CheckedChanged
        If rdbSusp.Checked Then
            chkForceOperation.Enabled = False
            chkForceOperation.Checked = False
        Else
            chkForceOperation.Enabled = True
        End If
    End Sub

    Private Sub rdbendproc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbEndProc.CheckedChanged
        If rdbEndProc.Checked Then
            chkForceOperation.Enabled = False
            chkForceOperation.Checked = False
            txtProcess.Visible = True
            processSelect.Show()
            processSelect.BtnUpgProcess.PerformClick()
        Else
            chkForceOperation.Enabled = True
            txtProcess.Visible = False
            processSelect.Visible = False
        End If
    End Sub

    Private Sub txtProcess_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProcess.TextChanged
        If txtProcess.Text <> "" Then
            txtProcess.BackColor = Color.Yellow
        Else
            txtProcess.BackColor = Color.White
        End If
    End Sub

    Public Sub endProcess()
        Dim PrcProcesso As System.Diagnostics.Process()
        Do
            PrcProcesso = Process.GetProcessesByName(txtProcess.Text)
            If (PrcProcesso.Length > 0) Then
                Try
                    PrcProcesso(0).Kill()
                Catch
                    MsgBox("Accesso da parte di OffTime al processo " & txtProcess.Text & " negato! Il processo potrebbe essere critico per la stabilità del sistema.", MsgBoxStyle.Exclamation, "Accesso negato a OffTime")
                    GoTo 10
                End Try
                System.Threading.Thread.Sleep(300)
            End If
        Loop While (PrcProcesso.Length > 0)
10:
        processSelect.Close()
        txtProcess.Text = ""
    End Sub

    Private Sub btnPausa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPausa.Click
        If btnPausa.Text = ("P&ausa") Then
            btnPausa.Text = "Ripren&di"
            tmrSeconds.Stop()
            prgBarSeconds.Style = ProgressBarStyle.Marquee
        Else
            btnPausa.Text = "P&ausa"
            tmrSeconds.Start()
            prgBarSeconds.Style = ProgressBarStyle.Blocks
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        nupH.Value = 0
        NupM.Value = 0
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Close()
    End Sub


    Private Sub btnNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If txtcomp.Text = ("") Or txtcomp.Text = (" ") Or txtcomp.Text = ("Nome o IP computer") And rdbrmt.Checked Then
            MsgBox("Hai selezionato l'opzione per eseguire l'operazione in un computer remoto senza inserire il nome del computer!", MsgBoxStyle.Exclamation, "Avviso OffTime")
            GoTo 10
        End If
        btnOk.Enabled = False
        btnClear.Enabled = True
        secondi = 0
        tmrSeconds.Start()
10:
    End Sub

    Private Sub rdblock_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbSysBlock.CheckedChanged
        If rdbSysBlock.Checked Then
            chkForceOperation.Checked = False
            chkForceOperation.Enabled = False
        Else
            chkForceOperation.Enabled = True
        End If
    End Sub


    Private Sub rdbExec0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbExecImm.CheckedChanged, rdbExecTimer.CheckedChanged, rdbExecTime.CheckedChanged
        If rdbExecTimer.Checked Or rdbExecTime.Checked Then
            grpFirstOperation.Enabled = True
            btnReset.Enabled = True
            nupH.Enabled = True
            NupM.Enabled = True
            lblH.Enabled = True
            lblM.Enabled = True
            lblTwoDot.Enabled = True
            If rdbExecTime.Checked Then
                nupH.Maximum = 23
            Else
                nupH.Maximum = 100
            End If
        Else
            btnReset.Enabled = False
            nupH.Enabled = False
            NupM.Enabled = False
            lblH.Enabled = False
            lblM.Enabled = False
            lblTwoDot.Enabled = False
            nupH.Value = 0
            NupM.Value = 0
            grpFirstOperation.Enabled = False
        End If
    End Sub

    Private Sub lblInsText_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblInsText.LinkClicked
        cmdVisualText.Checked = True
        inputText.Show()
        inputText.btnClear.Enabled = True
        inputText.txtMessage.ReadOnly = False
        inputText.Text = "Inserisci testo da visualizzare"
    End Sub

    Private Sub lblCloseProc_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblCloseProc.LinkClicked
        rdbEndProc.Checked = True
        processSelect.Show()
        processSelect.BtnUpgProcess.PerformClick()
    End Sub

    Private Sub cmdVisualText_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVisualText.CheckedChanged
        If cmdVisualText.Checked Then
            inputText.Show()
        End If
    End Sub

    Private Sub mainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If tmrSeconds.Enabled Then
            If MessageBox.Show("OffTime ha un'operazione in corso. Sei sicuro di voler uscire?", "Operazione OffTime in corso", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.No Then
                e.Cancel = True
            End If
        End If

    End Sub
End Class
