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.Net;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using ThickTcpUiInWindow.Converter;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_Curve.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Curve : Page
    {

        CurveServiceClient mCurveService;

        public Page_Curve()
        {
            InitializeComponent();
            InitializeComponent2();
        }

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


            legend1.Name = "Default";
            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;
            
            this.chart1.Legends.Add(legend1);
            //this.chart1.Location = new System.Drawing.Point(16, 32);

            series1.Name = "Series 1";
            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.Circle;
            
            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;



            this.chart1.Series.Add(series1);

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

        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            if (Window_Password.Authorize(2))
            {
                mCurveService.Apply();
                FLY.ControlLibrary.Window_Tip.Show("应用成功",
                    null,
                    TimeSpan.FromSeconds(2));
            }
        }
        public void Init(IPEndPoint serverep) 
        {
            mCurveService = new CurveServiceClient(FLY.Thick.Base.OBJ_INTERFACE.OBJ_INTERFACE_ID.CURVE_ID);

            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mCurveService.ID);

            this.DataContext = mCurveService;

            this.datagrid.ItemsSource = mCurveService.Curves;

            mCurveService.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mCurveService_PropertyChanged);
            mCurveService.Curves.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Curves_CollectionChanged);
            //DataBindAll();
        }
        void Page_Loaded(object sender, RoutedEventArgs e)
        {

        }

        void mCurveService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Flag") 
            {
                DataBindAll();
            }
        }
        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            mCurveService.Dispose();
        }

        void _c_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            DataBindAll();
        }

        void Curves_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (CurveCell c in e.NewItems)
                {
                    c.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_c_PropertyChanged);
                }
                DataBindAll();
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                foreach (CurveCell c in e.OldItems)
                {
                    DataBindAll();
                }
            }
        }

        void DataBindAll()
        {

            chart1.Series["Series 1"].Points.Clear();
            for (int i = 0; i < mCurveService.Curves.Count(); i++)
            {
                int ad = mCurveService.Curves[i].AD;
                int thick = mCurveService.Curves[i].Value;

                chart1.Series["Series 1"].Points.AddXY(thick / 100.0, ad);

                if ((i+1) <= (mCurveService.Curves.Count() - 1))
                {
                    //补齐点， 以thick=100 步进
                    int step = 100;
                    bool isDescending = true;//降序排列
                    if (mCurveService.Curves[i].AD < mCurveService.Curves[i + 1].AD)
                        isDescending = false;

                    while(true)
                    {
                        if (isDescending)
                        {
                            ad -= step;
                            if (ad <= mCurveService.Curves[i + 1].AD)
                                break;
                        }
                        else
                        {
                            ad += step;
                            if (ad >= mCurveService.Curves[i + 1].AD)
                                break;
                        }

                        thick = mCurveService.AD2Value(ad, AD2ValueFlag.NoRevised);
                        chart1.Series["Series 1"].Points.AddXY(thick / 100.0, ad);
                        chart1.Series["Series 1"].Points[chart1.Series["Series 1"].Points.Count() - 1].MarkerStyle = 
                            System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                    }
                }
            }
        }

        private void button_scale_Click(object sender, RoutedEventArgs e)
        {
            if (mCurveService.Curves.Count > 1)
            {
                Window_ADScale w = new Window_ADScale();
                w.Old = mCurveService.Curves[0].AD;
                w.New = mCurveService.Curves[0].AD;
                w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
                if (w.ShowDialog()==true) 
                {
                    if ((w.Old != w.New) &&( w.New!=0) && (w.Old!=0))
                    {
                        double scale = (double)w.New / w.Old;
                        for (int i = 0; i < mCurveService.Curves.Count(); i++) 
                        {
                            mCurveService.Curves[i].AD = (int)(scale * mCurveService.Curves[i].AD);
                        }
                    }
                }
            }
        }
    }
    public class CurveTypeConverter : EnumToIsCheckedConverter<CurveType>
    {

    }
    public class CurveCorrectWayConverter : EnumToIsCheckedConverter<CurveCorrectWay>
    {

    }
}
