using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using VideoConcat.Models;
using RadioButton = System.Windows.Controls.RadioButton;

namespace VideoConcat.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private VideoWindow? _videoWindow;
        private ExtractWindow? _extractWindow;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowModel();
            // 窗口加载完成后执行
            Loaded += MainView_Loaded;
        }

        public void MainView_Loaded(object sender, RoutedEventArgs e)
        {
            // 1. 为目标Grid创建列定义
            SetupGridColumns(mainGrid);

            // 2. 实例化已有视图（或获取已存在的视图实例）
            _videoWindow = new VideoWindow(); // 这里是已有视图的实例

            // 3. 将视图添加到指定列中（例如第1列，索引为1）
            AddViewToColumn(mainGrid, _videoWindow);
        }

        /// <summary>
        /// 为Grid设置列定义
        /// </summary>
        private void SetupGridColumns(Grid grid)
        {
            // 清空现有列（可选）
            grid.Children.Clear();
        }


        /// <summary>
        /// 将视图添加到Grid的指定列
        /// </summary>
        /// <param name="grid">目标Grid</param>
        /// <param name="view">要添加的视图（UserControl/FrameworkElement）</param>
        /// <param name="columnIndex">列索引（从0开始）</param>
        private void AddViewToColumn(Grid grid, FrameworkElement view)
        {

            // 将视图添加到Grid的子元素中
            grid.Children.Add(view);
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {

            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }


        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (sender is RadioButton radioButton)
            {
                if (radioButton.Name == "extract")
                {
                    // 切换到extract tab，检查是否有正在进行的处理
                    if (_extractWindow != null)
                    {
                        var viewModel = _extractWindow.DataContext as ViewModels.ExtractWindowViewModel;
                        if (viewModel != null && viewModel.ExtractWindowModel.IsStart)
                        {
                            // 有正在进行的处理，允许切换，因为用户可能想查看进度
                            // 不做任何阻止操作
                        }
                    }

                    SetupGridColumns(mainGrid);
                    
                    // 如果已存在实例，重用；否则创建新实例
                    if (_extractWindow == null)
                    {
                        _extractWindow = new ExtractWindow();
                    }

                    // 将视图添加到Grid中
                    AddViewToColumn(mainGrid, _extractWindow);
                }

                if (radioButton.Name == "video")
                {
                    // 从extract切换到video时，检查是否有正在进行的处理
                    if (_extractWindow != null)
                    {
                        var viewModel = _extractWindow.DataContext as ViewModels.ExtractWindowViewModel;
                        if (viewModel != null && viewModel.ExtractWindowModel.IsStart)
                        {
                            // 有正在进行的处理，显示提示并阻止切换
                            System.Windows.MessageBox.Show("当前有正在进行的处理任务，请等待处理完成后再切换！", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                            // 恢复extract tab的选中状态
                            radioButton.IsChecked = false;
                            var extractRadioButton = FindName("extract") as RadioButton;
                            if (extractRadioButton != null)
                            {
                                extractRadioButton.IsChecked = true;
                            }
                            return;
                        }
                    }

                    SetupGridColumns(mainGrid);

                    // 如果已存在实例，重用；否则创建新实例
                    if (_videoWindow == null)
                    {
                        _videoWindow = new VideoWindow();
                    }

                    // 将视图添加到Grid中
                    AddViewToColumn(mainGrid, _videoWindow);
                }
            }
        }
    }


}
