using BOB.Models;
using Common.PubEvent;
using SqlSugar;
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 static BOB.Models.ParameterModel;

namespace BOB.ViewModels.Dialogs
{
    public class ParameterSettingViewModel : BindableBase, IDialogAware
    {
        #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 ProgramModel _program;
        public ProgramModel Program
        {
            get => _program;
            set => SetProperty(ref _program, value);
        }
        private ParameterModel _Parameter;
        public ParameterModel Parameter
        {
            get => _Parameter;
            set => SetProperty(ref _Parameter, value);
        }
        #endregion
        public DialogCloseListener RequestClose{get;set;}
        private GlobalVariables _globalVariables;
        private IEventAggregator _eventAggregator;
        public ICommand CancelCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ParameterSettingViewModel(GlobalVariables globalVariables, IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _globalVariables = globalVariables;
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new DelegateCommand(Save);
        }

        private void Save()
        {
            if (Mode == "ADD")
            {
                Program.Parameters.Add(Parameter);
                _globalVariables.SelectedParameter = Parameter;
            }
            else
            {
                var index = Program.Parameters
                      .Select((x, i) => new { x, i })
                      .FirstOrDefault(p => p.x.ID == _globalVariables.SelectedParameter.ID)?.i;

                if (index.HasValue)
                {
                    Program.Parameters[index.Value] = Parameter;
                }
            }
             
            RequestClose.Invoke(ButtonResult.OK);
        }

        private void Cancel()
        {
            RequestClose.Invoke(ButtonResult.No);
        }
        #region Prism Dialog 规范
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            _eventAggregator.GetEvent<OverlayEvent>().Publish(false);
        }

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