using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.Chart;
using StockPortfolio.localhost;
using System.Windows.Controls.Primitives;

namespace StockPortfolio
{
    public partial class ChartArea : UserControl
    {
        ChartLogic _logic;
        SymbolButton _selectedSym;     // the button that represents the symbol selected in the grid

        public ChartArea()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ChartArea_Loaded);
        }

        void ChartArea_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeChart();
        }

        public void InitializeChart()
        {
            // -- symbol search box
            txtSearch.Items = PortfolioSet.Symbols;

            // show the date range being viewed
            lblDateRange.Text = string.Empty;

            _logic = new ChartLogic(this, chartMain, chartSub, /*sb, */slider, symHolder, lblDateRange);

        }

        public void AddSymbol(CurrentStockInfo info)
        {
            // create the default symbols
            CreateSymHolders();

            companyInfo.txtCoName.Text = info._name;
            companyInfo.txtCoHigh.Text = info._high.ToString("f");
            companyInfo.txtCoLow.Text = info._low.ToString("f");
            companyInfo.txtCoOpen.Text = info._open.ToString("f");

            float vol = info._volume / 1000000;
            companyInfo.txtCoVolumn.Text = vol.ToString("f") + "M";
            companyInfo.txtCoSymbol.Text = "Public, NASDAQ: " + info._symbol;
            companyInfo.txtLastTrade.Text = DateTime.Now.ToString("t");
            companyInfo.txtLastDate.Text = ((info._high + info._low) / 2).ToString("f");

            _selectedSym = new SymbolButton();
            _selectedSym.Text = info._symbol;
            _selectedSym.Tag = info._symbol;
            _selectedSym.CheckedChanged += new EventHandler(btn_CheckedChanged);
            _selectedSym.IsChecked = true;
            _selectedSym.IsEnabled = false;

            symHolder.Children.Insert(0, _selectedSym);

            // update the upper chart with the new stock
            _logic.Add(info._symbol);
            //ZoomButton btn = _zoomButtons.Children[3] as ZoomButton;
            //btn.Checked = true;
        }

        void _chartMain_LegendChanged(object sender, EventArgs e)
        {
            if (_logic != null)
                UpdateSymbols();
        }

        private void CreateSymHolders()
        {
            SymbolButton btn = new SymbolButton();
            btn.Text = "Nasd"; btn.Tag = "Nasd";
            btn.Color = Colors.DarkGray;
            btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
            btn.IsChecked = false;
            symHolder.Children.Add(btn);

            btn = new SymbolButton();
            btn.Text = "Dow"; btn.Tag = "Dow";
            btn.Color = Colors.DarkGray;
            btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
            btn.IsChecked = false;
            symHolder.Children.Add(btn);

            btn = new SymbolButton();
            btn.Text = "S&P500"; btn.Tag = "S&P500";
            btn.Color = Colors.DarkGray;
            btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
            btn.IsChecked = false;
            symHolder.Children.Add(btn);
        }

        public void RemoveSymHolders()
        {
            symHolder.Children.Clear();
            _logic.RemoveAll();
        }
        
        // handles the predefined ranges (e.g., 2Y, 1Y, 6M, etc.)
        private void ButtonZoom_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton btn = (RepeatButton)sender;

            // update slider with built in ranges
            _logic.SetTimeRange((btn.Content as TextBlock).Text);
        }

        void btn_CheckedChanged(object sender, EventArgs e)
        {
            SymbolButton btn = (SymbolButton)sender;

            if ((bool)btn.IsChecked)
            {
                // add the stuff to the chart
                if (btn.Tag is string)
                    _logic.Add((string)btn.Tag);
            }
            else
            {
                // remove the stuff from the chart, leave the button
                if (btn.Tag is string)
                {
                    _logic.Remove((string)btn.Tag);
                    btn.Text = (string)btn.Tag;
                }
            }
            UpdateSymbols();
        }

        void UpdateSymbols()
        {
            int cnt = symHolder.Children.Count;
            for (int i = 0; i < cnt; i++)
            {
                if (symHolder.Children[i] is SymbolButton)
                {
                    SymbolButton btn = symHolder.Children[i] as SymbolButton;
                    btn.Color = _logic.GetSymbolColor(btn.Tag as string);
                }
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // nothing entered
            if (txtSearch.Symbol.Length == 0) return;

            AddSym(txtSearch.Symbol);
        }


        /// <summary>
        /// Adds a symbol to the chart
        /// </summary>
        /// <param name="sym"></param>
        void AddSym(string sym)
        {
            var btn = new SymbolButton();
            btn.Text = sym;
            btn.Tag = sym;
            btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
            btn.IsChecked = true;

            symHolder.Children.Insert(0, btn);

            // update the upper chart with the new stock
            _logic.Add(sym);
            _logic.UpdateChart(false);
        }
    }
}
