using FLY.Thick.FilmCasting.IService;
using Misc;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace FLY.Thick.FilmCasting.UI.Server
{
    /// <summary>
    /// ColumnRangeScanGraph.xaml 的交互逻辑
    /// </summary>
    public partial class ScanGraph : UserControl
    {
        public ScanGraph()
        {
            InitializeComponent();
        }

        public void Init(
            ScanGraphVm viewModel)
        {
            this.DataContext = viewModel;

            //下面是 livechart 的 bug
            //没有 触发 ChartUpdater.Timer 的事件前，就算 axisX.DataContext 有值， binding 也无效， 无法获取数据

            axisX_0.SetBinding( LiveCharts.Wpf.Axis.LabelFormatterProperty, new Binding(nameof(viewModel.XFormatter)) { Source = viewModel });
            axisX_0.SetBinding( LiveCharts.Wpf.Axis.MinValueProperty, new Binding(nameof(viewModel.XMin)) { Source = viewModel });
            axisX_0.SetBinding( LiveCharts.Wpf.Axis.MaxValueProperty, new Binding(nameof(viewModel.XMax)) { Source = viewModel });
            axisX_0.Separator.SetBinding( LiveCharts.Wpf.Separator.StepProperty, new Binding(nameof(viewModel.XStep)) { Source = viewModel });


            axisY_0.SetBinding( LiveCharts.Wpf.Axis.LabelFormatterProperty, new Binding(nameof(viewModel.YFormatter)) { Source = viewModel });
            axisY_0.SetBinding( LiveCharts.Wpf.Axis.MinValueProperty, new Binding(nameof(viewModel.YMin)) { Source = viewModel });
            axisY_0.SetBinding( LiveCharts.Wpf.Axis.MaxValueProperty, new Binding(nameof(viewModel.YMax)) { Source = viewModel });

            axisY_0.Sections[0].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.YMax)) { Source = viewModel });
            axisY_0.Sections[1].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.YMin)) { Source = viewModel });
            axisY_0.Sections[2].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.Tolerance2YMax)) { Source = viewModel });
            axisY_0.Sections[3].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.Tolerance2YMin)) { Source = viewModel });
            axisY_0.Sections[4].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.ToleranceYMax)) { Source = viewModel });
            axisY_0.Sections[5].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.ToleranceYMin)) { Source = viewModel });
            axisY_0.Sections[6].SetBinding( LiveCharts.Wpf.AxisSection.ValueProperty, new Binding(nameof(viewModel.Target)) { Source = viewModel });

            series_0.SetBinding(LiveCharts.Wpf.Column2Series.YAxisCrossingProperty, new Binding(nameof(viewModel.Target)) { Source = viewModel });
            series_0.SetBinding(LiveCharts.Wpf.Column2Series.ValuesProperty, new Binding(nameof(viewModel.Values)) { Source = viewModel });
            series_0.SetBinding(LiveCharts.Wpf.Column2Series.ConfigurationProperty, new Binding(nameof(viewModel.Mapper)) { Source = viewModel });

        }

        public void ChartUpdate()
        {
            chart.Update(true, true); //force chart redraw
        }
    }
}
