using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using C1.Silverlight.Data;
using C1.Silverlight;
using C1.Silverlight.DataGrid;

namespace StockPortfolio
{
    public partial class SPCell : UserControl
    {
        public SPCell()
        {
            InitializeComponent();
            Clip = null;

            Loaded += new RoutedEventHandler(SPCell_Loaded);
        }


        public object MyDataContext
        {
            get { return (object)GetValue(MyDataContextProperty); }
            set { SetValue(MyDataContextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyDataContext.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyDataContextProperty =
            DependencyProperty.Register("MyDataContext", typeof(object), typeof(SPCell), new PropertyMetadata(null, OnMyDataContextChanged));


        static void OnMyDataContextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) 
        {
            var cell = (SPCell)sender;
            cell.ApplyData();
        }

        void SPCell_Loaded(object sender, RoutedEventArgs e)
        {
            ApplyData();                
        }

        public virtual void ApplyData()
        {
            DataRowView drv = (DataRowView)MyDataContext;
            if (drv == null)
                return;

            // get variation
            double open = drv.GetRow()["Open"] is double ? (double)drv.GetRow()["Open"] : (float)drv.GetRow()["Open"];
            double last = (double)drv.GetRow()["Last Price"];
            double var = last > 0 ? (last - open) / last : 0;
            if (Math.Abs(var) < 0.005)
                var = 0;

            // apply color (red/green)
            Color clr = var > 0 ? Color.FromArgb(255, 0, 128, 0) : var < 0 ? Color.FromArgb(255, 128, 0, 0) : Colors.Black;
            Brush brush = new SolidColorBrush(clr);
            lblSubTitle.Foreground = brush;
            lblSymbol.Foreground = brush;

            // show/hide symbol and subtitle
            symbolGrid.Visibility = Visibility.Visible;
            upArrow.Visibility = var > 0 ? Visibility.Visible : Visibility.Collapsed;
            downArrow.Visibility = var < 0 ? Visibility.Visible : Visibility.Collapsed;

            var symbol = (string)drv.GetRow()["Symbol"];
            lblSymbol.Text = symbol;
            string name;
            if (PortfolioSet.Symbols.TryGetValue(symbol, out name))
            {
                lblSubTitle.Text = name;
            }
        }

        //void SPCell_DoubleClick(object sender, MouseEventArgs e)
        //{
        //    btnChart_Click(sender, new RoutedEventArgs());
        //}

        //private void btnChart_Click(object sender, RoutedEventArgs e)
        //{
        //    DataRowView drv = _row.DataItem as DataRowView;
        //    string symbol = (string)drv.GetRow()["Symbol"];

        //    Main main = App.Current.RootVisual as Main;
        //    main.ShowChart(symbol);
        //}
    }
}