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;
using C1.Silverlight.Data;
using C1.Silverlight.DataGrid;
using System.ServiceModel.Channels;

namespace StockPortfolio
{
    public partial class GridArea : UserControl
    {
        public GridArea()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(GridArea_Loaded);
        }

        void GridArea_Loaded(object sender, RoutedEventArgs e)
        {
            // show active portfolio
            //InitializeGrid();
            
            chkTransactions.IsChecked = true;

            UpdatePortfolioMenu();
        }

        private void chkTransactions_Checked(object sender, RoutedEventArgs e)
        {
            // show/hide columns that only appear in the transaction view
            //_grdMain.BeginUpdate();
            string[] transactionColumns = "Remove,Date,Price,Commission,Notes".Split(',');
            foreach (DataGridColumn column in gridMain.Columns)
            {
                if (transactionColumns.Contains<string>((string)column.Header))
                {
                    column.Visibility = (bool)chkTransactions.IsChecked ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            //_grdMain.EndUpdate();

            // show transactions or summary
            gridMain.ItemsSource = (bool)chkTransactions.IsChecked
                ? PortfolioSet.Portfolio.Transactions.DefaultView
                : PortfolioSet.Portfolio.Summary.DefaultView;

            // can't delete summary rows
            gridMain.CanUserRemoveRows = chkTransactions.IsChecked.Value;
        }

        // update portfolio menu
        private void UpdatePortfolioMenu()
        {
            // add one item per portfolio, set menu text
            ItemCollection items = listBox.Items;
            items.Clear();
            foreach (DataRow dr in PortfolioSet.Portfolio.Portfolios.Rows)
            {
                int id = (int)dr["ID"];
                TextBlock item = new TextBlock();
                item.Text = (string)dr["Name"];
                item.Tag = dr;
                items.Add(item);
                item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
                if (id == PortfolioSet.Portfolio.ActivePortfolio)
                {
                    lblPortfolio.Text = item.Text;
                }
            }

            // add item to edit portfolio list
            items.Add(new Path() { Data = new LineGeometry() { StartPoint = new Point(0,0), EndPoint= new Point(1,0) }, Fill = new SolidColorBrush(Colors.Black), Stretch = Stretch.Fill, Stroke = new SolidColorBrush(Colors.Black), Height = 2 });
            TextBlock lbl = new TextBlock();
            lbl.Text = "Create/Edit Portfolios...";
            lbl.Opacity = 0.5;
            lbl.Tag = null;
            items.Add(lbl);
        }

        void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            portfolioMenu.IsDropDownOpen = false;
            TextBlock item = sender as TextBlock;
            DataRow dr = item.Tag as DataRow;
            if (dr != null)
            {
                lblPortfolio.Text = (string) dr["Name"];
                PortfolioSet.Portfolio.ActivePortfolio = (int) dr["ID"];
            }
            else
            {
                // TODO: Show portfolio editor
            }
        }

        private void btnChart_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            DataGridCell cell = gridMain.GetCellFromFrameworkElement(btn);

            DataRow drv = ((DataRowView)cell.Row.DataItem).GetRow();
            string symbol = (string)drv["Symbol"];

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

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            DataGridCell cell = gridMain.GetCellFromFrameworkElement(btn);

            // delete this row
            DataRow drv = ((DataRowView)cell.Row.DataItem).GetRow();
            drv.Delete();
            //gridMain.Reload(false);
        }

        private void gridMain_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            //var element = gridMain.GetCell(e.Row.Index, 0).Presenter;
        }
    }
}
