using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using WolfOfWallStreet.Models; using WolfOfWallStreet.Models.OnlinePlus; using WolfOfWallStreet.Services; using WolfOfWallStreet.Services.Contracts; using MessageBox = HandyControl.Controls.MessageBox; namespace WolfOfWallStreet.ViewModels { public class OnlinePlusControllerViewModel : ViewModelBase { private readonly IOnlinePlusService _onlinePlusService; private readonly SettingsServices _settingsServices; public int Stock { get; set; } public OnlinePlusOrder OpOrder { get; set; } = new OnlinePlusOrder(); public bool Ordered = true; public ICommand SendOrderCommand { get; set; } public ICommand SelectSymbolCommand { get; set; } public ICommand StopOrderCommand { get; set; } public ICommand ClearOrdersCommand { get; set; } private OnlinePlusSymbol _onlinePlusSymbol; public OnlinePlusSymbol OnlinePlusSymbol { get { return _onlinePlusSymbol; } set { SetProperty(ref _onlinePlusSymbol, value); } } private string _cookies; public string Cookies { get { return _cookies; } set { SetProperty(ref _cookies, value , nameof(Cookies)); } } private OnlinePlusAccount _account; public OnlinePlusAccount Account { get { return _account; } set { SetProperty(ref _account, value, nameof(Account)); } } public ObservableCollection Symbols { get; set; } = new ObservableCollection(); public ObservableCollection PlusOrderResults { get; set; } = new ObservableCollection(); public OnlinePlusControllerViewModel(IOnlinePlusService onlinePlusService, SettingsServices settingsServices) { _onlinePlusService = onlinePlusService; _settingsServices = settingsServices; _settingsServices.GetSymbols().ForEach(s => Symbols.Add(s)); } public override void InitialCommand() { base.InitialCommand(); SendOrderCommand = new DelegateCommand(async () => { Ordered = true; for (int i = 0; i < 7; i++) { Thread thr = new Thread(Order); thr.Start(); } }); StopOrderCommand = new DelegateCommand(async () => { Ordered = false; }); SelectSymbolCommand = new DelegateCommand(async (symbol) => { OnlinePlusSymbol = await _onlinePlusService.GetSymbolOrderBase(symbol, Stock); }); ClearOrdersCommand = new DelegateCommand(()=>PlusOrderResults.Clear()); } async void Order() { while (Ordered) { try { OpOrder.Cookies = Cookies; if (string.IsNullOrEmpty(OpOrder.Cookies)) throw new Exception("کوکی های درخواست را وارد کنید"); OpOrder.orderCount = OnlinePlusSymbol.Quantity; OpOrder.orderPrice = OnlinePlusSymbol.symbolinfo.ht; OpOrder.isin = OnlinePlusSymbol.symbolinfo.nc; OnlinePlusOrderResult res; res = await _onlinePlusService.SendOrder(OpOrder); if(res!=null) Application.Current.Dispatcher.Invoke(() => PlusOrderResults.Add(res)); } catch (Exception e) { MessageBox.Show(e.Message); Ordered = false; } } } protected override bool SetProperty(ref T storage, T value, string propertyName = null) { var flag = base.SetProperty(ref storage, value, propertyName); if(propertyName==nameof(Cookies)) Account = _onlinePlusService.GetAccounTask(Cookies).Result; return flag; } } }