using FLY.Thick.Base.UI;
using FLY.Thick.FilmCasting.IService;
using LiveCharts;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using Misc;
using System;
using System.Collections.Generic;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Unity;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    /// <summary>
    /// ColumnRangeScanGraph.xaml 的交互逻辑
    /// </summary>
    public partial class ScanMmGraph : UserControl
    {
        ScanMmGraphVm viewModel;
        ScanMmGraphItemParam graphparam;
        public ScanMmGraph()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(int id, IUnityContainer container,
            IBulkDbService bulkDbService, ParamDictionary paramDictionary, IFilmCastingProfileService calenderProfileService)
        {
            //查找参数
            graphparam = ScanMmGraphParams.Current.Items.Find(p => p.ID == id);
            if (graphparam == null)
            {
                graphparam = new ScanMmGraphItemParam() { ID = id };
                ScanGraphParams.Current.Items.Add(graphparam);
                ScanGraphParams.Current.Save();
            }

            //获取设备
            viewModel = new ScanMmGraphVm();

            viewModel.Init(graphparam, bulkDbService, paramDictionary, calenderProfileService.Param);

            this.DataContext = viewModel;

        }

        private void Button_info_click(object sender, RoutedEventArgs e)
        {
            if (!WdPassword.Authorize("GraphConfig"))
                return;

            WdScanMmGraphConfig window = new WdScanMmGraphConfig();
            window.Init(graphparam, viewModel);
            window.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            window.ShowDialog();
        }


        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.Series[0];
            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.SetSelected(xBegin, xBegin);

        }
        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;
            xEnd = closetsPoint.X;

            vm.SetSelected(xBegin, xEnd);
        }

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

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


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

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

            xEnd = closetsPoint.X;

            vm.SetSelected(xBegin, xEnd);

        }
    }
}
