using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.FSE.Common;
using Tango.FSE.Common.Helpers;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Integration;
using Tango.SharedUI;
namespace Tango.FSE.UI.Dialogs
{
///
/// Represents a machine TCP connection view model.
///
///
public class MachineConnectionWifiViewVM : MachineConnectionBaseViewVM
{
private String _password;
///
/// Gets or sets the password.
///
public String Password
{
get { return _password; }
set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private ExternalBridgeLoginIntent _selectedIntent;
///
/// Gets or sets the selected intent.
///
public ExternalBridgeLoginIntent SelectedIntent
{
get { return _selectedIntent; }
set { _selectedIntent = value; RaisePropertyChangedAuto(); }
}
///
/// Gets or sets the available intents.
///
public List Intents { get; set; }
private bool _requireSafetyLevelOperations;
///
/// Gets or sets a value indicating whether to require safety level operations on the remote machine.
///
public bool RequireSafetyLevelOperations
{
get { return _requireSafetyLevelOperations; }
set { _requireSafetyLevelOperations = value; RaisePropertyChangedAuto(); }
}
private bool _autoReconnection;
///
/// Gets or sets a value indicating whether keep trying to reconnect.
///
public bool AutoReconnection
{
get { return _autoReconnection; }
set { _autoReconnection = value; RaisePropertyChangedAuto(); }
}
private ExternalBridgeTcpClient _machine;
///
/// Gets or sets the selected machine.
///
public ExternalBridgeTcpClient Machine
{
get { return _machine; }
set { _machine = value; RaisePropertyChangedAuto(); }
}
private bool _rememberMachinePassword;
///
/// Gets or sets a value indicating whether to save the machine password.
///
public bool RememberMachinePassword
{
get { return _rememberMachinePassword; }
set { _rememberMachinePassword = value; RaisePropertyChangedAuto(); }
}
///
/// Initializes a new instance of the class.
///
/// The machine.
public MachineConnectionWifiViewVM(ExternalBridgeTcpClient machine)
{
Machine = machine;
Password = String.Empty;
Intents = new List()
{
ExternalBridgeLoginIntent.Diagnostics,
ExternalBridgeLoginIntent.FullControl
};
SelectedIntent = ExternalBridgeLoginIntent.Diagnostics;
var connectionSettings = Settings.StoredMachinesConnectionSettings.FirstOrDefault(x => x.SerialNumber == machine.SerialNumber);
if (connectionSettings != null)
{
Password = EncryptionHelper.Decrypt(connectionSettings.Password);
SelectedIntent = connectionSettings.Intent;
RequireSafetyLevelOperations = connectionSettings.RequireSafetyLevelOperations;
AutoReconnection = connectionSettings.AutoReconnection;
RememberMachinePassword = true;
}
}
///
/// Invokes the event.
///
protected override void Accept()
{
if (Machine != null)
{
if (RememberMachinePassword)
{
FSESettings.MachineConnectionSettings connectionSettings = null;
connectionSettings = Settings.StoredMachinesConnectionSettings.FirstOrDefault(x => x.SerialNumber == Machine.SerialNumber);
if (connectionSettings == null)
{
connectionSettings = new FSESettings.MachineConnectionSettings();
Settings.StoredMachinesConnectionSettings.Add(connectionSettings);
}
connectionSettings.SerialNumber = Machine.SerialNumber;
connectionSettings.Password = EncryptionHelper.Encrypt(Password);
connectionSettings.Intent = SelectedIntent;
connectionSettings.RequireSafetyLevelOperations = RequireSafetyLevelOperations;
connectionSettings.AutoReconnection = AutoReconnection;
}
else
{
Settings.StoredMachinesConnectionSettings.RemoveAll(x => x.SerialNumber == Machine.SerialNumber);
}
Settings.Save();
}
base.Accept();
}
///
/// Determines whether this instance can invoke the OK command.
///
///
protected override bool CanOK()
{
return Password.IsNotNullOrEmpty();
}
///
/// Gets the machine serial number.
///
///
public override string GetMachineSerialNumber()
{
return Machine.SerialNumber;
}
///
/// Called when the dialog has been shown.
///
public override void OnShow()
{
base.OnShow();
this.SetFocus(() => Password);
}
}
}