using System;
using System.Collections.Generic;
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.DemoApp.Framework;
using Ion.Sdk.Idi;

namespace Ion.Sdk.DemoApp.Views
{
    /// <summary>
    /// Interaction logic for InstrumentBrowser.xaml
    /// </summary>
    public partial class InstrumentBrowser : UserControl, IDialog, IDisposable
    {
        #region -=Instance members=-
        public event EventHandler Closed;
        public Func<WorkspaceInfo, Task> OnWorkspaceSelected;

        public List<WorkspaceInfo> Workspaces
        {
            get { return (List<WorkspaceInfo>)GetValue(WorkspacesProperty); }
            set { SetValue(WorkspacesProperty, value); }
        }

        public Command CloseCommand { get; }
        public Command SelectCommand { get; }

        public InstrumentBrowser()
        {
            CloseCommand = Command.Create(parameter => Closed?.Invoke(this, EventArgs.Empty));
            SelectCommand = Command.CreateAsync(async parameter =>
            {
                await OnWorkspaceSelected(parameter as WorkspaceInfo);
                Closed?.Invoke(this, EventArgs.Empty);
            });

            InitializeComponent();
            Root.DataContext = this;
        }

        public void Dispose()
        {
            OnWorkspaceSelected = null;
        }

        #endregion

        #region -=Static members=-
        public static readonly DependencyProperty WorkspacesProperty = DependencyProperty.Register(
            "Workspaces", typeof(List<WorkspaceInfo>), typeof(InstrumentBrowser), new PropertyMetadata(default(List<WorkspaceInfo>)));
        #endregion
    }
}
