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 Olympics.WSData;
using System.Windows.Data;
using System.Globalization;

namespace Olympics
{
    public partial class MedalsDistributionChart : UserControl
    {
        public MedalsDistributionChart()
        {
            InitializeComponent();

            c1chart.BeginUpdate();
            c1chart.ChartType = ChartType.ColumnStacked;
            c1chart.CustomPalette = new Brush[] { BrushGold, BrushSilver, BrushBronze };
            legend.Chart = c1chart;
            c1chart.EndUpdate();

            // load data
            DataBase.DataUpdated += (s, a) => { LoadData(); };
        }

        int _pageSize = 20;
        int _page = 1;

        public void LoadData() 
        {
          DataTemplate tt = c1chart.Resources["tooltip"] as DataTemplate;
            var data = new ChartData();
            var goldValues = new DataSeries() { Label = "Gold", Values = new DoubleCollection(), PointTooltipTemplate=tt};
            var silverValues = new DataSeries() { Label = "Silver", Values = new DoubleCollection(), PointTooltipTemplate = tt };
            var bronzeValues = new DataSeries() { Label = "Bronze", Values = new DoubleCollection(), PointTooltipTemplate = tt };

            var orderedEntries = DataBase.Data.OrderBy<CountryInfo, int>(DataBase.OlympicMedalComparer);
            var chartEntries = orderedEntries.Take(_page * _pageSize).Skip((_page-1) * _pageSize);
            foreach (var row in chartEntries)
            {
                int index = goldValues.Values.Count;
                goldValues.Values.Insert(index, row.GoldMedals);
                silverValues.Values.Insert(index, row.SilverMedals);
                bronzeValues.Values.Insert(index, row.BronzeMedals);

                if (row.TotalMedals == 0)
                    break;
            }

            // update page buttons
            btnNextPage.IsEnabled = DataBase.Data.Count() > _page * _pageSize;
            btnPreviousPage.IsEnabled = _page > 1;
            
            // set data
            c1chart.BeginUpdate();
            data.ItemNames = new List<CountryInfo>(chartEntries);
            data.Children.Add(goldValues); 
            data.Children.Add(silverValues);
            data.Children.Add(bronzeValues);
            c1chart.Data = data;
            if (DataBase.Data.Count() > 0)
            {
                c1chart.View.AxisY.Max = DataBase.Data.OrderBy(DataBase.TotalComparer).First().TotalMedals + 1;
            }
            c1chart.View.AxisX.Max = _pageSize;
            c1chart.EndUpdate();
        }


        private void btnPreviousPage_Click(object sender, RoutedEventArgs e)
        {
            _page -= 1;
            LoadData();
        }

        private void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            _page += 1;
            LoadData();
        }
    }
}
