using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// X轴显示为 mm 
    /// </summary>
    public class GraphScan3:GraphScan,IGraphBase3 
    {
        #region IGraphBase3
        /// <summary>
        /// mm/分区
        /// </summary>
        public static readonly DependencyProperty MmOfBoltProperty =
            DependencyProperty.Register("MmOfBolt", typeof(double), typeof(GraphScan3), new PropertyMetadata(
                10.0,
                new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    (d as GraphScan3).NotifyPropertyChanged("MmOfBolt");
                })));
        /// <summary>
        /// mm/分区
        /// </summary>
        public double MmOfBolt
        {
            get
            {
                return (double)GetValue(MmOfBoltProperty);
            }
            set
            {
                if (value != MmOfBolt)
                {
                    SetValue(MmOfBoltProperty, value);
                    NotifyPropertyChanged("MmOfBolt");
                }
            }
        }


        /// <summary>
        /// X轴间隔 单位分区
        /// </summary>
        public static readonly DependencyProperty XIntervalProperty =
            DependencyProperty.Register("XInterval", typeof(int), typeof(GraphScan3), new PropertyMetadata(
                5,
                new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    (d as GraphScan3).NotifyPropertyChanged("XInterval");
                })));
        /// <summary>
        /// X轴间隔 单位分区
        /// </summary>
        public int XInterval
        {
            get
            {
                return (int)GetValue(XIntervalProperty);
            }
            set
            {
                
                if (value != XInterval)
                {
                    SetValue(XIntervalProperty, value);
                    NotifyPropertyChanged("XInterval");
                }
            }
        }
        #endregion


        public GraphScan3():base()
        {
            PropertyChanged += GraphScan3_PropertyChanged;
        }

        private void GraphScan3_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "MmOfBolt")
            {
                mRefresh.UpdateData = true;
            }
            else if (e.PropertyName == "XInterval")
            {
                mRefresh.UpdateX = true;
            }
        }

        protected override void InitializeComponent2()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

            // 
            // chart1
            // 
            chartArea1.Name = "Default";
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.IsMarksNextToAxis = false;
            chartArea1.AxisX.Interval = XInterval;
            chartArea1.AxisX.IsLabelAutoFit = true;
            chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;

            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));

            
            //this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(78)))), ((int)(((byte)(160)))), ((int)(((byte)(34)))));
            //chartArea1.BackColor = this.chart1.BackColor;
            //chartArea1.BackColor = System.Drawing.Color.Transparent;
            //chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            
            //chartArea1.Position.Height = 100;// 42F;
            //chartArea1.Position.Width = 100;// 88F;
            //chartArea1.Position.X = 0;// 3F;
            //chartArea1.Position.Y = 0;// 10F;

            //chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            chart1.ChartAreas.Add(chartArea1);
            

            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 2;

            series1.ChartArea = chartArea1.Name;
            series1.ChartType = ChartType;//System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;

            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series1.MarkerSize = 6;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series1.Name = "Series 1";
            //series1.ShadowColor = System.Drawing.Color.Black;
            //series1.ShadowOffset = 1;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;


            this.chart1.Series.Add(series1);

            chart1.Name = "chart1";


            this.chart1.BackColor = System.Drawing.Color.Transparent;

            this.chart1.PrePaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(Chart1_PrePaint);

        }



        protected override void UpdateOnePoint(int index)
        {
            double multi = Multi;
            int emptyvalue = EmptyValue;
            DataPoint point;
            double v;
            if (DataSource[index] == emptyvalue)
                v = double.NaN;
            else
                v = DataSource[index] / multi;

            if (index >= chart1.Series[0].Points.Count)
            {
                chart1.Series[0].Points.AddXY(
                FirstBoltNo + index, v);
                point = chart1.Series["Series 1"].Points[index];
            }
            else
            {
                point = chart1.Series["Series 1"].Points[index];

                point.YValues[0] = v;
                point.XValue = FirstBoltNo + index;
            }

            point.AxisLabel = (point.XValue * MmOfBolt).ToString("F0") + "mm";

            //设定空数据
            if (double.IsNaN(point.YValues[0]))
            {
                point.IsEmpty = true;

                System.Drawing.Color c = System.Drawing.Color.Transparent;
                point.MarkerColor = c;
                point.BorderColor = c;
                point.Color = c;
            }
            else
            {
                point.IsEmpty = false;

                System.Drawing.Color c;

                double alarm;
                double yrange;
                double target;
                bool ispercent;
                GetActualTargetAlarm(out target, out alarm, out yrange, out ispercent);

                double threshold1 = alarm * multi;
                double threshold2 = threshold1 * 2;
                target *= multi;


                if ((DataSource[index] > (target + threshold2)) || (DataSource[index] < (target - threshold2)))
                {
                    c = System.Drawing.Color.Red;
                }
                else if ((DataSource[index] > (target + threshold1)) || (DataSource[index] < (target - threshold1)))
                {
                    c = System.Drawing.Color.Orange;
                }
                else
                {
                    c = System.Drawing.Color.Green;
                }

                if (ChartType == SeriesChartType.Line)
                {
                    point.MarkerColor = c;
                    point.BorderColor = chart1.Series["Series 1"].BorderColor;
                    point.Color = chart1.Series["Series 1"].Color;
                }
                else
                {
                    point.MarkerColor = c;
                    point.BorderColor = c;
                    point.Color = c;
                }
            }
        }

        /// <summary>
        /// 与 FirstBoltNo,FirstIndex,LastIndex 相关
        /// </summary>
        public override void UpdateXRange()
        {
            int firstBoltNo = FirstBoltNo;
            int first = FirstIndex;
            int last = LastIndex;
            if (first > last)
            {
                //交换
                int swap = last;
                last = first;
                first = swap;
            }
            else if (first == last)
            {
                last = first + 10;
            }
            // Set manual minimum and maximum values.
            Axis axis = chart1.ChartAreas["Default"].AxisX;

            axis.Minimum = firstBoltNo + first;
            axis.Maximum = firstBoltNo + last;
            axis.Interval = XInterval;
            //0必须在 写着0mm

            axis.IntervalOffset = (0 - axis.Minimum) % XInterval;
        }
    }
}
