using FLY.Thick.Base.UI;
using FLY.Thick.FilmCasting.IService;
using LiveCharts;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using Misc;
using System.Windows;
using System.Windows.Input;
using Unity;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    /// <summary>
    /// TrendGraph2.xaml 的交互逻辑
    /// </summary>
    public partial class TrendGraph2 : System.Windows.Controls.UserControl
    {
        TrendGraph2Vm trendGraphVm;
        TrendGraphItemParam trendGraphParam;

        ScanGraphVm scanGraphVm;
        ScanGraphItemParam scanGraphParam;
        public TrendGraph2()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(int id,
            IBulkDbService bulkDb,
            ParamDictionary paramDictionary,
            IFilmCastingProfileService profileService
            )
        {

            trendGraphParam = TrendGraphParams.Current.Items.Find(p => p.ID == id);
            if (trendGraphParam == null)
            {
                trendGraphParam = new TrendGraphItemParam() { ID = id };
                TrendGraphParams.Current.Items.Add(trendGraphParam);
                TrendGraphParams.Current.Save();
            }
            trendGraphVm = new TrendGraph2Vm();
            trendGraphVm.Init(trendGraphParam, bulkDb, profileService.Param);
            this.grid_trend.DataContext = trendGraphVm;




            scanGraphParam = ScanGraphParams.Current.Items.Find(p => p.ID == id);
            if (scanGraphParam == null)
            {
                scanGraphParam = new ScanGraphItemParam() { ID = id };
                ScanGraphParams.Current.Items.Add(scanGraphParam);
                ScanGraphParams.Current.Save();
            }
            scanGraphVm = new ScanGraphVm();
            scanGraphVm.Init(scanGraphParam, bulkDb, paramDictionary, profileService.Param);
            this.grid_scan.DataContext = scanGraphVm;



            this.trendGraphVm.PropertyChanged += TrendGraphVm_PropertyChanged;
            this.scanGraphVm.PropertyChanged += ScanGraphVm_PropertyChanged;

        }

        private void TrendGraphVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(trendGraphVm.SelectedId))
            {
                scanGraphVm.Number = trendGraphVm.SelectedId;
            }
        }

        private void ScanGraphVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(scanGraphVm.Number))
            {
                if (scanGraphVm.Number != trendGraphVm.SelectedId) {
                    trendGraphVm.ClearSelected();
                }
            }
        }

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

            TrendGraph2Config window = new TrendGraph2Config();
            window.Init(trendGraphParam, trendGraphVm);
            window.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            window.ShowDialog();
        }

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

            WdScanGraphConfig window = new WdScanGraphConfig();
            window.Init(scanGraphParam, scanGraphVm);
            window.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            window.ShowDialog();
        }

        private void UIElement_MouseDown(object sender, MouseButtonEventArgs e)
        {

            var vm = trendGraphVm;

            var chart = (LiveCharts.Wpf.CartesianChart)sender;
            var series = chart.Series[0];


            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)
            {
                return;
            }
            vm.SetSelected((int)(closetsPoint.X));
        }
    }
}
