using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FLY.Simulation.Blowing;

namespace FLYAD7_Simulation_Wpf
{
    
    /// <summary>
    /// WindowBlowing.xaml 的交互逻辑
    /// </summary>
    public partial class WindowBlowing : Window
    {
        public Blowing mBlowing;
        public WindowBlowing()
        {
            InitializeComponent();
            InitializeComponent_chart1();
            InitializeComponent_chart2();
        }
        public void InitializeComponent_chart1()
        {
            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.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.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.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.Name = "Default";
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            this.chart1.ChartAreas.Add(chartArea1);



            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Enabled = false;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            legend1.Name = "Default";
            this.chart1.Legends.Add(legend1);
            //this.chart1.Location = new System.Drawing.Point(16, 32);
            this.chart1.Name = "chart1";
            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 3;
            series1.ChartArea = "Default";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
            series1.Legend = "Default";
            //series1.MarkerSize = 8;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
            series1.Name = "Series 1";
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;


            chartArea1.CursorX.IsUserEnabled = true;
            chartArea1.CursorX.Position = 2;
            chartArea1.CursorX.SelectionColor = System.Drawing.Color.Gray;


            this.chart1.Series.Add(series1);

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

        public void InitializeComponent_chart2()
        {
            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.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.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.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.Name = "Default";
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            this.chart2.ChartAreas.Add(chartArea1);
            this.chart2.Name = "chart2";

            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 3;
            series1.ChartArea = "Default";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
            series1.Legend = "Default";
            //series1.MarkerSize = 8;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
            series1.Name = "Series 1";
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;


            chartArea1.CursorX.IsUserEnabled = true;
            chartArea1.CursorX.Position = 2;
            chartArea1.CursorX.SelectionColor = System.Drawing.Color.Gray;


            this.chart2.Series.Add(series1);

            this.chart2.BackColor = System.Drawing.Color.Transparent;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = mBlowing;
            this.spPlc.DataContext = mBlowing.mHMI;
            mBlowing.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mBlowing_PropertyChanged);
            DataBindAll();
            chart1.ChartAreas["Default"].CursorX.Position = mBlowing.TestPos;

            DataBindAll_chart2();

        }

        void mBlowing_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(mBlowing.TestPos)) 
            {
                chart1.ChartAreas["Default"].CursorX.Position = mBlowing.TestPos;
            }
            else if (e.PropertyName == nameof(mBlowing.AfterDatas)) 
            {
                this.Dispatcher.Invoke(new Action(DataBindAll2_chart2));
                this.Dispatcher.Invoke(new Action(DataBindAll2));
            }
        }
        public void DataBindAll()
        {
            chart1.Series["Series 1"].Points.Clear();
            for (int i = 0; i < mBlowing.AfterDatas.Count(); i++)
            {
                chart1.Series["Series 1"].Points.AddXY(i, mBlowing.AfterDatas[i]);
            }
            int max = mBlowing.AfterDatas.Max();
            int min = mBlowing.AfterDatas.Min();
            int avg = (int)mBlowing.AfterDatas.Average();
            if (max == min)
            {
                max = (int)(avg * 1.1);
                min = (int)(avg * 0.9);
            }
            chart1.ChartAreas["Default"].AxisY.Maximum = max + (max - min);
            chart1.ChartAreas["Default"].AxisY.Minimum = min - (max - min);
            chart1.Invalidate();
        }
        public void DataBindAll2()
        {
            for (int i = 0; i < mBlowing.AfterDatas.Count(); i++)
            {
                chart1.Series["Series 1"].Points[i].SetValueY(mBlowing.AfterDatas[i]);
            }
            chart1.Invalidate();
        }

        public void DataBindAll_chart2()
        {
            chart2.Series["Series 1"].Points.Clear();
            for (int i = 0; i < mBlowing.mAirRing2.Heats.Count(); i++)
            {
                chart2.Series["Series 1"].Points.AddXY(i, mBlowing.mAirRing2.Heats[i]);
            }
            chart2.ChartAreas["Default"].AxisY.Maximum = 100;
            chart2.ChartAreas["Default"].AxisY.Minimum = 0;
            chart2.Invalidate();
        }
        public void DataBindAll2_chart2()
        {
            for (int i = 0; i < mBlowing.mAirRing2.Heats.Count(); i++)
            {
                chart2.Series["Series 1"].Points[i].SetValueY(mBlowing.mAirRing2.Heats[i]);
            }
            chart2.Invalidate();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            mBlowing.Test();
        }

        private void btnSaveClick(object sender, RoutedEventArgs e)
        {
            mBlowing.Save();
            MessageBox.Show("已经保存数据，请重启");
        }
    }
}
