﻿using Prism.Commands;
using Prism.Mvvm;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
using ReNaming.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReNaming.ViewModels
{
    public class RenameConditionViewModel
    {
        public RenameRecipe RenameRecipe { get; private set; }

        public RenameCondition Condition { get; private set; }

        public ReactiveProperty<RenameSubConditionKind> ConditionKind { get; private set; }
        public ReadOnlyReactiveProperty<RenameBehaviorViewModelBase> SubConditionVM { get; private set; }



        public RenameConditionViewModel(RenameCondition condition, RenameRecipe recipe)
        {
            RenameRecipe = recipe;
            Condition = condition;

            ConditionKind = new ReactiveProperty<RenameSubConditionKind>(
                condition.SubCondition.Kind
                , mode: ReactivePropertyMode.DistinctUntilChanged
                );

            SubConditionVM = Condition.ObserveProperty(x => x.SubCondition)
                .Select(x => RenameSubConditionVMHelper.ToBehaviorVM(x))
                .ToReadOnlyReactiveProperty();

            ConditionKind.Subscribe(x => 
            {
                var subCondition = x.GenerateSubCondition();
                Condition.SubCondition = subCondition;
            });
        }

        private DelegateCommand _RemoveConditionCommnad;
        public DelegateCommand RemoveConditionCommand
        {
            get
            {
                return _RemoveConditionCommnad
                    ?? (_RemoveConditionCommnad = new DelegateCommand(() => 
                    {
                        RenameRecipe.Conditions.Remove(Condition);
                    }));
            }
        }
    }

    public static class RenameSubConditionVMHelper
    {
        public static RenameBehaviorViewModelBase ToBehaviorVM(IRenameSubCondition renameBehavior)
        {
            if (renameBehavior == null)
            {
                return null;
            }

            switch (renameBehavior.Kind)
            {
                case RenameSubConditionKind.NameStartWith:
                    return new TextInputRenameBehaviorViewModel<NameStartWithCondition>(renameBehavior as NameStartWithCondition, x => x.Text);
                case RenameSubConditionKind.NameEndWith:
                    return new TextInputRenameBehaviorViewModel<NameEndWithCondition>(renameBehavior as NameEndWithCondition, x => x.Text);
                case RenameSubConditionKind.NameContainString:
                    return new TextInputRenameBehaviorViewModel<NameContainStringCondition>(renameBehavior as NameContainStringCondition, x => x.Text);
                case RenameSubConditionKind.ItemType:
                    return new ListInputRenameBehaviorViewModel<ItemTypeCondition, ConditionItemType>(renameBehavior as ItemTypeCondition, x => x.ItemType);
                default:
                    throw new NotSupportedException(renameBehavior.Kind.ToString());
            }
        }
    }

    

}
