using UIShare.UIViewModel;
using UIShare.PubEvent;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UIShare.GlobalVariable;
using static UIShare.UIViewModel.ParameterVM;
using UIShare.ViewModelBase;
using Prism.Ioc;
using Prism.Navigation.Regions;

namespace TestingModule.ViewModels.Dialogs
{
    public class ParameterSettingViewModel : DialogViewModelBase
    {
        #region 属性
        private string _title = "参数设置界面"; 
        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }
        private Array _EnumValues;
        public Array EnumValues
        {
            get => _EnumValues;
            set => SetProperty(ref _EnumValues, value);
        }
        private ObservableCollection<Type> _types =
            [
            typeof(string), typeof(bool),
            typeof(short), typeof(int), typeof(long), typeof(float), typeof(double),
            typeof(byte[]), typeof(short[]), typeof(ushort[]), typeof(int[]), typeof(long[]), typeof(float[]), typeof(double[]),
            typeof(object)
            ];
        public ObservableCollection<Type> Types
        {
            get => _types;
            set => SetProperty(ref _types, value);
        }

        private ObservableCollection<string> _categories = new ObservableCollection<string>(
                Enum.GetNames(typeof(ParameterCategory))
            );
        public ObservableCollection<string> Categories
        {
            get => _categories;
            set => SetProperty(ref _categories, value);
        }
         private string _Mode;
        public string Mode
        {
            get => _Mode;
            set => SetProperty(ref _Mode, value);
        }

        private ProgramVM _program;
        public ProgramVM Program
        {
            get => _program;
            set => SetProperty(ref _program, value);
        }
        private ParameterVM _Parameter;
        public ParameterVM Parameter
        {
            get => _Parameter;
            set => SetProperty(ref _Parameter, value);
        }
        #endregion
        public DialogCloseListener RequestClose{get;set;}
        private ScopedContext _ScopedContext;
        public ICommand CancelCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ParameterSettingViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new DelegateCommand(Save);
        }

        private void Save()
        {
            if (Mode == "ADD")
            {
                Program.Parameters.Add(Parameter);
                _ScopedContext.SelectedParameter = Parameter;
            }
            else
            {
                var index = Program.Parameters
                      .Select((x, i) => new { x, i })
                      .FirstOrDefault(p => p.x.ID == _ScopedContext.SelectedParameter.ID)?.i;
                if (!_ScopedContext.SelectedParameter.IsEditable)
                {
                    //公共变量只允许更改ParameterCategory为Input,Temp
                    if (Program.Parameters[index.Value].Value != Parameter.Value
    || Program.Parameters[index.Value].UpperLimit != Parameter.UpperLimit
    || Program.Parameters[index.Value].LowerLimit != Parameter.LowerLimit
    || Program.Parameters[index.Value].Name != Parameter.Name
    || Parameter.Category == ParameterCategory.Output
    || Program.Parameters[index.Value].Type != Parameter.Type)
                    {
                        RequestClose.Invoke(ButtonResult.Yes);
                        return;
                    }
                }
                if (index.HasValue)
                {
                    Program.Parameters[index.Value] = Parameter;
                }
            }
             
            RequestClose.Invoke(ButtonResult.OK);
        }

        private void Cancel()
        {
            RequestClose.Invoke(ButtonResult.No);
        }
        #region Prism Dialog 规范
       
        public override void OnDialogClosed()
        {
            _eventAggregator.GetEvent<OverlayEvent>().Publish(false);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("ScopedContext"))
                _ScopedContext = parameters.GetValue<ScopedContext>("ScopedContext");
            _eventAggregator.GetEvent<OverlayEvent>().Publish(true);
            Program =_ScopedContext.Program;
            Mode = parameters.GetValue<string>("Mode");
            if (Mode == "ADD")
            {
                Parameter = new();
            }
            else
            {
                Parameter = new ParameterVM(_ScopedContext.SelectedParameter);
            }
        }
        #endregion
    }
}
