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

using System.Net;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FObjBase;

using System.Collections.ObjectModel;
using LiveCharts.Wpf;
using LiveCharts;
using LiveCharts.Helpers;
using FLY.OBJComponents.Client;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// PgGrid.xaml 的交互逻辑
    /// </summary>
    public partial class PgGrid : Page
    {
        PgGridVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        public PgGrid()
        {
            InitializeComponent();

            viewModel = new PgGridVm();
            this.DataContext = viewModel;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initParam"></param>
        /// <param name="gageService"></param>
        /// <param name="flyADService"></param>
        /// <param name="dynArea"></param>
        /// <param name="warningReasonWindow"></param>
        public void Init(
            InitParamServiceClient initParam,
            TDGageServiceClient gageService,
            FlyADServiceClient flyADService,
            DynArea dynArea,
            BufferWindow<FLY.OBJComponents.Common.FlyData_WarningHistory> warningReasonWindow
            )
        {
            viewModel.Init(initParam, gageService, flyADService);
            mircoGage.Init(dynArea, initParam, gageService, warningReasonWindow);
        }


        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);
        }
    }






}
