using MainShell.Common;
using MainShell.Filewritable;
using MainShell.Models;
using MainShell.Recipe.Models;
using MwFramework.Controls.Components;
using Stylet;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace MainShell.Recipe.ViewModel
{
    public class CarrierRecipeViewModel : RecipeViewModelBase<CarrierRecipe>,
        IHandle<SubstrateNameChangedEventArgs>, IHandle<SubstrateDeletedEventArgs>
    {
        #region //Command
        public ICommand RemoveSubstrateCommand { get; private set; }
        #endregion

        private CarrierRecipe _carrierRecipe;
        public CarrierRecipe CarrierRecipe
        {
            get { return _carrierRecipe; }
            set { SetAndNotify(ref _carrierRecipe, value); }
        }

        protected override CarrierRecipe CurrentRecipe
        {
            get => CarrierRecipe;
            set => CarrierRecipe = value;
        }

        protected override string RecipeFolderPath => Paths.CarrierRecipe;
        protected override string EmptyRecipeDisplayName => "当前载具配方";

        protected override string GetActiveRecipeName()
        {
            return RecipeManager.CurrentCarrierRecipe?.RecipeName;
        }

        protected override void SwitchActiveRecipe(string recipeName)
        {
            RecipeManager.SwitchCarrierRecipe(recipeName);
        }

        protected override void ClearActiveRecipe()
        {
            RecipeManager.ClearCarrierRecipe();
        }

        public ObservableCollection<RecipeWrap> SubstrateRecipes => _wrapManager.SubstrateRecipeWraps;

        private readonly RecipeWrapManager _wrapManager;
        private readonly IEventAggregator _eventAggregator;

        public CarrierRecipeViewModel(RecipeWrapManager wrapManager, IEventAggregator eventAggregator, RecipeManager recipeManager)
            : base(recipeManager)
        {
            _wrapManager = wrapManager;
            _eventAggregator = eventAggregator;
            _eventAggregator.Unsubscribe(this);
            _eventAggregator.Subscribe(this);

            RegisterButtonGroupEvents();
            RecipeWraps = wrapManager.CarrierRecipeWraps;
            RemoveSubstrateCommand = new DelegateCommand<object>(RemoveSubstrate);
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            InitializeSelection(RecipeManager.CurrentCarrierRecipe?.RecipeName);
        }

        protected override void OnRecipeLoaded(CarrierRecipe recipe, RecipeWrap recipeWrap)
        {
            foreach (var wrap in RecipeWraps)
            {
                wrap.IsInUse = ReferenceEquals(wrap, recipeWrap);
            }
        }

        public override void OnCopyRecipe(object sender, EventArgs eventArgs)
        {
            base.OnCopyRecipe(sender, eventArgs);
        }

        public override void OnImportRecipe(object sender, EventArgs eventArgs)
        {
            if (eventArgs is RecipeEventArgs args)
            {
            }
        }

        public override void OnSaveRecipe(object sender, EventArgs eventArgs)
        {
            if (eventArgs is RecipeEventArgs)
            {
                SaveCurrentRecipe(true);
            }
        }

        public override void OnExportRecipe(object sender, EventArgs eventArgs)
        {
            if (eventArgs is RecipeEventArgs args)
            {
            }
        }

        public override void OnReNameRecipe(object sender, EventArgs eventArgs)
        {
            if (eventArgs is RecipeRenameEventArgs args)
            {
                TryRenameRecipe(args);
            }
        }

        #region //Methods
        public void AddSubstrate()
        {
            if (CarrierRecipe == null)
            {
                MwMessageBox.Show("请先选择或创建载具配方");
                return;
            }
            CarrierRecipe.SubstrateSelectInfos.Add(new SubstrateSelectInfo());
        }

        public void RemoveSubstrate(object pars)
        {
            if (pars is SubstrateSelectInfo selectInfo)
            {
                CarrierRecipe.SubstrateSelectInfos.Remove(selectInfo);
            }
        }

        public void Handle(SubstrateNameChangedEventArgs message)
        {
            if (CarrierRecipe != null)
            {
                if (CarrierRecipe.SubstrateSelectInfos != null)
                {
                    foreach (var substrateInfo in CarrierRecipe.SubstrateSelectInfos)
                    {
                        if (substrateInfo.SubstrateName == message.OldName)
                        {
                            substrateInfo.SubstrateName = message.NewName;
                        }
                    }
                }
            }
        }

        public void Handle(SubstrateDeletedEventArgs message)
        {
            if (CarrierRecipe == null || CarrierRecipe.SubstrateSelectInfos == null)
                return;

            var list = CarrierRecipe.SubstrateSelectInfos;
            var targetName = message?.DeletedName;

            CommonUti.RunOnUi(() => RemoveByNameFromCollection(list, targetName));
        }

        private void RemoveByNameFromCollection(ObservableCollection<SubstrateSelectInfo> list, string name)
        {
            if (string.IsNullOrEmpty(name)) return;

            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i]?.SubstrateName == name)
                    list.RemoveAt(i);
            }
        }
        #endregion
    }
}
