using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace AnimatedCellsWPF
{
	/// <summary>
	/// Interaction logic for StockPricePresenter.xaml
	/// </summary>
	public partial class StockPricePresenter : UserControl
	{
		public StockPricePresenter()
		{
			InitializeComponent();
			this.Loaded += new RoutedEventHandler(StockPricePresenter_Loaded);
		}

		void StockPricePresenter_Loaded(object sender, RoutedEventArgs e)
		{
			UpdateUI();
		}

		private void UpdateUI()
		{
			this.textBlock.Text = string.Format("{0:c}", this.StockPrice);

			if (this.StockPrice < this.OldStockPrice)
			{
				((Storyboard)this.Resources["RedToTransparent"]).Begin();
				this.Arrow.Fill = new SolidColorBrush(Colors.Red);
				this.ArrowTransform.Angle = 90;
			}
			else
			{
				((Storyboard)this.Resources["GreenToTransparent"]).Begin();
				this.Arrow.Fill = new SolidColorBrush(Colors.Green);
				this.ArrowTransform.Angle = -90;
			}
		}

		public decimal StockPrice
		{
			get { return (decimal)GetValue(StockPriceProperty); }
			set { SetValue(StockPriceProperty, value); }
		}

		// Using a DependencyProperty as the backing store for StockPrice.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty StockPriceProperty =
			DependencyProperty.Register("StockPrice", typeof(decimal), typeof(StockPricePresenter), new PropertyMetadata(StockPriceChanged));

		static void StockPriceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			((StockPricePresenter)sender).UpdateUI();
		}

		public decimal OldStockPrice
		{
			get { return (decimal)GetValue(OldStockPriceProperty); }
			set { SetValue(OldStockPriceProperty, value); }
		}

		// Using a DependencyProperty as the backing store for OldStockPrice.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty OldStockPriceProperty =
			DependencyProperty.Register("OldStockPrice", typeof(decimal), typeof(StockPricePresenter), new PropertyMetadata(null));
	}
}
