using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Comal.Classes; using H.Pipes; using InABox.Client.IPC; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.Wpf; using InABox.WPF; using PRSServer; using Color = System.Drawing.Color; using Image = System.Windows.Controls.Image; using Size = System.Drawing.Size; namespace PRSDesktop { /// /// Interaction logic for SelectDatabase.xaml /// public partial class SelectDatabase : ThemableWindow { public SelectDatabase(Dictionary databases) { InitializeComponent(); var active = databases.Where(x => x.Value.IsActive).ToArray().Length; var cols = (int)Math.Ceiling(Math.Sqrt(active)); for (var iCol = 0; iCol < cols; iCol++) Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) }); var rows = (int)Math.Ceiling(active / (double)cols); for (var iRow = 0; iRow < cols; iRow++) Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); var i = 0; foreach (var key in databases.Keys) if (databases[key].IsActive) { var db = databases[key]; var button = new Button { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(0), Padding = new Thickness(0) }; var color = Colors.Red; // try // { // color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(db.ColorScheme); // } // catch (Exception e) // { // color = Colors.Red; // } var border = new Border { CornerRadius = new CornerRadius(5), BorderBrush = new SolidColorBrush(color), BorderThickness = new Thickness(0.75), Background = db.DatabaseType == DatabaseType.Standalone ? new SolidColorBrush(Colors.WhiteSmoke) : new SolidColorBrush(Colors.White), Width = 200, Height = 200 }; var panel = new DockPanel { VerticalAlignment = VerticalAlignment.Stretch }; border.Child = panel; StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center }; stack.SetValue(DockPanel.DockProperty, Dock.Top); Label dbtype = new Label() { Content = db.DatabaseType.ToString(), Margin = new Thickness(5,2,0,0), HorizontalContentAlignment = HorizontalAlignment.Left, FontSize = 8, Foreground = new SolidColorBrush(Colors.DimGray) }; stack.Children.Add(dbtype); Label dbver = new Label() { Margin = new Thickness(0,2,5,0), HorizontalContentAlignment = HorizontalAlignment.Right, FontSize = 8, Foreground = new SolidColorBrush(Colors.DimGray) }; stack.Children.Add(dbver); panel.Children.Add(stack); var label = new Label { Content = key, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, Width = 150, Height = 40 }; label.SetValue(DockPanel.DockProperty, Dock.Bottom); panel.Children.Add(label); var image = new Image(); image.Height = 150; image.Width = 150; image.SetValue(DockPanel.DockProperty, Dock.Top); panel.Children.Add(image); button.Content = border; button.Margin = new Thickness(5, 5, 0, 0); button.Click += Button_Click; button.Height = 200; button.Width = 200; button.Tag = key; var col = i % cols; var row = i / cols; button.SetValue(Grid.ColumnProperty, col); button.SetValue(Grid.RowProperty, row); Grid.Children.Add(button); Task.Run(() => { DatabaseInfo info = new DatabaseInfo() { Version = CoreUtils.GetVersion(), ColorScheme = db.ColorScheme, Logo = db.Logo }; if (db.DatabaseType == DatabaseType.Local) info = new PipeIPCClient(DatabaseServerProperties.GetPipeName(db.LocalServerName)).Info(); else if (db.DatabaseType == DatabaseType.Networked) JsonClient.Ping(db.URLs, out info); UpdateInfo(key, db, info, border, image, dbver); }); i++; } } private bool LogoEqual(byte[]? first, byte[]? second) { if ((first == null) && (second == null)) return true; if ((first == null) || (second == null)) return false; return first.SequenceEqual(second); } private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo info, Border border, Image image, Label version) { Dispatcher.Invoke(() => { if(info.ColorScheme is not null) { try { var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme); border.BorderBrush = new SolidColorBrush(color); border.Background = new SolidColorBrush(color.AdjustLightness(90)); } catch { } } Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small; try { if (info.Logo != null && info.Logo.Any()) using (var ms = new MemoryStream(info.Logo)) bitmap = new Bitmap(ms); } catch { } if (bitmap != null) { var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150); image.Source = bitmap.AsBitmapImage(size.Height, size.Width); } try { version.Content = String.Equals(info.Version,"???") ? "" : info.Version; } catch { } if (!string.Equals(db.ColorScheme, info.ColorScheme) || !LogoEqual(db.Logo, info.Logo)) { db.ColorScheme = info.ColorScheme ?? DatabaseSettings.DefaultColorScheme; db.Logo = info.Logo; new LocalConfiguration(key).Save(db); } }); } public string Database { get; private set; } private void Button_Click(object sender, RoutedEventArgs e) { Database = (sender as Button).Tag as string; DialogResult = true; } //private void OK_Click(object sender, RoutedEventArgs e) //{ // DialogResult = true; //} //private void Cancel_Click(object sender, RoutedEventArgs e) //{ // DialogResult = false; //} } }