using System;
using System.Collections.Generic;
using System.Globalization;
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.Xml;
using BankTV.Helpers;
using BankTV.Model.Currency;
using BankTV.Model.Stock;

namespace BankTV.Controls
{
    /// <summary>
    /// Interaction logic for BottomBar.xaml
    /// </summary>
    public partial class BottomBar : UserControl
    {
        private DateTime lastUpdate;

        public BottomBar()
        {
            InitializeComponent();


            lastUpdate = DateTime.Now;
            var left = (double)label1A.GetValue(Canvas.LeftProperty);
            label2A.SetValue(Canvas.LeftProperty, left + label2A.ActualWidth);
        }

        public void Update()
        {
            if (this.IsInitialized)
            {
                var currentUpdate = DateTime.Now;
                var deltaT = currentUpdate - lastUpdate;
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    var left = (double)label1A.GetValue(Canvas.LeftProperty);
                    labelTime.Content = DateTime.Now.ToString("H:mm");
                    var delta = (double)(50 * deltaT.Milliseconds) / 1000.0f;
                    left -= delta;
                    label1A.SetValue(Canvas.LeftProperty, left);
                    label2A.SetValue(Canvas.LeftProperty, left + label2A.ActualWidth);
                    if (left < (label2A.ActualWidth * (-1)))
                    {
                        label1A.SetValue(Canvas.LeftProperty, left + label2A.ActualWidth);
                        label2A.SetValue(Canvas.LeftProperty, left);
                    }
                }));
                lastUpdate = currentUpdate;
            }   //if
        }

        public void UpdateCurrencyValues(List<CurrencyItem> currencyItems)
        {
            if (currencyItems == null)
            {
                return;
            }   //if
            foreach (CurrencyItem item in currencyItems)
            {
                if (item.Abbr.Equals("USD"))
                {
                    this.updateCurrencyItem(item, lblUSDSellingPrice, lblUSDBuyingPrice, imgUSDBuyingPrice, imgUSDSellingPrice);
                }   //if
                else if (item.Abbr.Equals("EUR"))
                {
                    this.updateCurrencyItem(item, lblEURSellingPrice, lblEURBuyingPrice, imgEURBuyingPrice, imgEURSellingPrice);
                }   //if
                else if (item.Abbr.Equals("GBP"))
                {
                    this.updateCurrencyItem(item, lblGBPSellingPrice, lblGBPBuyingPrice, imgGBPBuyingPrice, imgGBPSellingPrice);
                }   //if
            }   //for
        }

        private void updateCurrencyItem(CurrencyItem item, Label sellingPrice, Label buiyingPrice, Image imgBuyingPrice, Image imgSellingPrice)
        {
            if (item == null)
            {
                return;
            }
            if (sellingPrice == null)
            {
                return;
            }
            if (buiyingPrice == null)
            {
                return;
            }
            if (imgBuyingPrice == null)
            {
                return;
            }
            if (imgSellingPrice == null)
            {
                return;
            }
            sellingPrice.Content = item.SellingPrice.ToString("0.00", CultureInfo.InvariantCulture);
            buiyingPrice.Content = item.BuyingPrice.ToString("0.00", CultureInfo.InvariantCulture);
            switch (item.BuyingPriceDirection)
            {
                case CurrencyItem.CurrencyPriceDirection.DOWN:
                    imgBuyingPrice.Source = new BitmapImage(new Uri("pack://application:,,,/Images/arrow_down.png"));
                    buiyingPrice.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFD0000");
                    imgBuyingPrice.Visibility = Visibility.Visible;
                    break;
                case CurrencyItem.CurrencyPriceDirection.UP:
                    imgBuyingPrice.Source = new BitmapImage(new Uri("pack://application:,,,/Images/arrow_up.png"));
                    buiyingPrice.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#358415");
                    imgBuyingPrice.Visibility = Visibility.Visible;
                    break;
                case CurrencyItem.CurrencyPriceDirection.NOCHANGE:
                    imgBuyingPrice.Visibility = Visibility.Hidden;
                    buiyingPrice.Foreground = new SolidColorBrush(Colors.Black);
                    break;
            }   //switch
            switch (item.SellingPriceDirection)
            {
                case CurrencyItem.CurrencyPriceDirection.DOWN:
                    imgSellingPrice.Source = new BitmapImage(new Uri("pack://application:,,,/Images/arrow_down.png"));
                    sellingPrice.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFD0000");
                    imgSellingPrice.Visibility = Visibility.Visible;
                    break;
                case CurrencyItem.CurrencyPriceDirection.UP:
                    imgSellingPrice.Source = new BitmapImage(new Uri("pack://application:,,,/Images/arrow_up.png"));
                    sellingPrice.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#358415");
                    imgSellingPrice.Visibility = Visibility.Visible;
                    break;
                case CurrencyItem.CurrencyPriceDirection.NOCHANGE:
                    imgSellingPrice.Visibility = Visibility.Hidden;
                    sellingPrice.Foreground = new SolidColorBrush(Colors.Black);
                    break;
            }   //switch
        }

        public void UpdateNewsBar(List<string> bottomBarNews)
        {
            StringBuilder bottomBarNewsBuilder = new StringBuilder();
            if (bottomBarNews != null)
            {
                foreach (string newsItem in bottomBarNews)
                {
                    bottomBarNewsBuilder.Append(String.Format("► {0}   ", newsItem));
                }   //for
            }   //if
            bottomBarNewsBuilder.Append("► ");
            label1A.Content = bottomBarNewsBuilder.ToString();
            label2A.Content = bottomBarNewsBuilder.ToString();
        }


    }
}
