using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Ion.Sdk.DemoApp.Framework;
using Ion.Sdk.DemoApp.Models;
using Ion.Sdk.Idi;

namespace Ion.Sdk.DemoApp.Views.ViewModels
{
    public sealed class StructureObjectViewModel : TreeNodeViewModel
    {
        private Lazy<TestsCollection> tests;
        public StructureObject Model { get; }

        public TestsCollection Tests
        {
            get => tests.Value;
        }

        public bool HasParameters { get; }

        public StructureObjectViewModel(StructureObject model)
        {
            Model = model;
            tests = new Lazy<TestsCollection>(() =>
            {
                var items = new TestsCollection();
                foreach (var test in model.Tests)
                    items.Add(test);
                return items;
            });

            HasParameters = model.HasParameters;
        }

        public void AddTest(ITest test)
        {
            Model.Tests.Add(test);
            Tests.Add(test);
            IsExpanded = true;
        }
    }

    public sealed class StructureObjectViewModelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is StructureObject structureObject)
                return new ViewModels.StructureObjectViewModel(structureObject);
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as ViewModels.StructureObjectViewModel)?.Model;
        }
    }
}
