// Copyright (c) 2024- schick Informatik // Description: Window to show (complete) list of current shipcall histories // using BreCalClient.misc.Api; using BreCalClient.misc.Model; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Printing; using System.Windows; namespace BreCalClient { /// /// Interaction logic for HistoryDialog.xaml /// public partial class HistoryDialog : Window { #region Fields private readonly ConcurrentDictionary _shipcalls; private readonly StaticApi _staticApi; #endregion #region delegate/event to react to history item selection public event Action? HistoryItemSelected; #endregion #region Construction public HistoryDialog(ConcurrentDictionary shipcalls, StaticApi staticApi) { InitializeComponent(); _shipcalls = shipcalls; _staticApi = staticApi; } #endregion #region event handler private void Window_Loaded(object sender, RoutedEventArgs e) { RefreshHistory(); } private void buttonClose_Click(object sender, RoutedEventArgs e) { this.Close(); } #endregion #region private methods private async void RefreshHistory() { List allHistories = new(); foreach (int shipcall_id in _shipcalls.Keys) { List shipcallHistory = await _staticApi.HistoryGetAsync(shipcall_id); System.Diagnostics.Trace.WriteLine($"{shipcallHistory.Count} history elements loaded for shipcall {shipcall_id}"); allHistories.AddRange( shipcallHistory ); } // sort all entries allHistories.Sort((x, y) => { return y.Timestamp.CompareTo(x.Timestamp); }); // create controls for all entries foreach (History history in allHistories) { string shipname = ""; Ship? ship = this._shipcalls[history.ShipcallId].Ship; if (ship != null) shipname = ship.Name; string etaetd = "", calltype = ""; if (_shipcalls.ContainsKey(history.ShipcallId)) { etaetd = _shipcalls[history.ShipcallId].GetETAETD(); if (_shipcalls[history.ShipcallId].Shipcall != null) { ShipcallType? type = _shipcalls[history.ShipcallId].Shipcall?.Type; if (type != null) calltype = type.Value.ToString(); } } HistoryControl hc = new(shipname, history, calltype, etaetd); hc.HistorySelected += (x) => { HistoryItemSelected?.Invoke(x); }; // bubble event this.stackPanel.Children.Add(hc); } } #endregion } }