using BOB.Models;
using Common.PubEvent;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Xml;

namespace BOB.ViewModels
{
    public class StepsManagerViewModel:BindableBase
    {
        #region 属性
        private string _Title;
        public string Title
        {
            get => _Title;
            set => SetProperty(ref _Title, value);
        }
        private StepModel _SelectedStep;
        public StepModel SelectedStep
        {
            get => _SelectedStep;
            set
            {
                if (SetProperty(ref _SelectedStep, value))
                {
                    _globalVariables.SelectedStep = value;
                }
            }
        }
        public ProgramModel Program
        {
            get => _globalVariables.Program;
            set
            {
                if (_globalVariables.Program != value)
                {
                    _globalVariables.Program = value;
                    RaisePropertyChanged(); 
                }
            }
        }
        private bool _IsAdmin;
        public bool IsAdmin
        {
            get => _IsAdmin;
            set => SetProperty(ref _IsAdmin, value);
        }
        GlobalVariables _globalVariables { get; set; }
        private List<StepModel> tmpCopyList = new List<StepModel>();
        private IEventAggregator _eventAggregator;

        #endregion
        public ICommand EditStepCommand { get;set; }
        public ICommand CopyStepCommand { get;set; }
        public ICommand PasteStepCommand { get;set; }
        public ICommand DeleteStepCommand { get;set; }
        public StepsManagerViewModel(GlobalVariables _GlobalVariables,IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _globalVariables = _GlobalVariables;
            IsAdmin = _globalVariables.IsAdmin;
            EditStepCommand = new DelegateCommand(EditStep);
            CopyStepCommand = new DelegateCommand(CopyStep);
            PasteStepCommand = new DelegateCommand(PasteStep);
            DeleteStepCommand = new DelegateCommand(DeleteStep);
            Program.StepCollection.CollectionChanged += StepCollection_CollectionChanged;
            
        }
        #region 委托命令
        private void EditStep()
        {
            if (IsAdmin && SelectedStep != null&& _globalVariables.SelectedStep!=null)
            {
                _eventAggregator.GetEvent<EditSetpEvent>().Publish() ;
            }
        }
        private void CopyStep()
        {
            if (IsAdmin && SelectedStep != null)
            {
                tmpCopyList.Clear();
                tmpCopyList.Add(SelectedStep); 
            }
        }
        private void PasteStep()
        {
            if (IsAdmin && tmpCopyList.Any())
            {
                int insertIndex = Program.StepCollection.IndexOf(SelectedStep) + 1;
                foreach (var item in tmpCopyList)
                {
                    Program.StepCollection.Insert(insertIndex, new StepModel(item) { ID = Guid.NewGuid() });
                    insertIndex++;
                }
            }
        }
        private void DeleteStep()
        {
            if (IsAdmin && SelectedStep != null)
            {
                var tmpList = new List<StepModel> { SelectedStep };
                _eventAggregator.GetEvent<DeletedStepEvent>().Publish(SelectedStep.ID);
                foreach (var item in tmpList)
                {
                    Program.StepCollection.Remove(item);
                }
                _globalVariables.SelectedStep = null;
               
            }
        }
        #endregion

        #region 辅助方法
        private void StepCollection_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Add/Move/Remove 都会触发，这里判断具体情形
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
                e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move||
                 e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                // 如果需要等 UI 更新完再处理，可也用 Dispatcher 延迟一小段时间
                Application.Current?.Dispatcher.BeginInvoke(new Action(() =>
                {
                    // after drop: 重新编号或其他处理
                    for (int i = 0; i < Program.StepCollection.Count; i++)
                        Program.StepCollection[i].Index = i + 1;

                    // 发布 Prism 事件（如果需要）
                    // _eventAggregator.GetEvent<StepMovedEvent>().Publish(...);
                }), System.Windows.Threading.DispatcherPriority.Background);
            }
        }

        #endregion
    }
}
