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

namespace Analyzator_01
{
    public partial class PrubehySignalu : UserControl
    {
        private byte[] _DataCasy = { 250, 250, 250, 250, 250, 5,5,5, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250};
        private byte[] _DataHodnoty = { 0x41, 0x42, 0x43, 0x44, 0x55, 0x66,0x56,0x66, 0x56, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F };
        //private byte[] _DataCasy = null;
        //private byte[] _DataHodnoty = null;
        private int _Bit = 0;
        private double _Cas = 0;
        private int _Offset = 0;
        private bool _MeritkoZobrazeni = false;

        public bool MeritkoZobrazeni
        {
            get { return _MeritkoZobrazeni; }
            set { _MeritkoZobrazeni = value; Invalidate(); }
        }

        public int Offset
        {
            get { return _Offset; }
            set 
            { 
                _Offset = value;
                Invalidate();  
            }
        }

        public byte[] DataCasy
        {
            get { return _DataCasy; }
            set { _DataCasy = value; Invalidate(); }
        }
        public byte[] DataHodnoty
        {
            get { return _DataHodnoty; }
            set { _DataHodnoty = value; Invalidate(); }
        }

        public double Cas
        {
            get { return _Cas; }
            set { _Cas = value; Invalidate(); }
        }

        public int Bit
        {
            get { return _Bit; }
            set 
            {
                if (value < 0 || value > 7)
                    throw new Exception("Bit musí být zvolen z rozsahu 0 - 7");
                if (value == _Bit)
                    return;
                _Bit = value;
                Invalidate(); 
            }
        }
      
        public PrubehySignalu()
        {
            InitializeComponent();

            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
           
            e.Graphics.FillRectangle(Brushes.LightGreen, ClientRectangle);

            if(_MeritkoZobrazeni)
                for (int j = 1; j < 10; j++)
                    e.Graphics.DrawLine(Pens.Gray, j * Width / 10, 0, j * Width / 10, Height);

            if (_DataHodnoty == null || _DataCasy == null)
            {
                e.Graphics.DrawString("Nejsou žádná data",SystemFonts.DefaultFont,Brushes.Black,ClientRectangle,new StringFormat(){ Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center});
                return;
            }
            byte maska = (byte)(1 << _Bit);
            const double vzorkovani = GlobalniPromene.Vzorkovani; //Hz
            double k = Width / (_Cas * vzorkovani);
            
            bool jedna = false;
            bool nula = false;
            bool pjedna = false;
            bool pnula = false;
            int N = 0;
            int i = 1;


            byte vzorek0 = _DataHodnoty[0 + _Offset];
            int pixel = 0;
            int novyPixel = 0;
            
            jedna |= (vzorek0 & maska) != 0;
            nula |= (vzorek0 & maska) == 0;
            

            while (i < (_DataHodnoty.Length - _Offset) && N * k < Width)
            {
                byte vzorek = _DataHodnoty[i +_Offset];  //hodnota od pozice novyPixel ->
                int skok = _DataCasy[i - 1 + _Offset];
                N += skok;
                novyPixel = (int)(Math.Round(N * k));
                

                if (pixel < novyPixel)
                {   
                    if(jedna && nula)
                    {
                        e.Graphics.DrawLine(Pens.Red, pixel, 0, novyPixel, Height - 1);
                        e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, novyPixel, 0);
                    }
                    if (jedna)
                        e.Graphics.DrawLine(Pens.Red, pixel, 0, novyPixel, 0);
                    if (nula)
                        e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, novyPixel, Height - 1);


                    if ((jedna && pnula) || (nula && pjedna))
                        e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, pixel, 0);


                    pjedna = jedna;
                    pnula = nula;

                    pixel = novyPixel;
                    jedna = nula = false;
                }
                jedna |= (vzorek & maska) != 0;
                nula |= (vzorek & maska) == 0;

                i++;
            }

            N += _DataCasy[i - 1 + _Offset];
            novyPixel = (int)(Math.Round(N* k));
            if (novyPixel > Width)
                novyPixel = Width;

            if (jedna && nula)
            {
                e.Graphics.DrawLine(Pens.Red, pixel, 0, novyPixel, Height - 1);
                e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, novyPixel, 0);
            }
            if (jedna)
                e.Graphics.DrawLine(Pens.Red, pixel, 0, novyPixel, 0);
            if (nula)
                e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, novyPixel, Height - 1);


            if ((jedna && pnula) || (nula && pjedna))
                e.Graphics.DrawLine(Pens.Red, pixel, Height - 1, pixel, 0);

        }
    }
}
