using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace FlexGridSamples
{
    /// <summary>
    /// Interaction logic for StockTicker.xaml
    /// </summary>
    public partial class StockTicker : Page
    {
        public static readonly DependencyProperty ValueProperty = 
            DependencyProperty.Register(
            "Value", 
            typeof(double), 
            typeof(StockTicker), 
            new PropertyMetadata(0.0, ValueChanged));

        string _format = "n2";
        Storyboard _flash;
        string _bindingSource;
        bool _firstTime = true;

        static Brush _brNegative = new SolidColorBrush(Colors.Red);
        static Brush _brPositive = new SolidColorBrush(Colors.Green);
        static Color _clrNegative = Color.FromArgb(100, 0xff, 0, 0);
        static Color _clrPositive = Color.FromArgb(100, 0, 0xff, 0);

        public StockTicker()
        {
            InitializeComponent();
        }

        private Storyboard Flash
        {
            get
            {
                if (_flash == null)
                {
                    _flash = (Storyboard)Resources["_sbFlash"];
                }
                return _flash;
            }
        }
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ticker = d as StockTicker;
            var value = (double)e.NewValue;
            var oldValue = (double)e.OldValue;

            // resetting
            if (double.IsNaN(value))
            {
                if (ticker._flash != null)
                {
                    ticker._flash.Stop();
                }
                ticker._root.Background = new SolidColorBrush(Colors.Transparent);
                ticker._arrow.Fill = null;
                ticker._txtChange.Foreground = ticker._txtValue.Foreground;
                return;
            }

            // get historical data
            var data = ticker.Tag as FinancialData;
            var list = ticker.BindingSource != null ? data.GetHistory(ticker.BindingSource) : new System.Collections.Generic.List<decimal>();
            if (list != null && list.Count > 1)
            {
                oldValue = (double)list[list.Count - 2];
            }

            // calculate percentage change
            var change = oldValue == 0 || double.IsNaN(oldValue) ? 0 : (value - oldValue) / oldValue;

            // update text
            ticker._txtValue.Text = value.ToString(ticker._format);
            ticker._txtChange.Text = string.Format(Strings.Change, change * 100);

            // update flash color
            ColorAnimation ca;

            // update symbol
            if (change == 0)
            {
                ticker._arrow.Fill = null;
                ticker._txtChange.Foreground = ticker._txtValue.Foreground;
            }
            else if (change < 0)
            {
                ticker._stArrow.ScaleY = -1;
                ticker._txtChange.Foreground = ticker._arrow.Fill = _brNegative;
                ca = ticker.Flash.Children[0] as ColorAnimation;
                ca.From = _clrNegative;
            }
            else
            {
                ticker._stArrow.ScaleY = +1;
                ticker._txtChange.Foreground = ticker._arrow.Fill = _brPositive;
                ca = ticker.Flash.Children[0] as ColorAnimation;
                ca.From = _clrPositive;
            }

            // update sparkline
            if (list != null)
            {
                ticker._sparkLine.Data = null;
                ticker._sparkLine.Data = list;
            }
            
            // flash new value (but not right after the control was created)
            if (!ticker._firstTime && change != 0)
            {
                ticker._flash.Begin();
            }
            ticker._firstTime = false;
        }
        public string BindingSource
        {
            get { return _bindingSource; }
            set { _bindingSource = value; }
        }
        public string Format
        {
            get { return _format; }
            set
            {
                _format = value;
                _txtValue.Text = Value.ToString(_format);
            }
        }
    }
}
