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.Data;
using C1.Silverlight;

namespace StockPortfolio
{
    public partial class Transactions : UserControl
    {
        public Transactions()
        {
            InitializeComponent();
            searchSymbol.Items = PortfolioSet.Symbols;
        }
        // buy/sell/watch stocks
        private void btnBuySellWatch_Click(object sender, RoutedEventArgs e)
        {
            // make sure transactions are visible
            Main main = Application.Current.RootVisual as Main;
            main.gridArea.chkTransactions.IsChecked = true;

            // get price/quantity information
            int shares = 0;
            double price = 0;
            double commission = 0;
            if (object.Equals(sender, btnBuy) || object.Equals(sender, btnSell))
            {
                // get values to use when buying/selling
                shares = (int)txtShares.Value;
                price = txtPrice.Value;
                commission = txtCommision.Value;

                // selling is like buying a negative qty of shares
                if (object.Equals(sender, btnSell))
                {
                    shares = -shares;
                }
            }

            // create new row
            DataRow dr = PortfolioSet.Portfolio.Transactions.NewRow();

            // set the data
            dr["Portfolio"] = PortfolioSet.Portfolio.ActivePortfolio;
            dr["Symbol"] = searchSymbol.Symbol;
            dr["Last Price"] = 0;
            dr["Open"] = 0;
            dr["High"] = 0;
            dr["Low"] = 0;
            dr["Date"] = (dpDate.SelectedDate != null ? dpDate.SelectedDate : DateTime.Now);
            dr["Shares"] = shares;
            dr["Price"] = price;
            dr["Commission"] = commission;
            dr["Notes"] = txtNotes.Text;

            // add to table
            PortfolioSet.Portfolio.Transactions.Rows.Add(dr);
        }

        // enable/disable buttons depending on input control state
        private void UpdateButtonState()
        {
            string companyName;
            if (searchSymbol.Symbol.Length > 0)
            {
                lblCompanyName.Text = searchSymbol.Text;
                bool canBuy = txtShares.Value > 0;
                btnBuy.IsEnabled = canBuy;
                btnSell.IsEnabled = canBuy;
                btnWatch.IsEnabled = !canBuy;
            }
            else
            {
                lblCompanyName.Text = string.Empty;
                btnBuy.IsEnabled = false;
                btnSell.IsEnabled = false;
                btnWatch.IsEnabled = false;
            }
        }

        private void txtTransaction_ValueChanged(object sender, EventArgs e)
        {
            UpdateButtonState();
        }
    }
}
