using System.Collections.ObjectModel; namespace CommanderApp.Services; public class PanelManager : IPanelManager { public event EventHandler StateChanged; private bool _isLeftPanelActive = true; private int _leftSelectedIndex = -1; private int _rightSelectedIndex = -1; private string _leftPanelPath = string.Empty; private string _rightPanelPath = string.Empty; public bool IsLeftPanelActive => _isLeftPanelActive; public FileSystemItem SelectedItem => GetSelectedItem(); public string ActivePanelPath => _isLeftPanelActive ? _leftPanelPath : _rightPanelPath; public string LeftPanelPath => _leftPanelPath; public string RightPanelPath => _rightPanelPath; public int LeftSelectedIndex => _leftSelectedIndex; public int RightSelectedIndex => _rightSelectedIndex; public ObservableCollection LeftItems { get; } = new(); public ObservableCollection RightItems { get; } = new(); public void SwitchToLeftPanel() { if (!_isLeftPanelActive && LeftItems.Count > 0) { _isLeftPanelActive = true; OnStateChanged(); } } public void SwitchToRightPanel() { if (_isLeftPanelActive && RightItems.Count > 0) { _isLeftPanelActive = false; OnStateChanged(); } } public void MoveSelection(int direction) { if (_isLeftPanelActive) { if (LeftItems.Count == 0) return; var newIndex = Math.Max(0, Math.Min(LeftItems.Count - 1, _leftSelectedIndex + direction)); if (newIndex != _leftSelectedIndex) { _leftSelectedIndex = newIndex; OnStateChanged(); } } else { if (RightItems.Count == 0) return; var newIndex = Math.Max(0, Math.Min(RightItems.Count - 1, _rightSelectedIndex + direction)); if (newIndex != _rightSelectedIndex) { _rightSelectedIndex = newIndex; OnStateChanged(); } } } public void SetSelection(int index, bool isLeftPanel) { if (isLeftPanel) { if (index >= 0 && index < LeftItems.Count) { _leftSelectedIndex = index; _isLeftPanelActive = true; } } else { if (index >= 0 && index < RightItems.Count) { _rightSelectedIndex = index; _isLeftPanelActive = false; } } OnStateChanged(); } public void UpdatePanelPaths(string leftPath, string rightPath) { _leftPanelPath = leftPath; _rightPanelPath = rightPath; } public void ClearSelection() { _leftSelectedIndex = -1; _rightSelectedIndex = -1; OnStateChanged(); } private FileSystemItem GetSelectedItem() { return _isLeftPanelActive ? (_leftSelectedIndex >= 0 ? LeftItems[_leftSelectedIndex] : null) : (_rightSelectedIndex >= 0 ? RightItems[_rightSelectedIndex] : null); } private void OnStateChanged() { StateChanged?.Invoke(this, new PanelStateChangedEventArgs { IsLeftPanelActive = _isLeftPanelActive, LeftSelectedIndex = _leftSelectedIndex, RightSelectedIndex = _rightSelectedIndex, SelectedItem = GetSelectedItem() }); } }