using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ion.Sdk.Ddr;
using Ion.Sdk.DemoApp.Framework;
using Ion.Sdk.DemoApp.Views.ViewModels;
using Ion.Sdk.Idi;

namespace Ion.Sdk.DemoApp.Views
{
    /// <summary>
    /// Interaction logic for SelectMeasurement.xaml
    /// </summary>
    public partial class SelectMeasurement : IDialog
    {
        #region -=Instance members=-
        public event EventHandler Closed;

        public StructureObjectViewModel StructureObject
        {
            get { return (StructureObjectViewModel)GetValue(StructureObjectProperty); }
            set { SetValue(StructureObjectProperty, value); }
        }

        public Command CloseCommand { get; }
        public Command AddCommand { get; }

        public SelectMeasurement()
        {
            CloseCommand = Command.Create(obj => Closed?.Invoke(this, EventArgs.Empty));
            AddCommand = Command.CreateAsync(async obj =>
            {
                (Root.DataContext as ViewModel)?.AddMeasurement(obj as MeasurementInfo);
                await CloseCommand.Execute(obj);
            });

            InitializeComponent();
            Root.DataContext = new ViewModel();

        }

        #endregion

        #region -=Static members=-
        public static readonly DependencyProperty StructureObjectProperty = DependencyProperty.Register(
            "StructureObject", typeof(StructureObjectViewModel), typeof(SelectMeasurement), new PropertyMetadata(null, new PropertyChangedCallback(OnStructureObjectChanged)));

        private static void OnStructureObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = ((SelectMeasurement)d).Root.DataContext as ViewModel;
            if (viewModel != null && e.NewValue != null)
            {
                viewModel.StructureObject = e.NewValue as StructureObjectViewModel;
                viewModel.LoadMeasurements();
            }
        }
        #endregion

        #region -=Definitions=-

        public sealed class ViewModel : BindableObject
        {
            public List<MeasurementInfo> Measurements { get; private set; }
            public StructureObjectViewModel StructureObject { get; set; }

            public void AddMeasurement(MeasurementInfo measurementInfo)
            {
                if (measurementInfo == null) return;
                var measurement = new EmptyMeasurement(measurementInfo.Id, StructureObject.Model.Region);
                StructureObject.AddTest(new SingleTest(measurement));
            }

            internal void LoadMeasurements()
            {
                Measurements = new List<MeasurementInfo>(from item in DataDisplayInfos.MeasurementInfoList
                                                         where StructureObject.Model.Region.IsInRegion(item.SimpleRegionInfoList) && !item.IsObsolete
                                                         select item);
                OnPropertyChanged(nameof(Measurements));
            }
        }
        #endregion


    }
}
