using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ISNet.Silverlight.Presenter;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Collections;
using IntersoftPresenter.NorthwindDataReference;
using Microsoft.Windows.Controls.DataVisualization.Charting;

namespace IntersoftPresenter
{
    public class ChartData
    {
        public string Key { get; set; }
        public double Count { get; set; }
    }

    public partial class ChartPresenter : UserControl, IDataPresenter
    {
        public ChartPresenter()
        {
            InitializeComponent();
        }

        #region IData Presenter

        public string PresenterCaption { get; set; }
        public string PresenterName { get; set; }
        public ImageSource PresenterIcon { get; set; }
        public double PresenterIconWidth { get; set; }
        public double PresenterIconHeight { get; set; }
        public double PresenterIconSpace { get; set; }

        // structure
        public FrameworkElement PresenterSelectorElement { get; set; }
        public ISNet.Silverlight.Presenter.DataPresenterManager OwningDataPresenterManager { get; set; }

        // method
        public void RefreshDataPresenter(IEnumerable itemsSource)
        {
            ReadOnlyCollection<EntityDescriptor> collection = itemsSource as ReadOnlyCollection<EntityDescriptor>;

            var titleCounts = from c in collection
                              group c by ((Customers)c.Entity).ContactTitle into g
                              select new ChartData { Key = g.Key, Count = g.ToList().Count() };

            ((PieSeries)ChartView1.Series[0]).ItemsSource = titleCounts.ToList();

        }

        #endregion
    }
}
