using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Xml;
using System.IO;

namespace WeatherForecast
{
    public partial class frm51WeatherForecast : Form
    {
        public frm51WeatherForecast()
        {
            InitializeComponent();
        }

        // Enter your API key here.
        // Get an API key by making a free account at:
        //      http://home.openweathermap.org/users/sign_in
        private const string APIKEY = "9749874xw2kfiq9029j092m0j9kfj07e";

        // Query URLs. Replace @LOC@ with the location.
        private const string CurrentUrl =
            "http://api.openweathermap.org/data/2.5/weather?" +
            "q=@LOC@&mode=xml&units=imperial&APPID=" + APIKEY;
        private const string ForecastUrl =
            "http://api.openweathermap.org/data/2.5/forecast?" +
            "q=@LOC@&mode=xml&units=imperial&APPID=" + APIKEY;

        // Get current conditions.
        private void conditionsButton_Click(object sender, EventArgs e)
        {
            // Compose the query URL.
            string url = CurrentUrl.Replace("@LOC@", locationTextBox.Text);
            xmlTextBox.Text = GetFormattedXml(url);
        }

        // Get a forecast.
        private void forecastButton_Click(object sender, EventArgs e)
        {
            // Compose the query URL.
            string url = ForecastUrl.Replace("@LOC@", locationTextBox.Text);
            xmlTextBox.Text = GetFormattedXml(url);
        }

        // Return the XML result of the URL.
        private string GetFormattedXml(string url)
        {
            // Create a web client.
            using (WebClient client = new WebClient())
            {
                // Get the response string from the URL.
                string xml = client.DownloadString(url);

                // Load the response into an XML document.
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                // Format the XML.
                using (StringWriter stringWriter = new StringWriter())
                {
                    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
                    xmlWriter.Formatting = Formatting.Indented;
                    doc.WriteTo(xmlWriter);

                    // Return the result.
                    return stringWriter.ToString();
                }
            }
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.conditionsButton = new System.Windows.Forms.Button();
            this.xmlTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.locationTextBox = new System.Windows.Forms.TextBox();
            this.forecastButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // conditionsButton
            // 
            this.conditionsButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.conditionsButton.Location = new System.Drawing.Point(114, 38);
            this.conditionsButton.Name = "conditionsButton";
            this.conditionsButton.Size = new System.Drawing.Size(75, 23);
            this.conditionsButton.TabIndex = 5;
            this.conditionsButton.Text = "Conditions";
            this.conditionsButton.UseVisualStyleBackColor = true;
            this.conditionsButton.Click += new System.EventHandler(this.conditionsButton_Click);
            // 
            // xmlTextBox
            // 
            this.xmlTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.xmlTextBox.Location = new System.Drawing.Point(12, 76);
            this.xmlTextBox.Multiline = true;
            this.xmlTextBox.Name = "xmlTextBox";
            this.xmlTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.xmlTextBox.Size = new System.Drawing.Size(360, 173);
            this.xmlTextBox.TabIndex = 8;
            this.xmlTextBox.WordWrap = false;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 15);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(51, 13);
            this.label2.TabIndex = 9;
            this.label2.Text = "Location:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 60);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 13);
            this.label1.TabIndex = 6;
            this.label1.Text = "XML:";
            // 
            // locationTextBox
            // 
            this.locationTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.locationTextBox.Location = new System.Drawing.Point(69, 12);
            this.locationTextBox.Name = "locationTextBox";
            this.locationTextBox.Size = new System.Drawing.Size(303, 20);
            this.locationTextBox.TabIndex = 4;
            this.locationTextBox.Text = "London";
            // 
            // forecastButton
            // 
            this.forecastButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.forecastButton.Location = new System.Drawing.Point(195, 38);
            this.forecastButton.Name = "forecastButton";
            this.forecastButton.Size = new System.Drawing.Size(75, 23);
            this.forecastButton.TabIndex = 7;
            this.forecastButton.Text = "Forecast";
            this.forecastButton.UseVisualStyleBackColor = true;
            this.forecastButton.Click += new System.EventHandler(this.forecastButton_Click);
            // 
            // frm51WeatherForecast
            // 
            this.AcceptButton = this.conditionsButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 261);
            this.Controls.Add(this.conditionsButton);
            this.Controls.Add(this.xmlTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.locationTextBox);
            this.Controls.Add(this.forecastButton);
            this.Name = "frm51WeatherForecast";
            this.Text = "WeatherForecast";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button conditionsButton;
        private System.Windows.Forms.TextBox xmlTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox locationTextBox;
        private System.Windows.Forms.Button forecastButton;
    }
}