using C1.Chart;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace FlexChartExplorer
{
    /// <summary>
    /// Interaction logic for AxisGrouping.xaml
    /// </summary>
    public partial class DateTimeAxisGrouping : UserControl
    {
        Random rnd = new Random();
        List<string> _groupSeparator;

        public DateTimeAxisGrouping()
        {
            InitializeComponent();
            flexChart.AxisX.GroupProvider = new MyDateTimeGroupProvider();
        }

        public List<string> GroupSeparator
        {
            get
            {
                if (_groupSeparator == null)
                {
                    _groupSeparator = Enum.GetNames(typeof(AxisGroupSeparator)).ToList();
                }

                return _groupSeparator;
            }
        }

        public List<Quote> Data
        {
            get { return CreateData(); }
        }

        List<Quote> CreateData()
        {
            List<Quote> list = new List<Quote>();
            var dt = DateTime.Today;
            var price = 1000;
            for (var i = 0; i < 1000; i++)
            {
                price += rnd.Next(10) % 2 == 0 ? rnd.Next(50) : -rnd.Next(50);
                list.Add(new Quote() { Time = dt.AddDays(i), Price = price });
            }

            return list;
        }

        public class Quote
        {
            public DateTime Time { get; set; }
            public double Price { get; set; }
        }

        public class MyDateTimeGroupProvider : IAxisGroupProvider
        {
            public int GetLevels(IRange range)
            {
                return 2;
            }

            public IList<IRange> GetRanges(IRange range, int level)
            {
                var timeRange = range as TimeRange;
                if (timeRange == null)
                    return null;
                var min = timeRange.TimeMin;
                var max = timeRange.TimeMax;
                var span = max - min;

                List<IRange> ranges = new List<IRange>();
                DateTime start;
                if (level == 1)
                {
                    start = new DateTime(min.Year, ((int)Math.Ceiling((double)min.Month / 3) - 1) * 3 + 1, 1);
                    ranges = Enumerable.Range(0, ((max.Month - start.Month) / 3 + 1) + 4 * (max.Year - start.Year)).Select(a => start.AddMonths(a * 3))
                   .TakeWhile(a => a <= max)
                   .Select(a => (IRange)(new TimeRange("Q" + (int)Math.Ceiling((double)a.Month / 3), a, a.AddMonths(3)))).ToList();
                }
                else
                {
                    start = new DateTime(min.Year, 1, 1);
                    ranges = Enumerable.Range(0, max.Year - start.Year + 1).Select(a => start.AddYears(a))
                   .TakeWhile(a => a <= max)
                   .Select(a => (IRange)(new TimeRange(a.ToString("yyyy"), a, a.AddYears(1)))).ToList();
                }

                return ranges;
            }
        }

    }
}
