using System.Windows.Controls;
using System.Windows.Input;

namespace NavisworksTransport.UI.WPF.Views
{
    /// <summary>
    /// MediaControlBar.xaml 的交互逻辑
    /// 专业级媒体播放器风格的动画控制条
    /// </summary>
    public partial class MediaControlBar : UserControl
    {
        public MediaControlBar()
        {
            InitializeComponent();
            
            // 启用键盘焦点以支持快捷键
            this.Focusable = true;
            this.KeyDown += OnKeyDown;
            
            LogManager.Debug("MediaControlBar 初始化完成");
        }

        /// <summary>
        /// 键盘快捷键处理
        /// </summary>
        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                var viewModel = this.DataContext as NavisworksTransport.UI.WPF.ViewModels.AnimationControlViewModel;
                if (viewModel == null) return;

                // 处理键盘快捷键
                switch (e.Key)
                {
                    case Key.Space:
                        // 空格键：播放/暂停切换
                        if (viewModel.CanPauseAnimation)
                        {
                            viewModel.PauseAnimationCommand?.Execute(null);
                        }
                        else if (viewModel.CanStartAnimation)
                        {
                            viewModel.StartAnimationCommand?.Execute(null);
                        }
                        e.Handled = true;
                        break;

                    case Key.Left:
                        // 左箭头：单帧后退 或 Shift+左箭头：快退10帧
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            viewModel.FastBackwardCommand?.Execute(null);
                        }
                        else
                        {
                            viewModel.StepBackwardCommand?.Execute(null);
                        }
                        e.Handled = true;
                        break;

                    case Key.Right:
                        // 右箭头：单帧前进 或 Shift+右箭头：快进10帧
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            viewModel.FastForwardCommand?.Execute(null);
                        }
                        else
                        {
                            viewModel.StepForwardCommand?.Execute(null);
                        }
                        e.Handled = true;
                        break;

                    case Key.R:
                        // R键：切换播放方向
                        if (viewModel.IsPlayingReverse)
                        {
                            viewModel.PlayForwardCommand?.Execute(null);
                        }
                        else
                        {
                            viewModel.PlayReverseCommand?.Execute(null);
                        }
                        e.Handled = true;
                        break;

                    case Key.Home:
                        // Home键：跳转到开头
                        viewModel.SeekToStartCommand?.Execute(null);
                        e.Handled = true;
                        break;

                    case Key.End:
                        // End键：跳转到结尾
                        viewModel.SeekToEndCommand?.Execute(null);
                        e.Handled = true;
                        break;

                    case Key.Escape:
                        // Esc键：停止播放
                        viewModel.StopAnimationCommand?.Execute(null);
                        e.Handled = true;
                        break;
                }

                if (e.Handled)
                {
                    LogManager.Debug($"处理键盘快捷键: {e.Key} (修饰键: {Keyboard.Modifiers})");
                }
            }
            catch (System.Exception ex)
            {
                LogManager.Error($"处理键盘快捷键时发生错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 控件获得焦点时自动聚焦到时间轴滑块，以便支持键盘操作
        /// </summary>
        private void OnGotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                // 将焦点设置到时间轴滑块，以便键盘操作
                TimelineSlider?.Focus();
            }
            catch (System.Exception ex)
            {
                LogManager.Debug($"设置焦点时出现异常: {ex.Message}");
            }
        }

        /// <summary>
        /// 鼠标进入控制条时获取焦点，以便支持键盘快捷键
        /// </summary>
        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!this.IsFocused)
                {
                    this.Focus();
                }
            }
            catch (System.Exception ex)
            {
                LogManager.Debug($"鼠标进入时设置焦点异常: {ex.Message}");
            }
        }
    }
}