using System.Windows.Input;
using JdeScoping.SecureStoreManager.Services;
namespace JdeScoping.SecureStoreManager.ViewModels;
///
/// View model for an individual secret item with show/hide toggle.
///
public class SecretItemViewModel : ViewModelBase
{
private readonly string _actualValue;
private readonly IClipboardService _clipboardService;
private bool _isValueVisible;
private const string MaskedValue = "********";
///
/// Initializes a new instance of the class.
///
/// The secret key name.
/// The secret value.
/// The clipboard service for copy operations.
public SecretItemViewModel(string key, string value, IClipboardService clipboardService)
{
Key = key;
_actualValue = value;
_clipboardService = clipboardService ?? throw new ArgumentNullException(nameof(clipboardService));
ToggleVisibilityCommand = new RelayCommand(ToggleVisibility);
CopyToClipboardCommand = new RelayCommand(CopyToClipboard);
}
///
/// Gets the secret key.
///
public string Key { get; }
///
/// Gets the displayed value (masked or actual based on visibility).
///
public string DisplayValue => _isValueVisible ? _actualValue : MaskedValue;
///
/// Gets the actual unmasked value.
///
public string ActualValue => _actualValue;
///
/// Gets or sets whether the value is visible.
///
public bool IsValueVisible
{
get => _isValueVisible;
set
{
if (SetProperty(ref _isValueVisible, value))
{
OnPropertyChanged(nameof(DisplayValue));
}
}
}
///
/// Command to toggle visibility of the secret value.
///
public ICommand ToggleVisibilityCommand { get; }
///
/// Command to copy the secret value to clipboard.
///
public ICommand CopyToClipboardCommand { get; }
///
/// Event raised when clipboard copy fails.
///
public event Action? OnCopyFailed;
private void ToggleVisibility()
{
IsValueVisible = !IsValueVisible;
}
private async void CopyToClipboard()
{
try
{
await _clipboardService.SetTextAsync(_actualValue);
}
catch (Exception ex)
{
OnCopyFailed?.Invoke($"Failed to copy to clipboard: {ex.Message}");
}
}
}