using System; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using ClassNotify.Client.Services; using ClassNotify.Client.ViewModels; using ClassNotify.Client.Views; using ClassNotify.Shared.Enums; using ClassNotify.Shared.Models; namespace ClassNotify.Client { public partial class DeviceStatusWindow : Window { private readonly DeviceStatusViewModel _viewModel; private readonly IpcClient? _ipcClient; private readonly FirstPairingView _firstPairingView; private readonly RunningDeviceView _runningDeviceView; private long _lastStateRevision = 0; private bool _allowRealExit = false; public DeviceStatusViewModel ViewModel => _viewModel; public DeviceStatusWindow(ClientStateSnapshotDto state, Action onReactivateRequested, IpcClient? ipcClient = null) { InitializeComponent(); _ipcClient = ipcClient; _viewModel = new DeviceStatusViewModel(); this.DataContext = _viewModel; _firstPairingView = new FirstPairingView(); _firstPairingView.Initialize(_ipcClient); _runningDeviceView = new RunningDeviceView(); _runningDeviceView.Initialize(_ipcClient); // Connect view switching to ViewModel.Page UpdateContentPage(); _viewModel.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(DeviceStatusViewModel.Page)) { Dispatcher.InvokeAsync(UpdateContentPage); } }; ApplyState(state); if (_ipcClient != null) { _ipcClient.OnEnterPairingMode += OnRemoteEnterPairingMode; this.Closed += (s, e) => { _ipcClient.OnEnterPairingMode -= OnRemoteEnterPairingMode; _viewModel.StopCountdownTimer(); }; } else { this.Closed += (s, e) => { _viewModel.StopCountdownTimer(); }; } this.Loaded += async (s, e) => { await InitialFetchPairingCodeAsync(); }; } private void UpdateContentPage() { MainContent.Content = _viewModel.Page == DevicePage.FirstPairing ? _firstPairingView : _runningDeviceView; } public void ApplyState(ClientStateSnapshotDto state) { if (state == null) return; // Monotonic state revision guard if (state.StateRevision > 0 && state.StateRevision < _lastStateRevision) { return; } _lastStateRevision = Math.Max(_lastStateRevision, state.StateRevision); _viewModel.ApplySnapshot(state); } private async Task InitialFetchPairingCodeAsync() { if (_ipcClient == null) return; try { var purpose = _viewModel.Page == DevicePage.FirstPairing ? PairingPurpose.InitialDeviceBinding : PairingPurpose.AddTeacher; var session = await _ipcClient.RequestPairingSessionAsync(purpose); if (session != null) { _viewModel.ApplyPairingSession(session); } } catch (Exception ex) { App.LogClient($"InitialFetchPairingCode error: {ex.Message}"); } } private void OnRemoteEnterPairingMode(string code, int ttl) { Dispatcher.Invoke(() => { _viewModel.UpdatePairingCode(code, ttl); }); } private void OnTitleBarMouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { try { this.DragMove(); } catch { } } } private void OnMinimizeClick(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } private void OnHideToBackgroundClick(object sender, RoutedEventArgs e) { this.Hide(); this.ShowInTaskbar = false; } public void ForceClose() { _allowRealExit = true; this.Close(); } protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { if (!_allowRealExit) { e.Cancel = true; this.Hide(); this.ShowInTaskbar = false; return; } base.OnClosing(e); } } }