using SciChart.Charting.Visuals.RenderableSeries;
using SciChart.Charting.Visuals;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SciChart.Charting.Model.ChartSeries;

namespace SicUI.Models.PMs.Charting
{
    /// <summary>
    /// PMChartingV2View.xaml 的交互逻辑
    /// </summary>
    public partial class PMChartingV2View : UserControl
    {
        public PMChartingV2View()
        {
            InitializeComponent();
        }
    }

    public class SciChartMouseDownChangingLocationBehavior : Behavior<SciChartSurface>
    {



        public AxisInfo YAxisInfo
        {
            get { return (AxisInfo)GetValue(YAxisInfoProperty); }
            set { SetValue(YAxisInfoProperty, value); }
        }

        public static readonly DependencyProperty YAxisInfoProperty =
            DependencyProperty.Register("YAxisInfo", typeof(AxisInfo), typeof(SciChartMouseDownChangingLocationBehavior), new PropertyMetadata(null));



        public AxisInfo XAxisInfo
        {
            get { return (AxisInfo)GetValue(XAxisInfoProperty); }
            set { SetValue(XAxisInfoProperty, value); }
        }

        // Using a DependencyProperty as the backing store for XAxisInfo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty XAxisInfoProperty =
            DependencyProperty.Register("XAxisInfo", typeof(AxisInfo), typeof(SciChartMouseDownChangingLocationBehavior), new PropertyMetadata(null));





        public ObservableCollection<IRenderableSeriesViewModel> Target
        {
            get { return (ObservableCollection<IRenderableSeriesViewModel>)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(ObservableCollection<IRenderableSeriesViewModel>), typeof(SciChartMouseDownChangingLocationBehavior), new PropertyMetadata(null));


        protected override void OnAttached()
        {
            AssociatedObject.PreviewMouseDown += mousedown;
        }

        private void mousedown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                foreach (IRenderableSeriesViewModel series in Target)
                {
                    if (series.IsSelected)
                    {
                        double xvalue = 0, yvalue = 0;
                        if (double.TryParse(XAxisInfo.CursorFormattedDataValue, out xvalue) && double.TryParse(YAxisInfo.CursorFormattedDataValue, out yvalue))
                        {
                            ChartDataLineXV2 s = series as ChartDataLineXV2;

                            s.DataXOffset = xvalue;
                            s.DataOffset = yvalue;
                            s.IsSelected = false;
                        }
                    }
                }
            }

            e.Handled = true;   //不写会失去焦点
        }
    }

}
