using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

using Olympics.WSData;
using System.ServiceModel;
using C1.Silverlight;


namespace Olympics
{
    public static class DataBase
    {
        public static ObservableCollection<CountryInfo> Data { get; set; }

        public static Func<CountryInfo, int> OlympicMedalComparer = new Func<CountryInfo, int>((row) => { return -row.GoldMedals; });
        public static Func<CountryInfo, int> TotalComparer = new Func<CountryInfo, int>((row) => { return -row.TotalMedals; });

        public static void Load() 
        {
            OlympicsDataSoapClient data = new OlympicsDataSoapClient(new BasicHttpBinding(), new EndpointAddress(Extensions.GetAbsoluteUri("WSData.asmx")));
            data.GetMedalsCompleted += new EventHandler<GetMedalsCompletedEventArgs>(data_GetMedalsCompleted);
            data.GetMedalsAsync();
        }

        static void data_GetMedalsCompleted(object sender, GetMedalsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Data = e.Result;

                if (DataUpdated != null)
                    DataUpdated(null, EventArgs.Empty);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
        }

        public static event EventHandler DataUpdated;
    }
}
