using C1.Xaml.Chart;
using C1.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace ControlExplorer.Controls.Chart
{
    public partial class AggregateChart
        : UserControl, IExposeApplicationBarItems
    {
        Dictionary<string, Brush> _dict = new Dictionary<string, Brush>();
        List<AppBarButton> _menuItems = new List<AppBarButton>();

        public AggregateChart()
        {
            InitializeComponent();

            foreach (var value in C1.Xaml.TypeEx.GetEnumValues<Aggregate>(typeof(Aggregate)))
            {
                var mitem = new AppBarButton()
                {
                    Label = Utils.SeparateWords(value.ToString())
                };
                mitem.Click += mitem_Click;
                _menuItems.Add(mitem);
            }

            Loaded += AggregateChart_Loaded;
        }

        void AggregateChart_Loaded(object sender, RoutedEventArgs e)
        {
            _dict["red"] = CreateBrush(Colors.Red);
            _dict["blue"] = CreateBrush(Color.FromArgb(255, 2, 150, 252));
            _dict["yellow"] = CreateBrush(Colors.Yellow);
            _dict["white"] = CreateBrush(Colors.White);


            int cnt = 10;
            chart.Data.ItemNames = CreateRandomStrings(cnt,
              new string[] { "blue", "red", "white", "yellow" }); ;

            var vals = CreateRandomValues(cnt);

            var ds = new DataSeries() { ValuesSource = vals };

            ds.PlotElementLoaded += (s, e2) =>
            {
                PlotElement pe = (PlotElement)s;
                if (_dict.ContainsKey(pe.DataPoint.Name))
                    pe.Fill = _dict[pe.DataPoint.Name];
                pe.StrokeThickness = 0;
            };

            chart.Data.Children.Add(ds);
            chart.View.AxisX.AnnoTemplate = chart.Resources["al_tmpl"];
            BarColumnOptions.SetRadiusX(chart, 4);
            BarColumnOptions.SetRadiusY(chart, 4);
        }

        void mitem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = (AppBarButton)sender;
            chart.Aggregate = (Aggregate) _menuItems.IndexOf(menuItem);
        }

        Random rnd = new Random();

        double[] CreateRandomValues(int cnt)
        {
            double[] vals = new double[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = rnd.NextDouble() * 100;
            return vals;
        }

        string[] CreateRandomStrings(int cnt, string[] list)
        {
            string[] vals = new string[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = list[rnd.Next(0, list.Length)];
            return vals;
        }

        Brush CreateBrush(Color clr)
        {
            return new LinearGradientBrush()
            {
                GradientStops = 
                { 
                  //new GradientStop() { Color= Colors.Black,  Offset = 0},
                  new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 0},
                  new GradientStop() { Color= clr, Offset = 0.2},
                  new GradientStop() { Color= clr, Offset = 0.7},
                  new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 1},
                },
                StartPoint = new Point(0, 0),
                EndPoint = new Point(1, 1),
                //Opacity = 0.8
            };
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get 
            {
                return _menuItems;
            }
        }

        #endregion
    }

    public class AxisLabelConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null)
                return string.Format("../../../Resources/flower_{0}.png", value.ToString());

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}