using System.Linq; using System.Threading.Tasks; using System.Windows; using AutoBidder.Utilities; using AutoBidder.ViewModels; namespace AutoBidder { /// /// Command implementations for MainWindow /// public partial class MainWindow { private void InitializeCommands() { StartAllCommand = new RelayCommand(_ => ExecuteStartAll()); StopAllCommand = new RelayCommand(_ => ExecuteStopAll()); PauseAllCommand = new RelayCommand(_ => ExecutePauseAll()); GridStartCommand = new RelayCommand(param => ExecuteGridStart(param as AuctionViewModel)); GridPauseCommand = new RelayCommand(param => ExecuteGridPause(param as AuctionViewModel)); GridStopCommand = new RelayCommand(param => ExecuteGridStop(param as AuctionViewModel)); GridBidCommand = new RelayCommand(async param => await ExecuteGridBidAsync(param as AuctionViewModel)); } private void ExecuteStartAll() { StartButton_Click(null, null); } private void ExecuteStopAll() { StopButton_Click(null, null); } private void ExecutePauseAll() { PauseAllButton_Click(null, null); } private void ExecuteGridStart(AuctionViewModel? vm) { if (vm == null) return; // Attiva l'asta vm.IsActive = true; vm.IsPaused = false; // Se il monitoraggio globale non è attivo, avvialo automaticamente if (!_isAutomationActive) { _auctionMonitor.Start(); _isAutomationActive = true; Log($"[AUTO-START] Monitoraggio avviato automaticamente per asta: {vm.Name}", LogLevel.Info); } else { Log($"[START] Asta avviata: {vm.Name}", LogLevel.Info); } UpdateGlobalControlButtons(); } private void ExecuteGridPause(AuctionViewModel? vm) { if (vm == null) return; vm.IsPaused = true; Log($"[PAUSA] Asta in pausa: {vm.Name}", LogLevel.Info); UpdateGlobalControlButtons(); } private void ExecuteGridStop(AuctionViewModel? vm) { if (vm == null) return; vm.IsActive = false; // Se tutte le aste sono fermate, ferma anche il monitoraggio globale bool hasActiveAuctions = _auctionViewModels.Any(a => a.IsActive); if (!hasActiveAuctions && _isAutomationActive) { _auctionMonitor.Stop(); _isAutomationActive = false; Log($"[AUTO-STOP] Monitoraggio fermato: nessuna asta attiva", LogLevel.Info); } else { Log($"[STOP] Asta fermata: {vm.Name}", LogLevel.Info); } UpdateGlobalControlButtons(); } private async Task ExecuteGridBidAsync(AuctionViewModel? vm) { if (vm == null) return; try { Log($"[BID] Puntata manuale richiesta su: {vm.Name}", LogLevel.Info); var result = await _auctionMonitor.PlaceManualBidAsync(vm.AuctionInfo); if (result.Success) Log($"[OK] Puntata manuale su {vm.Name}: {result.LatencyMs}ms", LogLevel.Success); else Log($"[FAIL] Puntata manuale su {vm.Name}: {result.Error}", LogLevel.Error); } catch (System.Exception ex) { Log($"[ERRORE] Puntata manuale: {ex.Message}", LogLevel.Error); } } } }