using MainShell.Filewritable;
using MaxwellFramework.Core.Attributes;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.Recipe.Models
{
    [Singleton]
    public class RecipeWrapManager : PropertyChangedBase , IHandle<SubstrateNameChangedEventArgs>
    {
        private ObservableCollection<RecipeWrap> _carrierRecipeWraps;

        public ObservableCollection<RecipeWrap> CarrierRecipeWraps
        {
            get { return _carrierRecipeWraps; }
            set { SetAndNotify(ref _carrierRecipeWraps, value); }
        }

        private ObservableCollection<RecipeWrap> _substrateRecipeWraps;

        public ObservableCollection<RecipeWrap> SubstrateRecipeWraps
        {
            get { return _substrateRecipeWraps; }
            set { SetAndNotify(ref _substrateRecipeWraps, value); }
        }
        private ObservableCollection<RecipeWrap> _waferRecipeWraps;

        public ObservableCollection<RecipeWrap> WaferRecipeWraps
        {
            get { return _waferRecipeWraps; }
            set { SetAndNotify(ref _waferRecipeWraps, value); }
        }
        private ObservableCollection<RecipeWrap> _processRecipeWraps;

        public ObservableCollection<RecipeWrap> ProcessRecipeWraps
        {
            get { return _processRecipeWraps; }
            set { SetAndNotify(ref _processRecipeWraps, value); }
        }

        public RecipeWrapManager(IEventAggregator eventAggregator)
        {
            CarrierRecipeWraps = new ObservableCollection<RecipeWrap>(LoadRecipes(Paths.CarrierRecipe));
            SubstrateRecipeWraps = new ObservableCollection<RecipeWrap>(LoadRecipes(Paths.SubstrateRecipe));
            WaferRecipeWraps = new ObservableCollection<RecipeWrap>(LoadRecipes(Paths.WaferRecipe));
            ProcessRecipeWraps = new ObservableCollection<RecipeWrap>(LoadRecipes(Paths.ProcessRecipe));
            eventAggregator.Unsubscribe(this);
            eventAggregator.Subscribe(this);
        }
        private IEnumerable<RecipeWrap> LoadRecipes(string dirPath)
        {
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            foreach (var dir in Directory.EnumerateDirectories(dirPath))
            {
                var name = Path.GetFileNameWithoutExtension(dir);
                yield return new RecipeWrap
                {
                    RecipeName = name,
                    CreateTime = Directory.GetCreationTime(dir),
                    ModifiedTime = Directory.GetLastWriteTime(dir),
                };
            }
        }

        public void Handle(SubstrateNameChangedEventArgs message)
        {
            CarrierRecipe carrierRecipe = new CarrierRecipe();
            foreach (var carrierWrap in CarrierRecipeWraps)
            {
                carrierRecipe.RecipeName= carrierWrap.RecipeName;
                carrierRecipe.Read();
                if(carrierRecipe.SubstrateSelectInfos!=null)
                {
                    foreach (var item in carrierRecipe.SubstrateSelectInfos)
                    {
                        if(item.SubstrateName==message.OldName)
                        {
                            item.SubstrateName= message.NewName;
                            carrierRecipe.Write();
                            break;
                        }
                    }
                }
            }
        }
    }
}
