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;
using Unity;
using GalaSoft.MvvmLight.Command;
using LiveCharts.Wpf;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_ScanCorr.xaml 的交互逻辑
    /// </summary>
    public partial class PgScanCorr : Page
    {
        PgScanCorrVm viewModel;
        public PgScanCorr()
        {
            InitializeComponent();
        }


        [InjectionMethod]
        public void Init(IUnityContainer container,
            IScanCorrService scanCorrService,
            IInitParamService initParamService,
            ITDGageService gageService
            )
        {
            viewModel = new PgScanCorrVm();
            viewModel.Init(scanCorrService, initParamService, gageService);
            this.DataContext = viewModel;

            container.BuildUp(mircoGage);
        }


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

            var vm = viewModel;
            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 = vm.Series[0];

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

            //pEnd = closetsPoint;
            pEnd = p;
            vm.Select(PgScanCorrVm.SelectType.Move, pBegin, pEnd);

        }

        private System.Windows.Point pBegin;
        private System.Windows.Point pEnd;
        private bool isDown = false;
        private void UIElement_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isDown = true;
            var vm = viewModel;
            var chart = (LiveCharts.Wpf.CartesianChart)sender;


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

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

            vm.Select(PgScanCorrVm.SelectType.Click, pBegin, pBegin);

        }
        private void UIElement_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
            var vm = viewModel;
            var series = vm.Series[0];
            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;
            pEnd = p;
            vm.Select(PgScanCorrVm.SelectType.Cancel, pBegin, pEnd);
        }
    }


    
}
