using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace FLY.ControlLibrary
{
    public class GraphScan2:GraphScan
    {
        public GraphScan2():base()
        {
            mRefresh = new Refresh(this);
            
        }

        #region 刷新率控制
        class Refresh
        {
            /// <summary>
            /// 控制刷新率
            /// </summary>
            System.Windows.Threading.DispatcherTimer timer;
            bool updatedata_flag = false;

            /// <summary>
            /// 最快0.1s 刷新一次
            /// </summary>
            TimeSpan REFRESH_RATE = TimeSpan.FromSeconds(0.1);

            public Refresh(GraphScan2 graph)
            {
                timer = new System.Windows.Threading.DispatcherTimer();
                timer.Tick += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (UpdateData)
                    {
                        UpdateData = false;
                        graph.DataBindAll2();
                    }
                    timer.Stop();
                });

                timer.Interval = REFRESH_RATE;
                timer.Start();
            }

            public bool UpdateData
            {
                get { return updatedata_flag; }
                set
                {
                    updatedata_flag = value;
                    if (value)
                    {
                        if (!timer.IsEnabled)
                            timer.Start();
                    }
                }
            }
        }
        Refresh mRefresh;
        #endregion



        protected override void Test()
        {
            base.Test();

            ObservableCollection<int> datasource = new ObservableCollection<int>();

            // Populate series data
            Random random = new Random();

            for (int i = 0; i < 40; i++)
            {
                datasource.Add(random.Next(3000) - 1500 + Target);
            }

            DataSource2 = datasource;
        
        }
        protected override void InitializeComponent2()
        {
            base.InitializeComponent2();

            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

            series1.BorderColor = System.Drawing.Color.DarkBlue;
            series1.BorderWidth = 2;

            series1.ChartArea = "Default";
            //TODO
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            //series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;

            series1.Color = System.Drawing.Color.Blue;

            series1.Legend = "Default";
            series1.Name = "Series 2";
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;


            this.chart1.Series.Add(series1);
        }

        /// <summary>
        /// 数据源
        /// </summary>
        private ObservableCollection<int> _datasource;
        public virtual ObservableCollection<int> DataSource2
        {
            get
            {
                return _datasource;
            }
            set
            {
                _datasource = value;
                _datasource.CollectionChanged += dataSource_CollectionChanged;
                mRefresh.UpdateData = true;
            }
        }
        /// <summary>
        /// DataSource 里面的数据都是 EmptyValue 组成, 当修改数据时，应该clear ,再添加
        /// </summary>
        private bool DataSourceIsEmpty = false;
        /// <summary>
        /// 当一个数据都没有时，需要添加空数据
        /// </summary>
        public void CheckDataSourceEmpty2()
        {
            if ((DataSource2 == null) || (DataSource2.Count() == 0))
            {
                chart1.Series["Series 2"].Points.Clear();
                chart1.Series["Series 2"].Points.AddXY(FirstBoltNo, EmptyValue);
                chart1.Series["Series 2"].Points[0].IsEmpty = true;
                DataSourceIsEmpty = true;
            }
            else
            {
                DataSourceIsEmpty = false;
            }
        }
        void dataSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            mRefresh.UpdateData = true;
        }
        public void DataBindAll2()
        {
            double multi = Multi;
            int emptyvalue = EmptyValue;

            chart1.Series["Series 2"].Points.Clear();

            if (DataSource2 != null)
            {
                for (int i = 0; i < DataSource2.Count(); i++)
                {
                    UpdateOnePoint2(i);
                }
            }

            CheckDataSourceEmpty2();
        }
        public virtual void UpdateOnePoint2(int index)
        {
            double multi = Multi;

            int emptyvalue = EmptyValue;
            System.Windows.Forms.DataVisualization.Charting.DataPoint point;
            int x = FirstBoltNo + index;
            double y = (double)DataSource2.ElementAt(index);
            y = y / multi;
            if (index >= chart1.Series["Series 2"].Points.Count)
            {
                chart1.Series["Series 2"].Points.AddXY(x, y);
                point = chart1.Series["Series 2"].Points[index];
            }
            else
            {

                point = chart1.Series["Series 2"].Points[index];
                point.XValue = x;
                point.YValues[0] = y;

            }

            //设定空数据

            if (DataSource2[index] == emptyvalue)
            {
                point.IsEmpty = true;
            }
            else
            {
                point.IsEmpty = false;
            }
        }


        void GraphScan2_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AutoTarget")
            {
                if (IsAutoTarget)
                {
                    mRefresh.UpdateData = true;
                }
            }
            if ((e.PropertyName == "Target") || (e.PropertyName == "Alarm") || (e.PropertyName == "YRange") || (e.PropertyName == "YRangePercent") || (e.PropertyName == "IsAutoTarget") || (e.PropertyName == "IsPercent") || (e.PropertyName == "AutoTarget"))
            {
                mRefresh.UpdateData = true;
            }
        }
    }
}
