using GalaSoft.MvvmLight.Command;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// SectionsCollectionTest.xaml 的交互逻辑
    /// </summary>
    public partial class WdSectionsCollectionTest : Window
    {
        WdSectionsCollectionTestVm viewModel;
        public WdSectionsCollectionTest()
        {
            InitializeComponent();

            viewModel = new WdSectionsCollectionTestVm();
            this.DataContext = viewModel;

            viewModel.LimitSections = thickChart_xAxis.Sections;
        }

    }
    public class WdSectionsCollectionTestVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 画在图上的 厚度曲线，限制最多100个数据
        /// </summary>
        public ChartValues<TimeValue> ThicknessValues { get; } = new ChartValues<TimeValue>();

        public SectionsCollection LimitSections;// { get; } = new SectionsCollection();

        /// <summary>
        /// y轴格式
        /// </summary>
        public Func<double, string> YFormatter { get; set; }

        /// <summary>
        /// X轴格式
        /// </summary>
        public Func<double, string> DateTimeFormatter { get; set; }


        public CartesianMapper<TimeValue> MapperTv { get; set; }

        public RelayCommand UpdateCmd { get; set; }
        public WdSectionsCollectionTestVm()
        {
            #region 界面配置
            MapperTv = Mappers.Xy<TimeValue>()
                .X(value => value.Time.Ticks)
                .Y(value => value.Value);

            DateTimeFormatter = value => new DateTime((long)value).ToString("HH:mm:ss");
            YFormatter = value => value.ToString("F1");

            #endregion

            UpdateCmd = new RelayCommand(Update);

        }

        private void Update()
        {
            List<TimeValue> thks = new List<TimeValue>();
            Random r = new Random();
            DateTime dt = DateTime.Now;
            for (int i = 0; i < 100; i++) {
                thks.Add(new TimeValue() { Time = dt.AddSeconds(i), Value = r.Next(100) });
            }
            ThicknessValues.Clear();
            ThicknessValues.AddRange(thks);

            LimitSections.Clear();
            int s_cnt = r.Next(3, 5);
            double interval = thks.Count() / s_cnt;
            for (int i = 0; i < s_cnt ; i++) {
                int index = (int)(i * interval);
                if (index < 0)
                    index = 0;
                else if (index > thks.Count())
                    index = thks.Count() - 1;

                var axisSection2 = new AxisSection()
                {
                    StrokeThickness = 2,
                    DataLabel = true,
                    DisableAnimations = true,
                    DataLabelForeground = new SolidColorBrush(Colors.White),
                    Opacity = 1,
                    Stroke = new SolidColorBrush(Colors.Orange),
                    Value = thks[index].Time.Ticks
                };

                LimitSections.Add(axisSection2);
            };
        }
    }

    public class TimeValue : INotifyPropertyChanged
    {
        public DateTime Time { get; set; }

        public double Value { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
