using System; using System.Reactive; using Avalonia.Media; using Npgsql; using pgLabII.Model; using ReactiveUI; namespace pgLabII.ViewModels; // UI ViewModel that wraps the persistence entity public class ServerConfigurationViewModel : ReactiveObject { private readonly ServerConfigurationEntity _entity; public ServerConfigurationViewModel(ServerConfigurationEntity entity) { _entity = entity ?? throw new ArgumentNullException(nameof(entity)); EditCommand = ReactiveCommand.Create(() => { var window = new Views.EditServerConfigurationWindow(new(this)) { New = false }; window.Show(); }); ExploreCommand = ReactiveCommand.Create(() => { var window = new Views.SingleDatabaseWindow(entity); window.Show(); }); } public ServerConfigurationEntity Entity => _entity; public Guid Id { get => _entity.Id; set { if (_entity.Id != value) { _entity.Id = value; this.RaisePropertyChanged(); } } } public string Name { get => _entity.Name; set { if (_entity.Name != value) { _entity.Name = value; this.RaisePropertyChanged(); } } } public string Host { get => _entity.Host; set { if (_entity.Host != value) { _entity.Host = value; this.RaisePropertyChanged(); } } } public ushort Port { get => _entity.Port; set { if (_entity.Port != value) { _entity.Port = value; this.RaisePropertyChanged(); } } } public string InitialDatabase { get => _entity.InitialDatabase; set { if (_entity.InitialDatabase != value) { _entity.InitialDatabase = value; this.RaisePropertyChanged(); } } } public SslMode DefaultSslMode { get => _entity.SslMode; set { if (_entity.SslMode != value) { _entity.SslMode = value; this.RaisePropertyChanged(); } } } public bool ColorEnabled { get => _entity.ColorEnabled; set { if (_entity.ColorEnabled != value) { _entity.ColorEnabled = value; this.RaisePropertyChanged(); this.RaisePropertyChanged(nameof(BackgroundBrush)); } } } public Color Color { get => Color.FromUInt32((uint)_entity.ColorArgb); set { var argb = unchecked((int)value.ToUInt32()); if (_entity.ColorArgb != argb) { _entity.ColorArgb = argb; this.RaisePropertyChanged(); this.RaisePropertyChanged(nameof(BackgroundBrush)); } } } public IBrush? BackgroundBrush => ColorEnabled ? new SolidColorBrush(Color) : null; public string UserName { get => _entity.UserName; set { if (_entity.UserName != value) { _entity.UserName = value; this.RaisePropertyChanged(); } } } public string Password { get => _entity.Password; set { if (_entity.Password != value) { _entity.Password = value; this.RaisePropertyChanged(); } } } public ReactiveCommand EditCommand { get; } public ReactiveCommand ExploreCommand { get; } }