using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using C1.WPF;
using C1.WPF.Theming;
using C1.WPF.C1Chart;
using System.Windows;
using System;


namespace Themes
{
    public partial class Gallery : UserControl
    {
        public Gallery()
        {
            InitializeComponent();

            // generate sample data
            var persons = Person.Generate(80);

            // bind controls
            propGrid1.SelectedObject = persons[0];
            //propGrid2.SelectedObject = persons[1];
            grid.ItemsSource = persons;
            flexGrid.ItemsSource = persons;
            listBox.ItemsSource = persons;
            olapPage.DataSource = persons;
            flexSheet.AddSheet("Sheet1", 50, 10);
            #region Populate Sheet1
            // populate the grid with some formulas (multiplication table)
            for (int r = 0; r < flexSheet.Rows.Count - 2; r++)
            {
                List<double> datas = new List<double>();
                for (int c = 0; c < flexSheet.Columns.Count; c++)
                {
                    flexSheet[r, c] = string.Format("={0}*{1}", r + 1, c + 1);
                    double value = (double)flexSheet[r, c];
                    datas.Add(value);
                }
            }

            // add a totals row to illustrate
            var totRow = flexSheet.Rows.Count - 1;
            for (int c = 0; c < flexSheet.Columns.Count; c++)
            {
                flexSheet[totRow, c] = string.Format("=sum({0}1:{0}{1})",
                    (char)('A' + c), flexSheet.Rows.Count - 3);
            }
            flexSheet.Rows[totRow].FontWeight = FontWeights.Bold;
            #endregion

            olapPage.Loaded += olapPage_Loaded;

            ViewType.ItemsSource = Enum.GetValues(typeof(C1.WPF.Schedule.ViewType));
            
            OnThemeChanged();
            //coverFlow.ItemsSource = persons;
            //coverFlow.SelectedIndex = 5;

            //ReportViewer.LoadDocument(Application.GetResourceStream(new Uri("/Themes;component/Resources/Sales.pdf", UriKind.Relative)).Stream);
        }

        void olapPage_Loaded(object sender, RoutedEventArgs e)
        {
            olapPage.OlapEngine.RowFields.Add("Name");
            olapPage.OlapEngine.ColumnFields.Add("Residence");
            olapPage.OlapEngine.ValueFields.Add("Age");
        }


        public Theme CurrentTheme
        {
            get { return (Theme)GetValue(CurrentThemeProperty); }
            set { SetValue(CurrentThemeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentTheme.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentThemeProperty =
            DependencyProperty.Register("CurrentTheme", typeof(Theme), typeof(Gallery), new PropertyMetadata(OnThemeChanged));

        private static void OnThemeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Gallery)d).OnThemeChanged();
        }

        private void OnThemeChanged()
        {
            if (CurrentTheme != null)
            {
                var theme = CurrentTheme.GetNewResourceDictionary();
                cal1.Theme = theme;
                sched1.Theme = theme;
            }
        }


        #region C1Chart specifics

        private void C1Chart_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            C1Chart chart = (C1Chart)sender;
            chart.Data.Children.Add(new DataSeries() { Label = "Series1", ValuesSource = new double[] { 3, 2, 7, 4, 8 } });

            if (chart.ChartType != ChartType.Pie)
            {
                chart.Data.Children.Add(new DataSeries() { Label = "Series2", ValuesSource = new double[] { 1, 2, 3, 4, 6 } });
                chart.Data.Children.Add(new DataSeries() { Label = "Series3", ValuesSource = new double[] { 0, 1, 6, 2, 3 } });
            }

            chart.Loaded -= new RoutedEventHandler(C1Chart_Loaded);
        }

        #endregion

        #region C1Scheduler specifics
        private void Grouping_Checked(object sender, RoutedEventArgs e)
        {
            sched1.GroupBy = "Category";
        }

        private void Grouping_Unchecked(object sender, RoutedEventArgs e)
        {
            sched1.GroupBy = "";
        }        
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bool b = basicsTabControl.IsLoaded;
        }

        private void flexSheet_Loaded(object sender, RoutedEventArgs e)
        {
            formulaBar.FlexSheet = flexSheet;
        }


    }
}
