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.Coating;

namespace FLYAD7_Simulation_Wpf
{
    /// <summary>
    /// WindowCoating.xaml 的交互逻辑
    /// </summary>
    public partial class WindowCoating : Window
    {
        Coating mCoating;
        List<double> mTrendData=new List<double>();//1cm 一个数据
        int FilmLength_cm=0;
        List<double> datas = new List<double>();
        public WindowCoating()
        {
            InitializeComponent();
            InitializeComponent_chart1();
            InitializeComponent_chart2();
        }

        public void Init(Coating coating) 
        {
            mCoating = coating;
            hmi_cc.DataContext = mCoating.hmi_cc.NowCtrl;
            inner_cc.DataContext = mCoating.inner_cc.NowCtrl;
            hmi.DataContext = mCoating.hmi;

            

            this.DataContext = coating;

            DataBindAll();

            mCoating.DatasChanged += new Action(mCoating_DatasChanged);
            mCoating.AvgChanged += new Action<double, int>(mCoating_AvgChanged);
        }

        void mCoating_AvgChanged(double arg1, int arg2)
        {
            int filmlength_cm = (int)(arg1*100);
            if(FilmLength_cm == filmlength_cm)
            {

            }
            else
            {
                FilmLength_cm = filmlength_cm;
                if (datas.Count() > 0)
                {
                    this.Dispatcher.Invoke(new Action<int>((d) =>
                    {
                        chart1.Series["Trend"].Points.AddXY(FilmLength_cm / 100.0, d / 100.0);
                        while (chart1.Series["Trend"].Points.Count() > 5 * 100)
                        {
                            chart1.Series["Trend"].Points.RemoveAt(0);
                        }
                        chart1.ChartAreas["Trend"].AxisX.Minimum = chart1.Series["Trend"].Points[0].XValue;
                        chart1.ChartAreas["Trend"].AxisX.Maximum = chart1.Series["Trend"].Points[0].XValue + 5;
                        chart1.Invalidate();
                    }), (int)(datas.Average()));
                }
                datas.Clear();
            }
            datas.Add(arg2);
        }

        void mCoating_DatasChanged()
        {
            this.Dispatcher.Invoke(new Action(DataBindAll));
        }
        private void button_set_out_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textbox_pumpset_out.Text)) 
            {
                MessageBox.Show("格式出错");
                return;
            }
            else if (string.IsNullOrEmpty(textbox_leftset_out.Text))
            {
                MessageBox.Show("格式出错");
                return;
            }
            else if (string.IsNullOrEmpty(textbox_rightset_out.Text))
            {
                MessageBox.Show("格式出错");
                return;
            }
            else
            {
                double pumpset = double.Parse(textbox_pumpset_out.Text);
                double leftset = double.Parse(textbox_leftset_out.Text);
                double rightset = double.Parse(textbox_rightset_out.Text);

                mCoating.hmi_cc.SetCoatingCtrl(pumpset, leftset, rightset);
            }
        }
        private void button_set_inner_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textbox_pumpset_inner.Text)) 
            {
                MessageBox.Show("格式出错");
                return;
            }
            else if (string.IsNullOrEmpty(textbox_leftset_inner.Text))
            {
                MessageBox.Show("格式出错");
                return;
            }
            else if (string.IsNullOrEmpty(textbox_rightset_inner.Text))
            {
                MessageBox.Show("格式出错");
                return;
            }
            else
            {
                double pumpset = double.Parse(textbox_pumpset_inner.Text);
                double leftset = double.Parse(textbox_leftset_inner.Text);
                double rightset = double.Parse(textbox_rightset_inner.Text);

                mCoating.inner_cc.SetCoatingCtrl(pumpset, leftset, rightset);
            }
        }


        void InitializeComponent_chart1()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea_trend = new System.Windows.Forms.DataVisualization.Charting.ChartArea();           
            System.Windows.Forms.DataVisualization.Charting.Series series_trend = new System.Windows.Forms.DataVisualization.Charting.Series();

            // 
            // chart1
            // 

            this.chart1.Name = "chart1";



            chartArea_trend.Name = "Trend";
            chartArea_trend.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea_trend.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_trend.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_trend.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea_trend.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_trend.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_trend.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea_trend.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_trend.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chartArea_trend.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            chartArea_trend.CursorX.IsUserEnabled = true;
            chartArea_trend.CursorX.IsUserSelectionEnabled = true;
            chartArea_trend.CursorX.SelectionColor = System.Drawing.SystemColors.Highlight;
            chartArea_trend.CursorY.IsUserEnabled = true;
            chartArea_trend.CursorY.IsUserSelectionEnabled = true;
            chartArea_trend.CursorY.SelectionColor = System.Drawing.SystemColors.Highlight;

            chartArea_trend.AxisX.LabelStyle.Format = @"{0:0}";
            this.chart1.ChartAreas.Add(chartArea_trend);

            //this.chart1.Location = new System.Drawing.Point(16, 32);

            series_trend.Name = "Trend";

            series_trend.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series_trend.BorderWidth = 3;
            series_trend.ChartArea = chartArea_trend.Name;
            series_trend.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_trend.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

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

            this.chart1.Series.Add(series_trend);

            this.chart1.BackColor = System.Drawing.Color.Transparent;
        }
        void InitializeComponent_chart2()
        {

            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea_scan = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Series series_scan = new System.Windows.Forms.DataVisualization.Charting.Series();

            // 
            // chart1
            // 

            this.chart2.Name = "chart2";



            chartArea_scan.Name = "Scan";
            chartArea_scan.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea_scan.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_scan.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_scan.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea_scan.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_scan.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_scan.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea_scan.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea_scan.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chartArea_scan.ShadowColor = System.Drawing.Color.Transparent;
            chartArea_scan.CursorX.IsUserEnabled = true;
            chartArea_scan.CursorX.IsUserSelectionEnabled = true;
            chartArea_scan.CursorX.SelectionColor = System.Drawing.SystemColors.Highlight;
            chartArea_scan.CursorY.IsUserEnabled = true;
            chartArea_scan.CursorY.IsUserSelectionEnabled = true;
            chartArea_scan.CursorY.SelectionColor = System.Drawing.SystemColors.Highlight;
            chart2.ChartAreas.Add(chartArea_scan);

            //this.chart1.Location = new System.Drawing.Point(16, 32);

            series_scan.Name = "Scan";

            series_scan.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series_scan.BorderWidth = 3;
            series_scan.ChartArea = chartArea_scan.Name;
            series_scan.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_scan.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

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

            chart2.Series.Add(series_scan);

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

        void DataBindAll()
        {
            chart2.Series["Scan"].Points.Clear();
            for (int i = 0; i < mCoating.Datas_Horizontal.Count(); i++)
            {
                chart2.Series["Scan"].Points.AddXY(i, mCoating.Datas_Horizontal[i].Thick / 100.0);
            }
            chart2.Invalidate();
        }
        
    }
    public class CoatingModeConverter : EnumToIsCheckedConverter<FLY.Simulation.Coating.CoatingCtrl.COATINGMODE>
    {

    }
}