using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Tango.FSE.Common;
using Tango.Integration.ExternalBridge;
namespace Tango.FSE.UI.Dialogs
{
///
/// Represents a machine communication failure view model.
///
///
public class MachineConnectionLostViewVM : FSEDialogViewVM
{
private DispatcherTimer _timer;
///
/// Gets or sets the connected machine.
///
public IExternalBridgeClient Machine { get; set; }
///
/// Gets or sets the error message.
///
public String Error { get; set; }
///
/// Gets or sets a value indicating whether to auto close this dialog with a reconnect response after a period of time expires.
///
public bool IsAutoReconnect { get; set; }
private int _remainingSeconds;
///
/// Gets or sets the remaining seconds.
///
public int RemainingSeconds
{
get { return _remainingSeconds; }
set { _remainingSeconds = value; RaisePropertyChangedAuto(); }
}
///
/// Initializes a new instance of the class.
///
/// The machine.
/// The error.
/// The automatic reconnect seconds.
public MachineConnectionLostViewVM(IExternalBridgeClient machine, String error, int? autoReconnectSeconds)
{
AutoMode = true;
OKText = "CONNECT";
CancelText = "CANCEL";
CanClose = true;
HasDefault = true;
Machine = machine;
Error = error;
IsAutoReconnect = autoReconnectSeconds != null;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += _timer_Tick;
if (IsAutoReconnect)
{
RemainingSeconds = Math.Max(autoReconnectSeconds.Value, 2);
_timer.Start();
}
}
///
/// Invokes the event.
///
protected override void Accept()
{
_timer.Stop();
base.Accept();
}
///
/// Invokes the event.
///
protected override void Cancel()
{
_timer.Stop();
base.Cancel();
}
private void _timer_Tick(object sender, EventArgs e)
{
if (IsVisible)
{
RemainingSeconds--;
if (RemainingSeconds == 0)
{
Accept();
}
}
}
}
}