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;
namespace F.Phone.Services.Weather
{
    public class YahooWeather
    {
        public const string WeatherUrl = "http://weather.yahooapis.com/forecastrss?w={0}";
        public const string AppKey = "YOUR-APP-ID";
        WebClient proxy;
        WebClient Proxy
        {
            get
            {
                if (proxy == null)
                {
                    proxy = new WebClient();
                }
                return proxy;
            }
        }
        public bool IsInError { get; set; }
        public string ErrorDescription { get; set; }
        public event EventHandler<DownloadStringCompletedEventArgs> DownloadedString;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="woeid"></param>
        public void GetWeatherReport(string woeid) {
            try{
            Proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Proxy_DownloadStringCompleted);
            Proxy.DownloadStringAsync(
                new Uri(
                    string.Format (WeatherUrl , woeid )
                ));
            }
            catch (Exception ex)
            {
                IsInError = true;
                ErrorDescription = ex.Message;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (DownloadedString != null)
                {
                    DownloadedString(sender, e);
                }
            }
            catch (Exception ex)
            {
                IsInError = true;
                ErrorDescription = ex.Message;
            }
            finally {
                proxy = null;
            }
        }
    }
}
