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

namespace Ion.Sdk.DemoApp.Views.ViewModels
{

    public abstract class InspectionViewModel<TInspection> : TreeNodeViewModel where TInspection : Inspection<TInspection>
    {
        public TInspection Model { get; }
        public bool HasChildren { get; }

        protected InspectionViewModel(TInspection model)
        {
            Model = model;
            HasChildren = model.Children.Count > 0;
        }
    }

    public sealed class InspectionViewModel : InspectionViewModel<PredefinedInspection>
    {
        public bool IsLocked => !(Model is EmptyPredefinedInspection);

        public InspectionViewModel(PredefinedInspection model) : base(model)
        {
        }
    }

    public sealed class CustomInspectionViewModel : InspectionViewModel<CustomInspection>
    {
        public bool IsLocked => !(Model is EmptyCustomInspection);

        public CustomInspectionViewModel(CustomInspection model) : base(model) { }
    }

    public class InspectionToViewModelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is PredefinedInspection inspection)
                return new InspectionViewModel(inspection);
            if (value is CustomInspection customInspection)
                return new CustomInspectionViewModel(customInspection);
            return value;
        }

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