using Standard;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
using static VideoConcat.Models.VideoModel;

namespace VideoConcat.Models
{
    /// <summary>
    /// 抽帧处理方式
    /// </summary>
    public enum ExtractFrameMode
    {
        DeleteFrame = 0,      // 删除帧
        AddTransparentImage = 1  // 添加透明图
    }

    /// <summary>
    /// 处理任务信息（用于表格显示）
    /// </summary>
    public class ExtractTaskItem : INotifyPropertyChanged
    {
        private string _index = "";
        private string _fileName = "";
        private string _fullFileName = "";
        private string _status = "";
        private string _originalSize = "";
        private string _outputSize = "";
        private string _sizeChange = "";
        private string _duration = "";
        private string _progress = "";

        public string Index
        {
            get => _index;
            set { _index = value; OnPropertyChanged(); }
        }

        public string FileName
        {
            get => _fileName;
            set { _fileName = value; OnPropertyChanged(); }
        }

        /// <summary>
        /// 完整文件名（用于ToolTip显示）
        /// </summary>
        public string FullFileName
        {
            get => _fullFileName;
            set { _fullFileName = value; OnPropertyChanged(); }
        }

        public string Status
        {
            get => _status;
            set { _status = value; OnPropertyChanged(); }
        }

        public string OriginalSize
        {
            get => _originalSize;
            set { _originalSize = value; OnPropertyChanged(); }
        }

        public string OutputSize
        {
            get => _outputSize;
            set { _outputSize = value; OnPropertyChanged(); }
        }

        public string SizeChange
        {
            get => _sizeChange;
            set { _sizeChange = value; OnPropertyChanged(); }
        }

        public string Duration
        {
            get => _duration;
            set { _duration = value; OnPropertyChanged(); }
        }

        public string Progress
        {
            get => _progress;
            set { _progress = value; OnPropertyChanged(); }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class ExtractWindowModel : INotifyPropertyChanged
    {

        private string _folderPath = "";
        private string _helpInfo = "";
        private bool _canExtractFrame = false;
        private bool _canModify = false;
        private bool _isCanOperate = false;
        private bool _isStart = false;
        private string[] _videos = [];
        private int _extractCount = 1;
        private Dispatcher _dispatcher;
        private ExtractFrameMode _extractFrameMode = ExtractFrameMode.DeleteFrame;
        private ObservableCollection<ExtractTaskItem> _taskItems = new();

        public string[] videos
        {
            get => _videos;
            set
            {
                _videos = value;
                OnPropertyChanged();
            }
        }

        public bool IsCanOperate
        {
            get => _isCanOperate;
            set
            {
                _isCanOperate = value;
                OnPropertyChanged();
            }
        }

        public bool IsStart
        {
            get => _isStart;
            set
            {
                _isStart = value;
                OnPropertyChanged();
            }
        }

        public Dispatcher Dispatcher
        {
            get => _dispatcher;
            set
            {
                _dispatcher = value;
            }
        }


        public class VideoStruct
        {
            public int Index { set; get; }
            public string FileName { set; get; } = "";
            public string Size { set; get; } = "";
            public int Seconds { set; get; }
            public string Status { set; get; } = "";
            public string Progress { set; get; } = "";
        }

        public string FolderPath
        {
            get { return _folderPath; }
            set
            {
                _folderPath = value;
                OnPropertyChanged(nameof(FolderPath));
            }
        }

        public string HelpInfo
        {
            get { return _helpInfo; }
            set
            {
                _helpInfo = value;
                OnPropertyChanged();
            }
        }

        public bool CanExtractFrame
        {
            get { return _canExtractFrame; }
            set
            {
                _canExtractFrame = value;
                OnPropertyChanged(nameof(CanExtractFrame));
            }
        }
        public bool CanModify
        {
            get { return _canModify; }
            set
            {
                _canModify = value;
                OnPropertyChanged(nameof(CanModify));
            }
        }

        public ICommand? BtnOpenFolderCommand { get; set; }
        public ICommand? BtnStartVideoConcatCommand { get; set; }
        public ICommand? BtnStartVideoModifyCommand { get; set; }
        public ICommand? BtnChooseAuditImageCommand { get; set; }

        public event PropertyChangedEventHandler? PropertyChanged;


        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }


        public ExtractWindowModel()
        {
            _dispatcher = Dispatcher.CurrentDispatcher;
        }

        public int ExtractCount
        {
            get => _extractCount;
            set
            {
                _extractCount = value;
                OnPropertyChanged();
                SetCanStart();
            }
        }

        public ExtractFrameMode ExtractFrameMode
        {
            get => _extractFrameMode;
            set
            {
                _extractFrameMode = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsDeleteFrameMode));
                OnPropertyChanged(nameof(IsAddTransparentImageMode));
            }
        }

        /// <summary>
        /// 是否选择删除帧模式（用于RadioButton绑定）
        /// </summary>
        public bool IsDeleteFrameMode
        {
            get => _extractFrameMode == ExtractFrameMode.DeleteFrame;
            set
            {
                if (value && _extractFrameMode != ExtractFrameMode.DeleteFrame)
                {
                    ExtractFrameMode = ExtractFrameMode.DeleteFrame;
                }
            }
        }

        /// <summary>
        /// 是否选择添加透明图模式（用于RadioButton绑定）
        /// </summary>
        public bool IsAddTransparentImageMode
        {
            get => _extractFrameMode == ExtractFrameMode.AddTransparentImage;
            set
            {
                if (value && _extractFrameMode != ExtractFrameMode.AddTransparentImage)
                {
                    ExtractFrameMode = ExtractFrameMode.AddTransparentImage;
                }
            }
        }

        /// <summary>
        /// 抽帧按钮的文本（固定显示"操作"）
        /// </summary>
        public string ExtractButtonText
        {
            get
            {
                return "操作";
            }
        }

        /// <summary>
        /// 任务列表（用于表格显示）
        /// </summary>
        public ObservableCollection<ExtractTaskItem> TaskItems
        {
            get => _taskItems;
            set
            {
                _taskItems = value;
                OnPropertyChanged();
            }
        }

        public void SetCanStart()
        {
            // 有视频文件且生成个数大于0时，可以抽帧
            CanExtractFrame = videos.Length > 0 && _extractCount > 0;
            
            // 有视频文件时可以修改
            if (videos.Length > 0)
            {
                CanModify = true;
            }
            else
            {
                CanModify = false;
            }
        }
    }

}
