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.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

using System.Net;

using FLY.Thick.Base.IService;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.Common;
using System.IO;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_ScanCorr.xaml 的交互逻辑
    /// </summary>
    public partial class Page_ScanCorr : Page, INotifyPropertyChanged
    {
        public ScanCorrService mScanCorrService = null;
        InitParamService mInitParamService;

        private int _groupindex = 0;
        public int GroupIndex
        {
            get
            {
                return _groupindex;
            }
            set
            {
                _groupindex = value;
                NotifyPropertyChanged("GroupIndex");
            }
        }

        public Page_ScanCorr()
        {
            InitializeComponent();
            InitializeComponent2();
           

            this.PropertyChanged += new PropertyChangedEventHandler(Page_scancorr_PropertyChanged);

            stackpanel_groupindex.DataContext = this;
        }

        void Page_scancorr_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "GroupIndex")
            {
                if (GroupIndex == 0)
                {
                    grid_result.DataContext = mScanCorrService.Group0;
                    DataBindAll(chart1, mScanCorrService.Group0);
                }
                else
                {
                    grid_result.DataContext = mScanCorrService.Group1;
                    DataBindAll(chart1, mScanCorrService.Group1);
                }
            }
        }
        
        void InitializeComponent_chart2(System.Windows.Forms.DataVisualization.Charting.Chart 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();
            System.Windows.Forms.DataVisualization.Charting.Series series2 = 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 = "Default";
            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 = "Default";
            //series1.MarkerSize = 6;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series1.Name = "正向";
            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 = "Default";
            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 = "Default";
            //series2.MarkerSize = 6;
            //series2.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series2.Name = "反向";
            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);
        }
        void InitializeComponent2()
        {
            InitializeComponent_chart2(chart1);
        }


        public void Init(IPEndPoint serverep) 
        {
            mScanCorrService = new ScanCorrService();
            mScanCorrService.Group0.PropertyChanged += new PropertyChangedEventHandler(Group0_PropertyChanged);
            mScanCorrService.Group1.PropertyChanged += new PropertyChangedEventHandler(Group1_PropertyChanged);
            mScanCorrService.PropertyChanged += new PropertyChangedEventHandler(mScanCorrService_PropertyChanged);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mScanCorrService.ID);

            mInitParamService = new InitParamService();
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mInitParamService.ID);
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            this.DataContext = mScanCorrService;
            this.grid_null.DataContext = mInitParamService;
            GroupIndex = 0;
            validrange_update();
        }

        void Group1_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "ForwDatas") || (e.PropertyName == "BackwDatas"))
            {
                if (GroupIndex == 1)
                {
                    DataBindAll(chart1, mScanCorrService.Group1);
                }
            }
        }

        void Group0_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "ForwDatas") || (e.PropertyName == "BackwDatas"))
            {
                if (GroupIndex == 0)
                {
                    DataBindAll(chart1, mScanCorrService.Group0);
                }
            }
        }

        void mScanCorrService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "ValidBegin") || (e.PropertyName == "ValidEnd")) 
            {
                validrange_update();
            }
        }
        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            mScanCorrService.Dispose();
        }
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        void DataBindAll(
            System.Windows.Forms.DataVisualization.Charting.Chart chart,
            IScanCorrGroup group)
        {
            string[] series = new string[2] { "正向", "反向" };
            if (mScanCorrService.PosOfGrid <= 0)
                return;
            int datadatasize = group.ForwDatas.Count();

            chart.Series[0].Points.Clear();

            chart.Series[1].Points.Clear();

            int max = Misc.MyBase.NULL_VALUE, min = Misc.MyBase.NULL_VALUE;


            for (int i = 0; i < group.ForwDatas.Count(); i++)
            {
                int data = group.ForwDatas[i];
                int pos = i * mScanCorrService.PosOfGrid;

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

                if (!Misc.MyBase.ISVALIDATA(data))
                {

                    chart1.Series[0].Points[i].IsEmpty = true;
                }
                else 
                {
                    if (!Misc.MyBase.ISVALIDATA(max))
                    {
                        max = data;
                        min = data;
                    }
                    if (max < data)
                        max = data;
                    if (min > data)
                        min = data;
                }
            }

            for (int i = 0; i < group.BackwDatas.Count(); i++)
            {
                int data = group.BackwDatas[i];
                int pos = i * mScanCorrService.PosOfGrid;

                chart1.Series[1].Points.AddXY(pos, data);
                if (!Misc.MyBase.ISVALIDATA(data))
                {
                    chart1.Series[1].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
            if (max!=min)
            {
                chart1.ChartAreas[0].AxisY.Maximum = max;
                chart1.ChartAreas[0].AxisY.Minimum = min;
            }
            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Maximum = mScanCorrService.PosLen;
        }


        void validrange_update()
        {

            System.Windows.Forms.DataVisualization.Charting.StripLinesCollection stripLines = chart1.ChartAreas[0].AxisX.StripLines;
            stripLines.Clear();
                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 = mScanCorrService.ValidBegin;
                stripline.StripWidth = mScanCorrService.ValidEnd - mScanCorrService.ValidBegin + 1;
                stripLines.Add(stripline);
        }


        private void button_start_click(object sender, RoutedEventArgs e)
        {
            mScanCorrService.Start(
                GroupIndex);
        }

        private void button_clear_click(object sender, RoutedEventArgs e)
        {
            mScanCorrService.Clear(GroupIndex);
        }

        private void button_group0_click(object sender, RoutedEventArgs e)
        {
            GroupIndex = 0;
        }

        private void button_group1_click(object sender, RoutedEventArgs e)
        {
            GroupIndex = 1;
        }


        private void button_smooth_click(object sender, RoutedEventArgs e)
        {
            mScanCorrService.Smooth(GroupIndex);
        }

        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            mScanCorrService.Apply();
        }
    }
    public class Group2boolConverter : IValueConverter
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int groupindex = (int)value;

            if (groupindex == 0)
                return false;
            else
                return true;
        }


        #endregion

        #region IValueConverter 成员


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool b = (bool)value;
            if (b == false)
                return 0;
            else
                return 1;
        }

        #endregion
    }
}
