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 System.Collections.ObjectModel;
using System.Windows.Forms.DataVisualization.Charting;
using System.ComponentModel;
using FObjBase;
using System.Windows.Threading;
using System.Diagnostics;
using FLY.FilmCasting.AutoDie.IService;
using MultiLayout.UiModule;
using Unity;
using MultiLayout;
using System.IO;
using LiveCharts.Wpf;
using LiveCharts.Helpers;
using LiveCharts;

namespace FLY.FilmCasting.AutoDie.UI.Client.UiModule
{
    /// <summary>
    /// Page_Graph.xaml 的交互逻辑
    /// </summary>
    public partial class AutoDieGraph : UserControl
    {
        IUnityContainer container;
        
        AutoDieGraphVm viewModel;

        public AutoDieGraph()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IFeedbackHeatService feedback,
            IHeatBufService heatBuf,
            IHeatCellService heatCell)
        {
            this.container = container;
            viewModel = new AutoDieGraphVm();
            viewModel.Init(feedback, heatBuf, heatCell, btnClearH);
            this.DataContext = viewModel;
        }

        private void btnInfoClick(object sender, RoutedEventArgs e)
        {
            var p = container.Resolve<PgSetup>();
            FlyLayoutManager.NavigationService.Navigate(p);
        }

        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 = chart.Series[0];

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

            xEnd = closetsPoint.X;

            vm.SelectBoltNo = (int)Math.Min(xBegin, xEnd);

            vm.SelectBoltNoWidth = (int)Math.Abs(xEnd - xBegin) + 1;

        }
        private double xBegin;
        private double xEnd;
        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);


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

            vm.SelectBoltNo = (int)Math.Min(xBegin, xEnd);
            vm.SelectBoltNoWidth = 1;

        }
        private void UIElement_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
            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);

            //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.SelectBoltNo = (int)Math.Min(xBegin, xEnd);

            vm.SelectBoltNoWidth = (int)Math.Abs(xEnd - xBegin) + 1;
        }
    }

}
