using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using C1.Xaml.Chart;
using Windows.UI.Xaml.Media.Animation;

namespace ChartSamples
{
    public sealed partial class Gallery : UserControl
    {
        SampleData sampleData;
        PlotElementAnimation animation;

        public Gallery()
        {
            this.InitializeComponent();

            var sb = new Storyboard();
            var duration = new Duration(TimeSpan.FromSeconds(1));
            var da = new DoubleAnimation() { From = 0, To = 1, Duration = duration };
            Storyboard.SetTargetProperty(da, "Opacity");
            sb.Children.Add(da);
            PlotElementAnimation.SetIndexDelay(da, 0.5);
            animation = new PlotElementAnimation() { Storyboard = sb, SymbolStyle = (Style)Resources["style"] };

            cbChartType.ItemsSource = Utils.GetEnumValues(typeof(ChartType));
            cbPalette.ItemsSource = Utils.GetEnumValues(typeof(Palette));

            sampleData = new SampleData(null/*DataSeries_Loaded*/);

            cbChartType.SelectedItem = chart.ChartType.ToString();
            cbPalette.SelectedItem = chart.Palette.ToString();


            //tg = new TransformGroup();
            //tg.Children.Add(new ScaleTransform() { ScaleX = 0, ScaleY = 0.3 });
            chart.Data = sampleData.DefaultData;
            chart.Data.LoadAnimation = animation;

            foreach (DataSeries ds in chart.Data.Children)
            {
                ds.PointTooltipTemplate = this.Resources["label"] as DataTemplate;
            }

        }

        private void cbPalette_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbPalette.SelectedItem is string)
                chart.Palette = (Palette)Enum.Parse(typeof(Palette),
                  (string)cbPalette.SelectedItem, false);
        }

        private void cbChartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbChartType.SelectedItem is string)
            {
                ChartType chartType = (ChartType)Enum.Parse(typeof(ChartType),
                  (string)cbChartType.SelectedItem, false);

                // several chart requires special data
                if (chartType == ChartType.HighLowOpenClose || chartType == ChartType.Candle)
                    chart.Data = sampleData.FinancialData;
                else if (chartType == ChartType.Bubble)
                    chart.Data = sampleData.BubbleData;
                else if (chartType == ChartType.Gantt)
                    chart.Data = sampleData.GanttData;
                else
                    chart.Data = sampleData.DefaultData;

                //StartAnimation();
                chart.Data.LoadAnimation = animation;
                foreach(DataSeries ds in chart.Data.Children)
                {
                    ds.PointTooltipTemplate = this.Resources["label"] as DataTemplate;
                }
                chart.ChartType = chartType;

            }
        }
    }

    class Utils
    {
        public static List<object> GetEnumValues(Type type)
        {
            List<object> list = new List<object>();
            for (int i = 0; ; i++)
            {
                string val = Enum.GetName(type, i);
                if (!string.IsNullOrEmpty(val))
                    list.Add(val);
                else
                    break;
            }

            return list;
        }
    }
}
