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
{

    public enum Gender
    {
        Male,
        Female
    }
    public class VideoModel : INotifyPropertyChanged
    {
        private int _num;
        private int _maxNum;
        private string _folderPath = "";
        private string _auditImagePath = "";
        private bool _canStart = false;
        private bool _isCanOperate = false;
        private bool _isStart = false;
        private ObservableCollection<FolderInfo> _FolderInfos = [];
        private ObservableCollection<ConcatVideo> _concatVideos = [];
        private Dispatcher _dispatcher;

        public static int Index = 0;


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

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

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

        public int Num
        {
            get => _num;
            set
            {
                _num = value;
                OnPropertyChanged();
                UpdateSum();
            }
        }

        public string AuditImagePath
        {
            get => _auditImagePath;
            set
            {
                _auditImagePath = value;
                OnPropertyChanged();
            }
        }

        public int MaxNum
        {
            get => _maxNum;
            set
            {
                _maxNum = value;
            }
        }

        public struct FolderInfo
        {
            public DirectoryInfo DirectoryInfo { set; get; }
            public int Num { set; get; }

            public List<string> VideoPaths { set; get; }
        }


        public class ConcatVideo : INotifyPropertyChanged
        {
            private int _index;
            private string _fileName = "";
            private string _filePath = "";
            private string _size = "";
            private int _seconds;
            private string _status = "";
            private string _progress = "";

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

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

            public string FilePath
            {
                get => _filePath;
                set
                {
                    _filePath = value;
                    OnPropertyChanged();
                }
            }

            public string Size
            {
                get => _size;
                set
                {
                    _size = value;
                    OnPropertyChanged();
                }
            }

            public int Seconds
            {
                get => _seconds;
                set
                {
                    _seconds = value;
                    OnPropertyChanged();
                }
            }

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

            public string Progress
            {
                get => _progress;
                set
                {
                    _progress = value;
                    OnPropertyChanged();
                    // 同时更新进度条数值
                    UpdateProgressValue();
                }
            }

            private double _progressValue = 0;
            public double ProgressValue
            {
                get => _progressValue;
                set
                {
                    _progressValue = value;
                    OnPropertyChanged();
                }
            }

            private void UpdateProgressValue()
            {
                // 从进度文本中提取百分比数值
                if (string.IsNullOrEmpty(_progress))
                {
                    ProgressValue = 0;
                    return;
                }

                // 处理各种进度格式： "50%", "50", "拼接中...", "100%", "失败" 等
                if (_progress.EndsWith("%"))
                {
                    string percentStr = _progress.Replace("%", "").Trim();
                    if (double.TryParse(percentStr, out double percent))
                    {
                        ProgressValue = percent;
                    }
                    else
                    {
                        ProgressValue = 0;
                    }
                }
                else if (_progress == "拼接成功" || _progress == "100%")
                {
                    ProgressValue = 100;
                }
                else if (_progress == "拼接失败" || _progress == "失败")
                {
                    ProgressValue = 0;
                }
                else if (_progress.Contains("中") || _progress.Contains("..."))
                {
                    // 处理中状态，保持当前进度或设置为中间值
                    if (ProgressValue < 10)
                    {
                        ProgressValue = 10; // 开始处理时显示10%
                    }
                }
                else
                {
                    // 尝试解析为数字
                    if (double.TryParse(_progress, out double value))
                    {
                        ProgressValue = value;
                    }
                }
            }

            public event PropertyChangedEventHandler? PropertyChanged;

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

        public ObservableCollection<FolderInfo> FolderInfos
        {
            get { return _FolderInfos; }
            set
            {
                if (_FolderInfos != null)
                {
                    _FolderInfos.CollectionChanged -= ItemList_CollectionChanged;
                }
                _FolderInfos = value;
                if (_FolderInfos != null)
                {
                    _FolderInfos.CollectionChanged += ItemList_CollectionChanged;
                }
                OnPropertyChanged();
                UpdateSum();
            }
        }


        public ObservableCollection<ConcatVideo> ConcatVideos
        {
            get { return _concatVideos; }
            set
            {
                if (_concatVideos != null)
                {
                    _concatVideos.CollectionChanged -= ItemList_CollectionChanged;
                }
                _concatVideos = value;
                if (_concatVideos != null)
                {
                    _concatVideos.CollectionChanged += ItemList_CollectionChanged;
                }

                OnPropertyChanged();
            }
        }

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

        public bool CanStart
        {
            get { return _canStart; }
            set
            {
                _canStart = value;
                OnPropertyChanged(nameof(CanStart));
            }
        }

        public ICommand? BtnOpenFolderCommand { get; set; }
        public ICommand? BtnStartVideoConcatCommand { 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));
        }

        private void ItemList_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateSum();
        }

        public void UpdateSum()
        {
            int _temp = 1;
            if (FolderInfos.Count > 0)
            {
                if (IsJoinType1Selected)
                {
                    foreach (FolderInfo item in FolderInfos)
                    {
                        if (item.Num > 0)
                        {
                            _temp *= item.Num;
                        }
                    }
                    MaxNum = _temp;
                }
                if (IsJoinType2Selected)
                {
                    MaxNum = FolderInfos[0].Num;
                }
                SetCanStart();
            }
            else
            {
                MaxNum = 0;
                SetCanStart();
            }
        }

        public void SetCanStart()
        {
            if (Num > 0 && Num <= MaxNum && IsStart == false)
            {
                CanStart = true;
            }
            else
            {
                CanStart = false;
            }
            IsCanOperate = !IsStart;
        }

        public VideoModel()
        {
            ConcatVideos = [];
            _dispatcher = Dispatcher.CurrentDispatcher;
        }

        private bool _isJoinType1Selected;
        private bool _isJoinType2Selected;

        public bool IsJoinType1Selected
        {
            get { return _isJoinType1Selected; }
            set
            {
                _isJoinType1Selected = value;
                OnPropertyChanged(nameof(IsJoinType1Selected));
            }
        }

        public bool IsJoinType2Selected
        {
            get { return _isJoinType2Selected; }
            set
            {
                _isJoinType2Selected = value;
                OnPropertyChanged(nameof(IsJoinType2Selected));
            }
        }
    }

}
