using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Threading;

namespace AnimatedCellsWPF
{
	public class LiveDataMock
	{
		static LiveDataMock()
		{
			random = new Random();
			DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1.5) };
			timer.Tick += new EventHandler(timer_Tick);
			timer.Start();

			var fakeCompanyNames = new[] { "Acme, inc.", "Widget Corp", "123 Warehousing", "Demo Company", "Smith and Co.", "Foo Bars", "ABC Telecom" };
			companies = (from s in fakeCompanyNames
						 select new CompanyInfo() { CompanyName = s, StockPrice = System.Convert.ToDecimal(random.NextDouble() * 500), OldStockPrice = System.Convert.ToDecimal(random.NextDouble() * 500) }).ToList();
		}

		static void timer_Tick(object sender, EventArgs e)
		{
			UpdateRandomPrice();
			MainWindow.blockAnimations = false;
		}

		private static void UpdateRandomPrice()
		{
			decimal change = System.Convert.ToDecimal(random.NextDouble() - 0.5);
			int index = random.Next(Companies.Count);
			Companies[index].StockPrice = Companies[index].StockPrice + change;
		}

		private static Random random;
		private static List<CompanyInfo> companies;
		public static List<CompanyInfo> Companies
		{
			get
			{
				return companies;
			}
		}
	}

	public class CompanyInfo : INotifyPropertyChanged
	{
		public string CompanyName { get; set; }
		private decimal stockPrice;

		public decimal StockPrice
		{
			get
			{
				return this.stockPrice;
			}
			set
			{
				this.OldStockPrice = this.stockPrice;
				this.stockPrice = value;
				this.OnPropertyChanged("StockPrice");
			}
		}

		private decimal oldStockPrice;
		public decimal OldStockPrice
		{
			get
			{
				return this.oldStockPrice;
			}
			set
			{
				this.oldStockPrice = value;
				this.OnPropertyChanged("OldStockPrice");
			}
		}

		void OnPropertyChanged(string property)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(property));
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
	}
}
