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.IO;

namespace StockPrices
{
    public partial class frm48StockPrices : Form
    {
        public frm48StockPrices()
        {
            InitializeComponent();
        }

        // Get the stock prices.
        private void getPricesButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();

            // Build the URL.
            string url = "";
            if (symbol1TextBox.Text != "") url += symbol1TextBox.Text + "+";
            if (symbol2TextBox.Text != "") url += symbol2TextBox.Text + "+";
            if (symbol3TextBox.Text != "") url += symbol3TextBox.Text + "+";
            if (symbol4TextBox.Text != "") url += symbol4TextBox.Text + "+";
            if (url != "")
            {
                // Remove the trailing plus sign.
                url = url.Substring(0, url.Length - 1);

                // Prepend the base URL.
                const string baseUrl =
                    "http://download.finance.yahoo.com/d/quotes.csv?s=@&f=sl1d1t1c1";
                url = baseUrl.Replace("@", url);

                // Get the response.
                try
                {
                    // Get the web response.
                    string result = GetWebResponse(url);
                    Console.WriteLine(result.Replace("\\r\\n", "\r\n"));

                    // Pull out the current prices.
                    string[] lines = result.Split(
                        new char[] { '\r', '\n' },
                        StringSplitOptions.RemoveEmptyEntries);
                    price1TextBox.Text = FormatCurrency(lines[0].Split(',')[1]);
                    price2TextBox.Text = FormatCurrency(lines[1].Split(',')[1]);
                    price3TextBox.Text = FormatCurrency(lines[2].Split(',')[1]);
                    price4TextBox.Text = FormatCurrency(lines[3].Split(',')[1]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Read Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

            this.Cursor = Cursors.Default;
        }

        // Return a number formatted as currency
        // or a blank string if the text isn't a number.
        private string FormatCurrency(string text)
        {
            decimal value;
            if (decimal.TryParse(text, out value)) return value.ToString("C3");
            return "";
        }

        // Get a web response.
        private string GetWebResponse(string url)
        {
            // Make a WebClient.
            WebClient webClient = new WebClient();

            // Get the indicated URL.
            Stream response = webClient.OpenRead(url);
            
            // Read the result.
            using (StreamReader reader = new StreamReader(response))
            {
                return reader.ReadToEnd();
            }
        }


        /// <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.price4TextBox = new System.Windows.Forms.TextBox();
            this.price3TextBox = new System.Windows.Forms.TextBox();
            this.price2TextBox = new System.Windows.Forms.TextBox();
            this.price1TextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.getPricesButton = new System.Windows.Forms.Button();
            this.symbol4TextBox = new System.Windows.Forms.TextBox();
            this.symbol3TextBox = new System.Windows.Forms.TextBox();
            this.symbol2TextBox = new System.Windows.Forms.TextBox();
            this.symbol1TextBox = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // price4TextBox
            // 
            this.price4TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.price4TextBox.Location = new System.Drawing.Point(203, 104);
            this.price4TextBox.Name = "price4TextBox";
            this.price4TextBox.ReadOnly = true;
            this.price4TextBox.Size = new System.Drawing.Size(54, 20);
            this.price4TextBox.TabIndex = 21;
            this.price4TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // price3TextBox
            // 
            this.price3TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.price3TextBox.Location = new System.Drawing.Point(203, 78);
            this.price3TextBox.Name = "price3TextBox";
            this.price3TextBox.ReadOnly = true;
            this.price3TextBox.Size = new System.Drawing.Size(54, 20);
            this.price3TextBox.TabIndex = 20;
            this.price3TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // price2TextBox
            // 
            this.price2TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.price2TextBox.Location = new System.Drawing.Point(203, 52);
            this.price2TextBox.Name = "price2TextBox";
            this.price2TextBox.ReadOnly = true;
            this.price2TextBox.Size = new System.Drawing.Size(54, 20);
            this.price2TextBox.TabIndex = 19;
            this.price2TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // price1TextBox
            // 
            this.price1TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.price1TextBox.Location = new System.Drawing.Point(203, 26);
            this.price1TextBox.Name = "price1TextBox";
            this.price1TextBox.ReadOnly = true;
            this.price1TextBox.Size = new System.Drawing.Size(54, 20);
            this.price1TextBox.TabIndex = 18;
            this.price1TextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // label2
            // 
            this.label2.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.label2.Location = new System.Drawing.Point(203, 10);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(54, 13);
            this.label2.TabIndex = 17;
            this.label2.Text = "Prices";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // getPricesButton
            // 
            this.getPricesButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.getPricesButton.Location = new System.Drawing.Point(107, 57);
            this.getPricesButton.Name = "getPricesButton";
            this.getPricesButton.Size = new System.Drawing.Size(75, 23);
            this.getPricesButton.TabIndex = 16;
            this.getPricesButton.Text = "Get Prices";
            this.getPricesButton.UseVisualStyleBackColor = true;
            this.getPricesButton.Click += new System.EventHandler(this.getPricesButton_Click);
            // 
            // symbol4TextBox
            // 
            this.symbol4TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.symbol4TextBox.Location = new System.Drawing.Point(32, 104);
            this.symbol4TextBox.Name = "symbol4TextBox";
            this.symbol4TextBox.Size = new System.Drawing.Size(54, 20);
            this.symbol4TextBox.TabIndex = 15;
            this.symbol4TextBox.Text = "AAPL";
            // 
            // symbol3TextBox
            // 
            this.symbol3TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.symbol3TextBox.Location = new System.Drawing.Point(32, 78);
            this.symbol3TextBox.Name = "symbol3TextBox";
            this.symbol3TextBox.Size = new System.Drawing.Size(54, 20);
            this.symbol3TextBox.TabIndex = 14;
            this.symbol3TextBox.Text = "COKE";
            // 
            // symbol2TextBox
            // 
            this.symbol2TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.symbol2TextBox.Location = new System.Drawing.Point(32, 52);
            this.symbol2TextBox.Name = "symbol2TextBox";
            this.symbol2TextBox.Size = new System.Drawing.Size(54, 20);
            this.symbol2TextBox.TabIndex = 13;
            this.symbol2TextBox.Text = "MSFT";
            // 
            // symbol1TextBox
            // 
            this.symbol1TextBox.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.symbol1TextBox.Location = new System.Drawing.Point(32, 26);
            this.symbol1TextBox.Name = "symbol1TextBox";
            this.symbol1TextBox.Size = new System.Drawing.Size(54, 20);
            this.symbol1TextBox.TabIndex = 12;
            this.symbol1TextBox.Text = "DIS";
            // 
            // label1
            // 
            this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.label1.Location = new System.Drawing.Point(32, 10);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(54, 13);
            this.label1.TabIndex = 11;
            this.label1.Text = "Symbol";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frm48StockPrices
            // 
            this.AcceptButton = this.getPricesButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(288, 135);
            this.Controls.Add(this.price4TextBox);
            this.Controls.Add(this.price3TextBox);
            this.Controls.Add(this.price2TextBox);
            this.Controls.Add(this.price1TextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.getPricesButton);
            this.Controls.Add(this.symbol4TextBox);
            this.Controls.Add(this.symbol3TextBox);
            this.Controls.Add(this.symbol2TextBox);
            this.Controls.Add(this.symbol1TextBox);
            this.Controls.Add(this.label1);
            this.Name = "frm48StockPrices";
            this.Text = "StockPrices";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox price4TextBox;
        private System.Windows.Forms.TextBox price3TextBox;
        private System.Windows.Forms.TextBox price2TextBox;
        private System.Windows.Forms.TextBox price1TextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button getPricesButton;
        private System.Windows.Forms.TextBox symbol4TextBox;
        private System.Windows.Forms.TextBox symbol3TextBox;
        private System.Windows.Forms.TextBox symbol2TextBox;
        private System.Windows.Forms.TextBox symbol1TextBox;
        private System.Windows.Forms.Label label1;
    }
}