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 C1.Silverlight.Chart;
using Factories.FactoriesService;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace Factories
{
    public partial class StoreDecorator : UserControl
    {
        public StoreDecorator()
        {
            InitializeComponent();
            
            SetBinding(SalesProperty, new Binding("Sales"));

            legend.Chart = chart;

            chartGrid.MouseLeftButtonDown += (s, e) =>
            {
                showLargeChart.Begin();
                legend.Visibility = Visibility.Visible;
                chartGrid.Cursor = Cursors.Arrow;
            };
            chartGrid.MouseLeave += (s, e) =>
            {
                showLargeChart.Stop();
                legend.Visibility = Visibility.Collapsed;
                chartGrid.Cursor = Cursors.Hand;
            };
        }

        static readonly DependencyProperty SalesProperty =
            DependencyProperty.Register(
                "Sales",
                typeof(ObservableCollection<ProductSale>),
                typeof(StoreDecorator),
                new PropertyMetadata(OnSalesPropertyChanged));

        static void OnSalesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as StoreDecorator).UpdateData();
        }

        void UpdateData()
        {
            var sales = (ObservableCollection<ProductSale>)GetValue(SalesProperty);
            if (sales == null)
                return;

            chart.Data.Children.Clear();
            
            foreach (var psale in sales)
            {
                chart.Data.Children.Add(new DataSeries { ValuesSource = new[] { psale.Sales }, Label = psale.Product.Name });
            }
            chart.Data.ItemNames = new[] { "" };
        }
    }
}
