using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FObjBase;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_GageInfo.xaml 的交互逻辑
    /// </summary>
    public partial class PgGageInfo : Page
    {
        #region 延时推送 MARKNO
        const int MARKNO_UPDATE = 1;
        #endregion

        IGageInfoService gageInfo = null;
        IGetSampleService getSample = null;
        IBorderSearchService borderSearch = null;
        IDynAreaService dynAreaService = null;
        IUnityContainer container;
        public PgGageInfo()
        {
            InitializeComponent();
            InitializeComponent2();
        }
        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IGageInfoService gageInfoService,
            IGetSampleService getSample,
            IBorderSearchService borderSearch,
            IDynAreaService dynAreaService
            )
        {
            this.gageInfo = gageInfoService;
            this.getSample = getSample;
            this.borderSearch = borderSearch;
            this.dynAreaService = dynAreaService;
            this.container = container;
            container.BuildUp(mircoGage);
        }
        private void Page_Loaded(object sender, RoutedEventArgs e1)
        {


            this.DataContext = gageInfo;

            gageInfo.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Data")
                {
                    DataBindAll();
                }
            };
            getSample.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Enable")
                {
                    update();
                }
            };

            for (int i = 0; i < getSample.Samples.Count(); i++)
            {
                getSample.Samples[i].PropertyChanged += (s, e) =>
                {
                    if ((e.PropertyName == "Enable") ||
                        (e.PropertyName == "Position") ||
                        (e.PropertyName == "OrgAD"))
                    {
                        update();
                    }
                };
            }
            for (int i = 0; i < getSample.Features.Count(); i++)
            {
                getSample.Features[i].PropertyChanged += (s, e) =>
                {
                    if ((e.PropertyName == "Enable") ||
                        (e.PropertyName == "StartPos") ||
                        (e.PropertyName == "EndPos"))
                    {
                        update();
                    }
                };
            }

            borderSearch.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Enable")
                {
                    update();
                }
            };

            borderSearch.Valid.PropertyChanged += (s, e) =>
            {
                update();
            };

            DataBindAll();
            update();
        }

        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();
            System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series();//样品
            // 
            // chart1
            // 
            chart1.BackColor = System.Drawing.Color.Transparent;

            chartArea1.Area3DStyle.Inclination = 15;
            chartArea1.Area3DStyle.IsClustered = true;
            chartArea1.Area3DStyle.IsRightAngleAxes = false;
            chartArea1.Area3DStyle.Perspective = 10;
            chartArea1.Area3DStyle.Rotation = 10;
            chartArea1.Area3DStyle.WallWidth = 0;
            chartArea1.AxisX.IsLabelAutoFit = false;
            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.ScrollBar.BackColor = System.Drawing.Color.AliceBlue;
            chartArea1.AxisX.ScrollBar.ButtonColor = System.Drawing.SystemColors.Control;
            chartArea1.AxisX.ScrollBar.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            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)))));
            chartArea1.AxisY.ScrollBar.BackColor = System.Drawing.Color.AliceBlue;
            chartArea1.AxisY.ScrollBar.ButtonColor = System.Drawing.SystemColors.Control;
            chartArea1.AxisY.ScrollBar.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(165)))), ((int)(((byte)(191)))), ((int)(((byte)(228)))));
            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.CursorX.IsUserEnabled = true;
            chartArea1.CursorX.IsUserSelectionEnabled = true;
            chartArea1.CursorX.SelectionColor = System.Drawing.SystemColors.Highlight;
            chartArea1.CursorY.IsUserEnabled = true;
            chartArea1.CursorY.IsUserSelectionEnabled = true;
            chartArea1.CursorY.SelectionColor = System.Drawing.SystemColors.Highlight;
            chartArea1.InnerPlotPosition.Auto = false;
            chartArea1.InnerPlotPosition.Height = 75F;
            chartArea1.InnerPlotPosition.Width = 86.32634F;
            chartArea1.InnerPlotPosition.X = 11.21863F;
            chartArea1.InnerPlotPosition.Y = 3.96004F;
            chartArea1.Name = "Default";
            chartArea1.Position.Auto = false;// true;
            chartArea1.Position.Height = 100;//
            chartArea1.Position.Width = 100;// 81.46518F;
            chartArea1.Position.X = 0;// 9.267409F;
            chartArea1.Position.Y = 0;// 10F;

            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            chart1.ChartAreas.Add(chartArea1);
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Enabled = true;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            legend1.Name = "Default";
            chart1.Legends.Add(legend1);
            //chart1.Location = new System.Drawing.Point(16, 48);
            chart1.Name = "chart1";


            //series1
            series1.BorderColor = System.Drawing.Color.DarkBlue;
            series1.BorderWidth = 3;

            series1.ChartArea = chartArea1.Name;
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Color = System.Drawing.Color.Blue;// System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series1.Legend = legend1.Name;
            series1.Name = "正向";
            //series1.MarkerSize = 6;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            try
            {
                series1.LegendText = Application.Current.FindResource("strForward") as string;
            }
            catch
            {
            }
            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;
            chart1.Series.Add(series1);


            //series2
            series2.BorderColor = System.Drawing.Color.DarkRed;
            series2.BorderWidth = 3;

            series2.ChartArea = chartArea1.Name;
            series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series2.Color = System.Drawing.Color.Red; //System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series2.Legend = legend1.Name;
            //series2.MarkerSize = 6;
            //series2.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series2.Name = "反向";
            try
            {
                series2.LegendText = Application.Current.FindResource("strBackward") as string;
            }
            catch
            {
            }
            series2.ShadowColor = System.Drawing.Color.Black;
            series2.ShadowOffset = 2;
            //series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            //series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;

            chart1.Series.Add(series2);

            //series3
            series3.Name = "样品";
            try
            {
                series3.LegendText = Application.Current.FindResource("strSample") as string;
            }
            catch
            {
            }
            series3.ChartArea = chartArea1.Name;
            series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
            series3.Color = System.Drawing.Color.Green;
            series3.BorderColor = System.Drawing.Color.DarkGreen;
            series3.BorderWidth = 3;

            series3.MarkerSize = 6;
            series3.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series3.MarkerBorderColor = System.Drawing.Color.DarkGreen;
            series3.MarkerColor = System.Drawing.Color.Green;

            series3.ShadowColor = System.Drawing.Color.Black;
            series3.ShadowOffset = 2;
            //series3.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            //series3.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;

            series3.Legend = legend1.Name;

            chart1.Series.Add(series3);
        }

        void DataBindAll()
        {
            string[] series = new string[2] { "正向", "反向" };
            List<int>[] datas = new List<int>[2] { gageInfo.ForwData, gageInfo.BackwData };
            if (gageInfo.PosOfGrid == 0)
                return;
            int datadatasize = gageInfo.PosLen / gageInfo.PosOfGrid;
            if (datadatasize > gageInfo.ForwData.Count)
                datadatasize = gageInfo.ForwData.Count;

            int max = Misc.MyBase.NULL_VALUE, min = Misc.MyBase.NULL_VALUE;
            for (int j = 0; j < 2; j++)
            {
                chart1.Series[j].Points.Clear();
                for (int i = 0; i < datadatasize; i++)
                {
                    int data = datas[j][i];
                    int pos = i * gageInfo.PosOfGrid;

                    chart1.Series[j].Points.AddXY(pos, data);

                    if (!Misc.MyBase.ISVALIDATA(data))
                    {
                        chart1.Series[j].Points[i].IsEmpty = true;
                    }
                    else
                    {
                        if (!Misc.MyBase.ISVALIDATA(max))
                        {
                            max = data;
                            min = data;
                        }
                        if (max < data)
                            max = data;
                        if (min > data)
                            min = data;
                    }
                }
            }
            //set max min
            //set max min
            if (max != min)
            {
                chart1.ChartAreas[0].AxisY.Maximum = max + (max - min) / 10;
                chart1.ChartAreas[0].AxisY.Minimum = min - (max - min) / 10;
                if (chart1.ChartAreas[0].AxisY.Minimum < 0)
                    chart1.ChartAreas[0].AxisY.Minimum = 0;

                if (chart1.ChartAreas[0].AxisY.Maximum > dynAreaService.DynArea.ADMax)
                    chart1.ChartAreas[0].AxisY.Maximum = dynAreaService.DynArea.ADMax;
            }
            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Maximum = gageInfo.PosLen;
        }
        void update()
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate ()
                {
                    System.Windows.Forms.DataVisualization.Charting.StripLinesCollection stripLines = chart1.ChartAreas[0].AxisX.StripLines;
                    stripLines.Clear();
                    mBorderSearchService_update();
                    mGetSampleService_update();
                }), this, MARKNO_UPDATE);


        }
        void mBorderSearchService_update()
        {
            System.Windows.Forms.DataVisualization.Charting.StripLinesCollection stripLines = chart1.ChartAreas[0].AxisX.StripLines;

            if (borderSearch.Enable)
            {
                System.Windows.Forms.DataVisualization.Charting.StripLine stripline = new System.Windows.Forms.DataVisualization.Charting.StripLine();
                stripline.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))));
                stripline.Interval = 0;
                stripline.IntervalOffset = borderSearch.Valid.Begin;
                stripline.StripWidth = borderSearch.Valid.Width;
                stripLines.Add(stripline);
            }
        }
        void mGetSampleService_update()
        {
            System.Windows.Forms.DataVisualization.Charting.StripLinesCollection stripLines = chart1.ChartAreas[0].AxisX.StripLines;
            //stripLines.Clear();

            System.Windows.Forms.DataVisualization.Charting.Series series = chart1.Series["样品"];
            series.Points.Clear();



            if (!getSample.Enable)
                return;

            for (int i = 0; i < getSample.Samples.Count(); i++)
            {
                var sample = getSample.Samples[i];
                if (sample.Enable)
                {
                    int data = sample.OrgAD;
                    int pos = sample.Position;
                    series.Points.AddXY(pos, data);
                }
            }

            for (int i = 0; i < getSample.Features.Count(); i++)
            {
                var feature = getSample.Features[i];
                if (feature.Enable)
                {
                    System.Windows.Forms.DataVisualization.Charting.StripLine stripline = new System.Windows.Forms.DataVisualization.Charting.StripLine();


                    if (i == 0)
                    {
                        try
                        {
                            stripline.Text = Application.Current.FindResource("strForwFeature") as string;
                        }
                        catch
                        {
                            stripline.Text = "正向特征";
                        }
                    }
                    else
                    {
                        try
                        {
                            stripline.Text = Application.Current.FindResource("strBackwFeature") as string;
                        }
                        catch
                        {
                            stripline.Text = "反向特征";
                        }
                    }
                    //stripline.TextAlignment = System.Drawing.StringAlignment.Near;
                    //stripline.TextLineAlignment = System.Drawing.StringAlignment.Near;
                    stripline.TextOrientation = System.Windows.Forms.DataVisualization.Charting.TextOrientation.Stacked;

                    stripline.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))));
                    stripline.Interval = 0;
                    stripline.IntervalOffset = feature.StartPos;
                    stripline.StripWidth = feature.EndPos - feature.StartPos + 1;
                    stripLines.Add(stripline);
                }
            }
        }
        void Page_Unloaded(object sender, RoutedEventArgs e)
        {

        }
        void button_start_Click(object sender, RoutedEventArgs e)
        {
            if (gageInfo.IsRunning)
                gageInfo.Stop();
            else
            {
                if (!WdPassword.Authorize("GageInfo"))
                    return;

                gageInfo.Start();
            }

        }
    }
}