using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;    // Niezbdne dla operacji asynchronicznych.

public partial class _Default : System.Web.UI.Page
{
   // Tworzenie delegatw.
   private AsyncCallback myCallBackFirmNameStockSymbol;
   private AsyncCallback myCallBackPriceStockSymbol;
   private AsyncCallback myCallBackHistory;

   StockTickerSoap proxy = new StockTickerSoap();

   int flags;

   // Domylny konstruktor klasy.
   public _Default()
   {
      // Przypisanie wywoania zwrotnego.
      myCallBackFirmNameStockSymbol = new
         AsyncCallback(this.onCompletedGetName);
      myCallBackPriceStockSymbol = new
         AsyncCallback(this.onCompletedGetPrice);
      myCallBackHistory = new
         AsyncCallback(this.onCompletedGetHistory);
   }

   protected void Page_Load(object sender, EventArgs e)
   {
   }

   protected void txtFirmNameStockSymbol_TextChanged(object sender,
      EventArgs e)
   {
      lblFirmName.Text = proxy.GetName(txtFirmNameStockSymbol.Text);
   }

   protected void txtPriceStockSymbol_TextChanged(object sender,
      EventArgs e)
   {
      lblStockPrice.Text = "$ " +
         Convert.ToString(proxy.GetPrice(txtPriceStockSymbol.Text));
   }

   protected void btnGetData_Click(object sender, EventArgs e)
   {
      flags = 0;
      // lblFirmName.Text = proxy.GetName(txtFirmNameStockSymbol.Text);
      proxy.BeginGetName(txtFirmNameStockSymbol.Text,
         myCallBackFirmNameStockSymbol,
         0);

      // lblStockPrice.Text = "$ " +
      Convert.ToString(proxy.GetPrice(txtPriceStockSymbol.Text));
      proxy.BeginGetPrice(txtPriceStockSymbol.Text,
         myCallBackPriceStockSymbol,
         0);

      // Stock theStock = proxy.GetHistory(txtHistoryStockSymbol.Text);
      proxy.BeginGetHistory(txtHistoryStockSymbol.Text,
         myCallBackHistory,
         0);

      while (flags < 3)
      {
         Thread.Sleep(100);
      }
   }

   private void onCompletedGetName(IAsyncResult asyncResult)
   {
      string s = proxy.EndGetName(asyncResult);
      lblFirmName.Text = s;
      flags++;
   }

   private void onCompletedGetPrice(IAsyncResult asyncResult)
   {
      lblStockPrice.Text = "$ " +
         Convert.ToString(proxy.EndGetPrice(asyncResult));
      flags++;
   }

   private void onCompletedGetHistory(IAsyncResult asyncResult)
   {
      Stock theStock = proxy.EndGetHistory(asyncResult);
      string StockName = theStock.StockName;
      double StockPrice = theStock.Price;

      DateTime TradeDate1 = theStock.History[0].TradeDate;
      double Price1 = theStock.History[0].Price;

      DateTime TradeDate2 = theStock.History[1].TradeDate;
      double Price2 = theStock.History[1].Price;

      // Wywietlenie wynikw.
      pnlHistory.Visible = true;
      lblHistoryStockName.Text = StockName;
      lblHistoryStockPrice.Text = "$ " + Convert.ToString(StockPrice);
      lblHistoryDate1.Text =TradeDate1.ToString("d");
      lblHistoryPrice1.Text = "$ " + Convert.ToString(Price1);
      lblHistoryDate2.Text = TradeDate2.ToString("d");
      lblHistoryPrice2.Text = "$ " + Convert.ToString(Price2);
      flags++;
   }
}
