using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

using Ecng.Collections;
using Ecng.Xaml;
using Ecng.Common;

using StockSharp.Algo;
using StockSharp.Algo.Candles;
using StockSharp.BusinessEntities;
using StockSharp.Algo.Derivatives;
using StockSharp.Algo.Reporting;
using StockSharp.Xaml;
using StockSharp.Algo.Logging;
using StockSharp.Algo.Strategies;
using StockSharp.Algo.Testing;

using StockSharp.AlfaDirect;
using StockSharp.Quik;

namespace OptionPremiumExample
{
  public partial class MainWindow
    {
        #region Поля

        private ITrader _trader;

        private readonly ObservableCollection<Security> _securities = new ObservableCollection<Security>();

        #endregion

        public MainWindow()
        {
            InitializeComponent();

            cbSecurities.ItemsSource  = _securities;
        }

        private void Connect()
        {
            _trader = new QuikTrader();

            var columns = (_trader as QuikTrader).SecuritiesTable.Columns;
            columns.Add(DdeSecurityColumns.Strike);
            columns.Add(DdeSecurityColumns.Volatility);
            columns.Add(DdeSecurityColumns.UnderlyingSecurity);
            columns.Add(DdeSecurityColumns.TheorPrice);
            columns.Add(DdeSecurityColumns.OptionType);
            columns.Add(DdeSecurityColumns.ExpiryDate);
            
            if (_trader != null)
            {
                _trader.Connected += () => 
                {
                    _trader.NewSecurities += securities => this.GuiAsync(() =>
                    {
                        if (!_securities.Any(s => securities.Any(sec => sec.Code == s.Code)))
                        {
                            _securities.AddRange(securities);

                            if (cbSecurities.SelectedItem == null)
                                cbSecurities.SelectedItem = securities.Where(s => s.Type == SecurityTypes.Future).FirstOrDefault(s => s.Code.StartsWith("RTSI-") || s.Code.StartsWith("RI"));
                        }
                    });

                    if (!_trader.IsExportStarted)
                        _trader.StartExport();
                };

                _trader.Connect();
            }
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            Connect();
        }

        private void Securities_OnSelected(object sender, RoutedEventArgs e)
        {
            var trade = _trader.RegisteredTrades.FirstOrDefault();
            if (trade != null)
                _trader.UnRegisterTrades(_trader.Securities.FirstOrDefault(s => s.Code == trade.Code));

            var depth = _trader.RegisteredMarketDepths.FirstOrDefault();
            if (depth != null)
                _trader.UnRegisterMarketDepth(_trader.Securities.FirstOrDefault(s => s.Code == depth.Code));

            var security = _trader.RegisteredSecurities.FirstOrDefault();
            if (security != null)
                _trader.UnRegisterSecurity(_trader.Securities.FirstOrDefault(s => s.Code == depth.Code));

            _trader.RegisterTrades(SelectedSecurity);
            _trader.RegisterMarketDepth(SelectedSecurity);
            _trader.RegisterSecurity(SelectedSecurity);
        }

        private void PremiumClick(object sender, RoutedEventArgs e)
        {
        	lbPremium.Content = (new MyBlackScholes(SelectedSecurity)).Premium(tbPrice.Text.To<decimal>());
        }

        private Security SelectedSecurity
        {
            get { return (Security)cbSecurities.SelectedItem; }
        }
    }
}
