using System.Collections.ObjectModel;
using System.Windows;
using DemoLib.Model;
using DemoLib.Service;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Messaging;

namespace Demo1.ViewModel
{
    public class ReaderInfoViewModel : ViewModelBase
    {
        public ReaderInfoViewModel()
        {
            CloseCommand = new RelayCommand<Window>(CloseCommandExecute);
            ReaderProperties = new ObservableCollection<ReaderProperty>();

            Messenger.Default.Register<Reader>(this, ReceiveMessage);
        }

        #region Close

        public RelayCommand<Window> CloseCommand { get; private set; }

        private void CloseCommandExecute(Window window)
        {
            window.Close();
        }

        #endregion Close

        public ObservableCollection<ReaderProperty> ReaderProperties { get; set; }

        public Reader Reader { get; set; }

        #region Private Method

        private void ReceiveMessage(Reader reader)
        {
            Messenger.Default.Unregister<Reader>(this);

            Reader = reader;
            var properties = OpcUaService.Instance.GetReaderProperties(reader);
            ReaderProperties = new ObservableCollection<ReaderProperty>(properties);

            RaisePropertyChanged(nameof(Reader));
            RaisePropertyChanged(nameof(ReaderProperties));
        }

        #endregion Private Method
    }
}