using FLY.Thick.Base.IService;
using LiveCharts;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// PgGrid.xaml 的交互逻辑
    /// </summary>
    public partial class PgGrid : Page
    {
        PgGridVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        public PgGrid()
        {
            InitializeComponent();
            InitializeChart();
        }
        void InitializeChart()
        {
            //写在xaml ,设计模式会引发 最大值最小值=0 异常， 所以用代码方式编写
            //
            //异常内容：
            //LiveChartsException: One axis has an invalid range, it is or it tends to zero, please ensure your axis has a valid range
            var axis = chart1.AxisX[0];
            axis.SetBinding(LiveCharts.Wpf.Axis.LabelFormatterProperty, "XFormatter");
            axis.SetBinding(LiveCharts.Wpf.Axis.MaxValueProperty, "XUpper");
            axis.SetBinding(LiveCharts.Wpf.Axis.MinValueProperty, "XLower");


            axis = chart1.AxisY[0];
            axis.SetBinding(LiveCharts.Wpf.Axis.LabelFormatterProperty, "YFormatter");
            axis.SetBinding(LiveCharts.Wpf.Axis.MaxValueProperty, "YUpper");
            axis.SetBinding(LiveCharts.Wpf.Axis.MinValueProperty, "YLower");
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="container"></param>
        /// <param name="initParam"></param>
        /// <param name="gageService"></param>
        /// <param name="flyADService"></param>
        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IInitParamService initParam,
            ITDGageService gageService,
            IFlyADService flyADService
            )
        {
            viewModel = new PgGridVm();
            viewModel.Init(initParam, gageService, flyADService);

            this.DataContext = viewModel;
            container.BuildUp(mircoGage);
        }


        private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (!isDown)
                return;

            var vm = viewModel;
            var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
            var chart = (LiveCharts.Wpf.CartesianChart)sender;



            //lets get where the mouse is at our chart
            var mouseCoordinate = e.GetPosition(chart);

            //now that we know where the mouse is, lets use
            //ConverToChartValues extension
            //it takes a point in pixes and scales it to our chart current scale/values
            var p = chart.ConvertToChartValues(mouseCoordinate);


            //var series = chart.Series[0];

            var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
            if (closetsPoint == null)
                return;

            xEnd = closetsPoint.X;

            vm.MeasureValuePos = (int)(Math.Min(xBegin, xEnd) * vm.PosOfGrid);

            vm.MeasureWidthPos = (int)((Math.Abs(xEnd - xBegin) + 1) * vm.PosOfGrid);

        }
        private double xBegin;
        private double xEnd;
        private bool isDown = false;
        private void UIElement_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isDown = true;
            var vm = viewModel;
            var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
            var chart = (LiveCharts.Wpf.CartesianChart)sender;


            var mouseCoordinate = e.GetPosition(chart);
            var p = chart.ConvertToChartValues(mouseCoordinate);


            //var series = chart.Series[0];
            var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
            if (closetsPoint == null)
            {
                isDown = false;
                return;
            }
            xBegin = closetsPoint.X;

            vm.MeasureValuePos = (int)(xBegin * vm.PosOfGrid);

        }
        private void UIElement_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
            var vm = viewModel;
            var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
            var chart = (LiveCharts.Wpf.CartesianChart)sender;

            //lets get where the mouse is at our chart
            var mouseCoordinate = e.GetPosition(chart);

            //now that we know where the mouse is, lets use
            //ConverToChartValues extension
            //it takes a point in pixes and scales it to our chart current scale/values
            var p = chart.ConvertToChartValues(mouseCoordinate);

            //for X in this case we will only highlight the closest point.
            //lets use the already defined ClosestPointTo extension
            //it will return the closest ChartPoint to a value according to an axis.
            //here we get the closest point to p.X according to the X axis
            //var series = chart.Series[0];
            var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
            if (closetsPoint == null)
                return;
            xEnd = closetsPoint.X;

            vm.MeasureValuePos = (int)(Math.Min(xBegin, xEnd) * vm.PosOfGrid);

            vm.MeasureWidthPos = (int)((Math.Abs(xEnd - xBegin) + 1) * vm.PosOfGrid);
        }
    }






}
