context: namespace StockSharp.Hydra.Panes
  {
  	using System;
  	using System.Collections.Generic;
  	using System.Collections.ObjectModel;
  	using System.ComponentModel;
  	using System.Diagnostics;
  	using System.Globalization;
  	using System.Linq;
  	using System.Windows;
  	using System.Windows.Controls;
  	using System.Windows.Data;
  	using System.Windows.Input;
  
  	using Ecng.Collections;
  	using Ecng.Common;
  	using Ecng.ComponentModel;
  	using Ecng.Configuration;
  	using Ecng.Serialization;
  	using Ecng.Xaml;
  
  	using MoreLinq;
  
  	using StockSharp.Algo;
  	using StockSharp.Algo.Candles;
  	using StockSharp.Algo.Storages;
  	using StockSharp.BusinessEntities;
  	using StockSharp.Hydra.Windows;
  	using StockSharp.Logging;
  	using StockSharp.Hydra.Core;
  	using StockSharp.Messages;
  	using StockSharp.Localization;
  
  	public partial class TaskPane : IPane, INotifyPropertyChanged
  	{
  		public static RoutedUICommand OpenDirectoryCommand = new RoutedUICommand();
  
  		private static IEntityRegistry EntityRegistry
  		{
  			get { return ConfigManager.GetService<IEntityRegistry>();  }
  		}
  
  		public static readonly DependencyProperty TaskProperty = DependencyProperty.Register("Task", typeof(IHydraTask), typeof(TaskPane), new PropertyMetadata(null,
  			(o, args) =>
  			{
  				var pane = (TaskPane)o;
  				var task = (IHydraTask)args.NewValue;
  				var prevTask = pane._task;
  
  				if (prevTask != null)
  				{
  					prevTask.Settings.PropertyChanged -= pane.SettingsOnPropertyChanged;
  
  					prevTask.Started -= pane.OnStartedTask;
  					prevTask.Stopped -= pane.OnStopedTask;
  				}
  
  				pane._task = task;
  				pane.DataContext = task;
  
  				if (task == null)
  				{
  					pane._dataTypes.Keys.ForEach(cb => cb.IsEnabled = false);
  					pane.Candles.IsEnabled = false;
  					return;
  				}
  
  				task.Settings.PropertyChanged += pane.SettingsOnPropertyChanged;
  				task.Started += pane.OnStartedTask;
  				task.Stopped += pane.OnStopedTask;
  
  				pane.AddAllSecurity.Content = Core.Extensions.AllSecurityId;
  				pane.TimeFrames.ItemsSource = new ObservableCollection<SelectableObject>(task.SupportedCandleSeries.Select(s => new SelectableObject(s)));
  
  				//pane.DeleteSecurities.IsEnabled = false;
  
  				foreach (var pair in pane._dataTypes)
  				{
  					var checkBox = pair.Key;
  
  					checkBox.IsEnabled = task.SupportedMarketDataTypes.Contains(pair.Value.Item1);
  
  					if (checkBox.IsEnabled)
  						continue;
  
  					checkBox.IsThreeState = false;
  					checkBox.IsChecked = false;
  				}
  
  				pane.Candles.IsEnabled = task.SupportedMarketDataTypes.Contains(typeof(Candle));
  			}));
  
  		private IHydraTask _task;
  
  		public IHydraTask Task
  		{
  			get { return _task; }
  			set { SetValue(TaskProperty, value); }
  		}
  
  		private void OnStartedTask(IHydraTask task)
  		{
  			this.GuiAsync(() => ControlPanel.IsEnabled = false);
  		}
  
  		private void OnStopedTask(IHydraTask task)
  		{
  			this.GuiAsync(() => ControlPanel.IsEnabled = true);
  		}
  
  		private void SettingsOnPropertyChanged(object sender, PropertyChangedEventArgs e)
  		{
  			if (e.PropertyName == "Title")
  				_propertyChanged.SafeInvoke(this, "Title");
  		}
  
  		private readonly List<HydraTaskSecurity> _allSecurities = new List<HydraTaskSecurity>();
  		private readonly ObservableCollection<TaskVisualSecurity> _filteredSecurities = new ObservableCollection<TaskVisualSecurity>();
  		private readonly Dictionary<CheckBox, Tuple<Type, Func<TaskVisualSecurity, bool>, Action<TaskVisualSecurity, bool>>> _dataTypes = new Dictionary<CheckBox, Tuple<Type, Func<TaskVisualSecurity, bool>, Action<TaskVisualSecurity, bool>>>();
  
  		private readonly PairSet<HydraTaskSecurity, TaskVisualSecurity> _visualSecurities = new PairSet<HydraTaskSecurity, TaskVisualSecurity>(); 
  
  		public class TaskVisualSecurity : NotifiableObject
  		{
  			public HydraTaskSecurity TaskSecurity { get; private set; }
  
  			public TaskVisualSecurity(HydraTaskSecurity taskSecurity)
  			{
  				TaskSecurity = taskSecurity;
  			}
  
  			public bool GetIsEnabled(Type dataType)
  			{
  				if (dataType == null)
  					throw new ArgumentNullException("dataType");
  
  				return TaskSecurity.MarketDataTypesSet.Contains(dataType);
  			}
  
  			public void SetIsEnabled(Type dataType, bool value)
  			{
  				if (dataType == null)
  					throw new ArgumentNullException("dataType");
  
  				var set = TaskSecurity.MarketDataTypesSet;
  
  				if (value)
  					set.Add(dataType);
  				else
  					set.Remove(dataType);
  
  				TaskSecurity.MarketDataTypes = set.ToArray();
  
  				NotifyChanged("Is{0}Enabled".Put(dataType.Name
  					.Replace("ChangeMessage", string.Empty)
  					.Replace("Message", string.Empty)
  					.Replace("Market", string.Empty)
  					.Replace("Item", string.Empty)));
  
  				NotifyChanged("IsInvalid");
  			}
  
  			public bool IsTradeEnabled
  			{
  				get { return GetIsEnabled(typeof(Trade)); }
  			}
  
  			public bool IsDepthEnabled
  			{
  				get { return GetIsEnabled(typeof(MarketDepth)); }
  			}
  
  			public bool IsLevel1Enabled
  			{
  				get { return GetIsEnabled(typeof(Level1ChangeMessage)); }
  			}
  
  			public bool IsOrderLogEnabled
  			{
  				get { return GetIsEnabled(typeof(OrderLogItem)); }
  			}
  
  			public bool IsCandleEnabled
  			{
  				get { return GetIsEnabled(typeof(Candle)); }
  			}
  
  			public bool IsExecutionEnabled
  			{
  				get { return GetIsEnabled(typeof(ExecutionMessage)); }
  			}
  
  			public bool IsInvalid
  			{
  				get
  				{
  					return
  						!IsTradeEnabled &&
  						!IsDepthEnabled &&
  						!IsLevel1Enabled &&
  						!IsOrderLogEnabled &&
  						!IsCandleEnabled &&
  						!IsExecutionEnabled;
  				}
  			}
  		}
  
  		private TaskVisualSecurity ToVisualSecurity(HydraTaskSecurity security)
  		{
  			if (security == null)
  				throw new ArgumentNullException("security");
  
  			return _visualSecurities.SafeAdd(security, key => new TaskVisualSecurity(key));
  		}
  
  		public TaskPane()
  		{
  			InitializeComponent();
  
  			SecuritiesCtrl.ItemsSource = _filteredSecurities;
  			SecuritiesCtrl.ContextMenu.Items.Add(new MenuItem
  			{
  				Header = LocalizedStrings.Str2916,
  				Command = OpenDirectoryCommand,
  			});
  
  			AddDataType<Trade>(Trades);
  			AddDataType<MarketDepth>(Depths);
  			AddDataType<Level1ChangeMessage>(Level1Changes);
  			AddDataType<OrderLogItem>(OrderLog);
  			AddDataType<ExecutionMessage>(Executions);
  		}
  
  		private void AddDataType<T>(CheckBox checkBox)
  		{
  			Func<TaskVisualSecurity, bool> get = s => s.GetIsEnabled(typeof(T));
  			Action<TaskVisualSecurity, bool> set = (s, v) => s.SetIsEnabled(typeof(T), v);
  			_dataTypes.Add(checkBox, Tuple.Create(typeof(T), get, set));
  		}
  
  		private bool _isInitialized;
  
  		private void TaskPane_OnLoaded(object sender, RoutedEventArgs e)
  		{
  			if (_isInitialized)
  				return;
  
  			_isInitialized = true;
  
  			_allSecurities.Clear();
  			_filteredSecurities.Clear();
  
  			Mouse.OverrideCursor = Cursors.Wait;
  
  			var task = Task;
  
  			System.Threading.Tasks.Task.Factory
  				.StartNew(() => _allSecurities.AddRange(task.Settings.Securities))
  				.ContinueWithExceptionHandling(this.GetWindow(), rest =>
  				{
  					FilterSecurities();
  					AddAllSecurity.IsEnabled = _allSecurities.All(s => !s.Security.IsAllSecurity());
  					Mouse.OverrideCursor = null;
  				});
  		}
  
  		private TaskVisualSecurity SelectedSecurity
  		{
  			get { return SelectedSecurities.FirstOrDefault(); }
  		}
  
  		public TaskVisualSecurity[] SelectedSecurities
  		{
  			get { return SecuritiesCtrl.SelectedItems.Cast<TaskVisualSecurity>().ToArray(); }
  		}
  
  		private void NameLikeTextChanged(object sender, TextChangedEventArgs e)
  		{
  			FilterSecurities();
  		}
  
  		private void FilterSecurities()
  		{
  			var secLike = NameLike.Text;
  
  			var selectedSecurities = _allSecurities
  				.Where
  				(s =>
  					secLike.IsEmpty() ||
  					(!s.Security.Id.IsEmpty() && s.Security.Id.ContainsIgnoreCase(secLike)) ||
  					(!s.Security.Code.IsEmpty() && s.Security.Code.ContainsIgnoreCase(secLike)) ||
  					(!s.Security.Name.IsEmpty() && s.Security.Name.ContainsIgnoreCase(secLike))
  				)
  				.ToArray();
  
  			_filteredSecurities.Clear();
  			_filteredSecurities.AddRange(selectedSecurities.Select(ToVisualSecurity));
  		}
  
  		private void ExecutedOpenDirectory(object sender, ExecutedRoutedEventArgs e)
  		{
  			var security = SelectedSecurity;
  
  			var drive = Task.Settings.Drive;
  
  
gt: 			if (!(drive is LocalMarketDataDrive))