using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Emulations.ExternalBridge;
using Tango.Integration.ExternalBridge;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Notifications;
using Tango.Settings;
using Tango.SharedUI;
namespace Tango.MachineStudio.UI.ViewModels
{
///
/// Represents the Machine Studio connection dialog, view model.
///
///
public class MachineConnectionViewVM : DialogViewVM
{
private EmulatorExternalBridge _emulator;
private ExternalBridgeScanner _scanner;
///
/// Gets or sets the machine scanner.
///
public ExternalBridgeScanner Scanner
{
get { return _scanner; }
set { _scanner = value; RaisePropertyChangedAuto(); }
}
private IExternalBridgeClient _selectedMachine;
///
/// Gets or sets the selected machine.
///
public IExternalBridgeClient SelectedMachine
{
get { return _selectedMachine; }
set { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
///
/// Gets or sets the connect command.
///
public RelayCommand ConnectCommand { get; set; }
///
/// Initializes a new instance of the class.
///
/// The scanner.
public MachineConnectionViewVM(ExternalBridgeScanner scanner)
{
Scanner = scanner;
ConnectCommand = new RelayCommand(Connect, (x) => SelectedMachine != null);
}
///
/// Connect to the currently selected machine.
///
private void Connect()
{
if (SelectedMachine != null)
{
Scanner.Stop();
Accept();
}
}
///
/// Invokes the event.
///
protected override void Cancel()
{
Scanner.Stop();
base.Cancel();
}
///
/// Called when the dialog has been shown.
///
public override void OnShow()
{
base.OnShow();
Scanner.AvailableMachines.Clear();
if (SettingsManager.Default.GetOrCreate().UseExternalBridgeEmulator)
{
if (_emulator != null)
{
_emulator.Disconnect();
}
_emulator = new EmulatorExternalBridge();
}
Scanner.AvailableMachines.Add(_emulator);
Scanner.Start();
}
}
}