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

namespace SnimaceStisku
{
    public partial class FlowGraph : System.Windows.Forms.PictureBox
    {
        Int32 FMinValue = 0;
        Int32 FMaxValue = 100;
        Int32 FVertikalnichPricek = 10;
        Int32 FHorizontalnichPricek = 5;
        Int32 FMargin = 20;
        Int32 FLeftMargin = 40;
        List<FlowGraphData> FDataSegment = new List<FlowGraphData>();
        Point FZeroPoint = new Point();
        Int32 FVertikalniSirka = 0;
        Int32 FHorizontalniSirka = 0;
        //Boolean Initialized = false;
        public Int32 MinValue
        {
            get
            {
                return FMinValue;
            }
            set
            {
                if (value < FMaxValue)
                {
                    FMinValue = value;
                    this.Invalidate();
                }
            }
        }
        public Int32 MaxValue
        {
            get
            {
                return FMaxValue;
            }
            set
            {
                if (value > FMinValue)
                {
                    FMaxValue = value;
                    this.Invalidate();
                }
            }
        }
        public FlowGraph()
        {
            InitializeComponent();
        }

        protected override void OnResize(EventArgs e)
        {

            base.OnResize(e);
            FZeroPoint = new Point(FLeftMargin, this.Height - FMargin);
            FHorizontalniSirka = (Int32)Math.Round((this.Width - FMargin - FLeftMargin) / (Double)(FVertikalnichPricek - 1));
            FVertikalniSirka = (Int32)Math.Round((this.Height - 2 * FMargin) / (Double)(FHorizontalnichPricek - 1));
            foreach (FlowGraphData data in FDataSegment)
                data.Resize(this.Width - FLeftMargin - FMargin);
        }

        protected override void OnCreateControl()
        {

            base.OnCreateControl();
            AddDataSegment(Brushes.Red);
            AddDataSegment(Brushes.Green);
        }       

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics canvas = e.Graphics;
            Brush pozadi = Brushes.White;
            Brush linky = Brushes.Black;
            canvas.FillRectangle(pozadi, new Rectangle(0, 0, this.Width, this.Height));
            UInt16 i = 0;
            for (i = 0; i < FHorizontalnichPricek; i++)
            {
                canvas.DrawLine(new Pen(linky), FZeroPoint.X, FZeroPoint.Y - i * FVertikalniSirka, FZeroPoint.X + (FVertikalnichPricek - 1) * FHorizontalniSirka, FZeroPoint.Y - i * FVertikalniSirka);
                canvas.DrawString(((Int32)Math.Round(FMinValue + i * ((FMaxValue - FMinValue) / (Double)(FHorizontalnichPricek - 1)))).ToString(), new Font("Arial", 12,FontStyle.Bold), linky, 10, FZeroPoint.Y - i * FVertikalniSirka - 8);
            }
            for (i = 0; i < FVertikalnichPricek; i++)
            {
                canvas.DrawLine(new Pen(linky), FZeroPoint.X + i * FHorizontalniSirka, FZeroPoint.Y, FZeroPoint.X + i * FHorizontalniSirka, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka);
            }
            foreach (FlowGraphData data in FDataSegment)
                canvas.DrawLines(new Pen(data.Color, 4), data.Data);
            //canvas.DrawLine(new Pen(pozadi, 5), FZeroPoint.X, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka - 3, this.Width, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka - 3);
            canvas.FillRectangle(pozadi, FZeroPoint.X, 0, this.Width - FZeroPoint.X, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka);
            canvas.FillRectangle(pozadi, FZeroPoint.X, FZeroPoint.Y + 1, this.Width - FZeroPoint.X, this.Height - FZeroPoint.Y - 1);
            //canvas.DrawLine(new Pen(Brushes.AntiqueWhite, 1), FZeroPoint.X, FZeroPoint.Y , FZeroPoint.X + (FVertikalnichPricek - 1) * FHorizontalniSirka, FZeroPoint.Y);
            //canvas.DrawLine(new Pen(Brushes.AntiqueWhite, 1), FZeroPoint.X, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka, FZeroPoint.X + (FVertikalnichPricek - 1) * FHorizontalniSirka, FZeroPoint.Y - (FHorizontalnichPricek - 1) * FVertikalniSirka);
        }

        public void AddDataSegment(Brush Color)
        {
            FDataSegment.Add(new FlowGraphData(FZeroPoint, FMinValue, FMaxValue, this.Height - 2*FMargin, this.Width - FLeftMargin - FMargin, Color));
            //(FHorizontalnichPricek - 1) * FVertikalniSirka
        }

        public void ClearDataSegments()
        {
            FDataSegment.Clear();
        }

        public void Clear()
        {
            foreach (FlowGraphData data in FDataSegment)
                data.Clear();
        }

        public void AddValues(Int32[] Values)
        {
            if (Values.Length != FDataSegment.Count)
                return;
            UInt16 i = 0;
            for (i = 0; i < Values.Length; i++)
                FDataSegment[i].AddValue(Values[i]);
            Invalidate();
        }

        public void AddValue(Int32 Value)
        {
            if (FDataSegment.Count != 1)
                return;
            FDataSegment[0].AddValue(Value);
            Invalidate();
        }
    }
}
