using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Diagnostics; 
namespace MonitorPC
{
    public partial class FrmPrincipal : Form
    {
        public FrmPrincipal()
        {
            InitializeComponent();
        }
        private void FrmPrincipal_Load(object sender, EventArgs e)
        {
            txtIp.Text = "200.44.32.12";
            txtTiempo.Value = 3;
            butEmpezar.Enabled = true;
            butDetener.Enabled = false;
            empezarCiclo();
        }
        private void empezarCiclo() {
            decimal vvalor=0;
            vvalor = txtTiempo.Value;
            tiempo.Interval = Convert.ToInt32(vvalor) * 60000;
            tiempo.Enabled = true;
            butEmpezar.Enabled = false;
            butDetener.Enabled = true;
            this.lblRespuesta.Text = "";
            txtTiempo.Enabled = false;
            txtIp.Enabled = false;
        }
        private void butEmpezar_Click(object sender, EventArgs e)
        {
            empezarCiclo();
        }
        private void butDetener_Click(object sender, EventArgs e)
        {
            tiempo.Enabled = false;
            butEmpezar.Enabled = true;
            butDetener.Enabled = false;
            this.lblRespuesta.Text = "";
            txtTiempo.Enabled = true;
            txtIp.Enabled = true;
        }
        private void reiniciarTarjetasdeRed()
        {
            ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("CMD.EXE", "/C ipconfig /release");
            info.Verb = "open";
            System.Diagnostics.Process.Start(info);
            info = new System.Diagnostics.ProcessStartInfo("CMD.EXE", "/C ipconfig /renew");
            info.Verb = "open";
            System.Diagnostics.Process.Start(info); 
        }
        private void reiniciarPC()
        {
            ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("CMD.EXE", "/C shutdown /r");
            info.Verb = "open";
            System.Diagnostics.Process.Start(info);
        }
   
        private void tiempo_Tick(object sender, EventArgs e)
        {
            bool vhayError = false;
            try
            {
                this.lblRespuesta.Text = "- - - - - -";
                Ping Pings = new Ping();
                IPStatus status;
                listMensajes.Items.Add("Ejecutando Ping...!");
                if (Pings.Send(txtIp.Text).Status == IPStatus.Success)
                {
                    status = Pings.Send(txtIp.Text).Status;
                    this.lblRespuesta.Text = "Ping Succeded !!!";
                    listMensajes.Items.Add("Ping Succeded !!!");
                    this.lblRespuesta.ForeColor = Color.Green;
                    vhayError = false;
                }
                else
                {
                    status = Pings.Send(txtIp.Text).Status;
                    this.lblRespuesta.Text = status.ToString() + " !!!";
                    this.lblRespuesta.ForeColor = Color.Red;
                    tiempo.Enabled = false;
                    vhayError = true;
                    listMensajes.Items.Add("!!! Ping Error !!!");
                }
            }
            catch (System.Net.NetworkInformation.PingException ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message, "Tienda On Line - Error");
                Console.Write(ex.Message);
                listMensajes.Items.Add("!!! Ping Error !!!");
                listMensajes.Items.Add(ex.Message);
                tiempo.Enabled = false;
                vhayError = true;
            }
            if (vhayError == true) {
                listMensajes.Items.Add("Inicio Reiniciando Tarjetas");
                reiniciarTarjetasdeRed();
                tiempo.Enabled = true;
                listMensajes.Items.Add("Fin Reiniciando Tarjetas");
            }
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo("http://computacionysistemas.blogspot.com/");
            Process.Start(sInfo);
        }
        private void FrmPrincipal_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
                trayBar.Visible = true;
            }
        }
        private void trayBar_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            trayBar.Visible = false;
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            trayBar.Visible = false;
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void mostrarMilisegundos() {
            decimal vvalor = 0;
            vvalor = txtTiempo.Value;
            vvalor= Convert.ToInt32(vvalor) * 60000;
            labMilisegundos.Text = vvalor + " Milisegundos";
        }
        private void txtTiempo_ValueChanged(object sender, EventArgs e)
        {
            mostrarMilisegundos();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            reiniciarPC();
        }
    }
}