using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Analyzator_01
{
    public partial class Form1 : Form
    {
        UdpClient Klient = new UdpClient(55000);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Klient.EnableBroadcast = true;
            timer1.Start();
            labelPocetVzorku.Text = "Počet vzorků = " + (prubehySignalu1.DataCasy.Length).ToString();
            comboBox2.Items.Insert(0,(GlobalniPromene.Vzorkovani/1000000).ToString() + " MHz");
            comboBox2.SelectedIndex = 0;   
            
            textBox1.Text = (100).ToString();
            comboBox1.SelectedIndex = 2;    //us

            numericUpDown1.Maximum = prubehySignalu1.DataCasy.Length - 1; //maximalni hodnota pro offset
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Klient.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            while (Klient.Available > 0)
            { 
                IPEndPoint ep = new IPEndPoint(0,0);
                int posun = 10;
                byte[] data0 = Klient.Receive(ref ep);
                int novePole = 0;

                if ((posun % 2) == 1)
                    throw new Exception("posun - musi byt sude cislo");

                if ((data0.Length % 2) == 1) //lichy pocet bajtu - nemelo by nikdy nastat - merena data jsou 16bit
                    novePole = data0.Length - posun - 1; //odstraneni prvku z pole
                else
                    novePole = data0.Length - posun;

                byte[] data = new byte[novePole];
                Array.Copy(data0, posun, data , 0, data.Length);      

                byte[] DataCasy = new byte[data.Length / 2];
                byte[] DataHodnoty = new byte[data.Length / 2];

                //DataCasy[0] = data[0];
                //DataHodnoty[0] = data[1];

                for (int i = 0; i < data.Length; i+=2)
                {
                    DataCasy[i / 2] = data[i];
                }
                for (int i = 1; i < data.Length; i += 2)
                {
                    DataHodnoty[(i-1) / 2] = data[i];
                }

                labelPocetVzorku.Text = "Počet vzorků = " + (data.Length/2).ToString();
                numericUpDown1.Maximum = data.Length / 2 - 1 ;  //omezeni nastaveni offsetu

                prubehySignalu1.DataCasy = DataCasy;
                prubehySignalu1.DataHodnoty = DataHodnoty;
                prubehySignalu2.DataCasy = DataCasy;
                prubehySignalu2.DataHodnoty = DataHodnoty;
                prubehySignalu3.DataCasy = DataCasy;
                prubehySignalu3.DataHodnoty = DataHodnoty;
                prubehySignalu4.DataCasy = DataCasy;
                prubehySignalu4.DataHodnoty = DataHodnoty;
                prubehySignalu5.DataCasy = DataCasy;
                prubehySignalu5.DataHodnoty = DataHodnoty;
                prubehySignalu6.DataCasy = DataCasy;
                prubehySignalu6.DataHodnoty = DataHodnoty;
                prubehySignalu7.DataCasy = DataCasy;
                prubehySignalu7.DataHodnoty = DataHodnoty;
                prubehySignalu8.DataCasy = DataCasy;
                prubehySignalu8.DataHodnoty = DataHodnoty;
            }

            if (checkBox2.Checked == true)
            {
                timer1.Stop();
                button1.Text = "Start";
            }
                
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double Cas = System.Convert.ToDouble(textBox1.Text);        //pokud textove pole neobsahuje cislo, text se zbarvy do cervena
                if (Cas / (Math.Pow(1000, comboBox1.SelectedIndex)) > 1000 || Cas == 0 || Cas / (Math.Pow(1000, comboBox1.SelectedIndex)) < 1e-11) // omezeni zadavani <1 000 s; 10 ps>
                    throw new Exception();
                meritko1.Cas = Cas/(Math.Pow(1000,comboBox1.SelectedIndex));
                textBox1.ForeColor = Color.Black;               
            }
            catch
            {
                textBox1.ForeColor = Color.Red; 
            }
            

            prubehySignalu1.Cas = meritko1.Cas;
            prubehySignalu2.Cas = meritko1.Cas;
            prubehySignalu3.Cas = meritko1.Cas; 
            prubehySignalu4.Cas = meritko1.Cas;
            prubehySignalu5.Cas = meritko1.Cas;
            prubehySignalu6.Cas = meritko1.Cas;
            prubehySignalu7.Cas = meritko1.Cas;
            prubehySignalu8.Cas = meritko1.Cas;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(button1.Text == "Start")
            {
                if (checkBox2.Checked == false)
                {
                    timer1.Start();
                    button1.Text = "Stop";
                }
                else
                {
                    timer1.Start();
                }
                
            }
            else if (button1.Text == "Stop")
            {
                timer1.Stop();
                button1.Text = "Start";
            }
                
            
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            int pozice = (int)numericUpDown1.Value;
            double posledni_cas = 0;
            if(pozice > 0)
                posledni_cas = (double)prubehySignalu1.DataCasy[pozice - 1] / GlobalniPromene.Vzorkovani; //data jsou ve vsech 8 komponentach stejna

            prubehySignalu1.Offset = pozice;
            prubehySignalu2.Offset = pozice;
            prubehySignalu3.Offset = pozice;
            prubehySignalu4.Offset = pozice;
            prubehySignalu5.Offset = pozice;
            prubehySignalu6.Offset = pozice;
            prubehySignalu7.Offset = pozice;
            prubehySignalu8.Offset = pozice;

            
            double OffsetCas = 0;
            for (int j = 0; j < pozice; j++)
            {
                OffsetCas += (double)prubehySignalu1.DataCasy[j] / GlobalniPromene.Vzorkovani;
            }    
           
            if (OffsetCas >= 1)
            {
                labelOffset.Text = "+" + (OffsetCas).ToString("0.###") + "s";
                labelDelta.Text = "Δ= " + ((OffsetCas - (OffsetCas - posledni_cas)) * 1000 * 1000).ToString("0.###") + "us";
            }
            else if (OffsetCas * 1000 >= 1)
            {
                labelOffset.Text = "+" + (OffsetCas * 1000).ToString("0.###") + "ms";
                labelDelta.Text = "Δ= " + ((OffsetCas - (OffsetCas - posledni_cas)) * 1000 * 1000).ToString("0.###") + "us";
            }
            else if (OffsetCas * 1000 * 1000 >= 1)
            {
                labelOffset.Text = "+" + (OffsetCas * 1000 * 1000).ToString("0.###") + "us";
                labelDelta.Text = "Δ= " + ((OffsetCas - (OffsetCas - posledni_cas)) * 1000 * 1000).ToString("0.###") + "us";
            }
            else if (OffsetCas * 1000 * 1000 * 1000 >= 1)
            {
                labelOffset.Text = "+" + (OffsetCas * 1000 * 1000 * 1000).ToString("0.###") + "ns";
                labelDelta.Text = "Δ= " + ((OffsetCas - (OffsetCas - posledni_cas)) * 1000 * 1000 * 1000).ToString("0.###") + "ns";
            }
            else if (OffsetCas * 1000 * 1000 * 1000 * 1000 >= 1)
            {
                labelOffset.Text = "+" + (OffsetCas * 1000 * 1000 * 1000 * 1000).ToString("0.###") + "ps";
                labelDelta.Text = "Δ= " + ((OffsetCas - (OffsetCas - posledni_cas)) * 1000 * 1000 * 1000 * 1000).ToString("0.###") + "ps";
            }
            else if (OffsetCas == 0)
            {
                labelOffset.Text = "+0s";
                labelDelta.Text = "Δ= 0s";

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                double Cas = System.Convert.ToDouble(textBox1.Text);
                if (Cas / (Math.Pow(1000, comboBox1.SelectedIndex)) > 1000 || Cas == 0 || Cas / (Math.Pow(1000, comboBox1.SelectedIndex)) < 1e-11) // <1 000 s; 10 ps>
                    throw new Exception();
                meritko1.Cas = Cas / (Math.Pow(1000, comboBox1.SelectedIndex));
                textBox1.ForeColor = Color.Black;
            }
            catch
            {
                textBox1.ForeColor = Color.Red;
            }


            prubehySignalu1.Cas = meritko1.Cas;
            prubehySignalu2.Cas = meritko1.Cas;
            prubehySignalu3.Cas = meritko1.Cas;
            prubehySignalu4.Cas = meritko1.Cas;
            prubehySignalu5.Cas = meritko1.Cas;
            prubehySignalu6.Cas = meritko1.Cas;
            prubehySignalu7.Cas = meritko1.Cas;
            prubehySignalu8.Cas = meritko1.Cas;

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
            {
                prubehySignalu1.MeritkoZobrazeni = true;
                prubehySignalu2.MeritkoZobrazeni = true;
                prubehySignalu3.MeritkoZobrazeni = true;
                prubehySignalu4.MeritkoZobrazeni = true;
                prubehySignalu5.MeritkoZobrazeni = true;
                prubehySignalu6.MeritkoZobrazeni = true;
                prubehySignalu7.MeritkoZobrazeni = true;
                prubehySignalu8.MeritkoZobrazeni = true;
                
            }
            else
            {
                prubehySignalu1.MeritkoZobrazeni = false;
                prubehySignalu2.MeritkoZobrazeni = false;
                prubehySignalu3.MeritkoZobrazeni = false;
                prubehySignalu4.MeritkoZobrazeni = false;
                prubehySignalu5.MeritkoZobrazeni = false;
                prubehySignalu6.MeritkoZobrazeni = false;
                prubehySignalu7.MeritkoZobrazeni = false;
                prubehySignalu8.MeritkoZobrazeni = false;
            }
        }
    }
}
