// 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; using System.Windows.Input; 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) { Mouse.OverrideCursor = Cursors.Wait; RefreshHistory(); } private void buttonClose_Click(object sender, RoutedEventArgs e) { this.Close(); } private void checkboxMyOwnOnly_Checked(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = Cursors.Wait; RefreshHistory(); } #endregion #region private methods private async void RefreshHistory() { List allHistories = new(); this.stackPanel.Children.Clear(); 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); }); EnumToStringConverter enumToStringConverter = new(); // create controls for all entries foreach (History history in allHistories) { if (FilterShipcall(history.ShipcallId)) continue; 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 = (string) (enumToStringConverter.Convert(type ?? ShipcallType.Undefined, typeof(ShipcallType), new(), System.Globalization.CultureInfo.CurrentCulture) ?? ""); } } HistoryControl hc = new(shipname, history, calltype, etaetd); hc.HistorySelected += (x) => { HistoryItemSelected?.Invoke(x); }; // bubble event this.stackPanel.Children.Add(hc); } Mouse.OverrideCursor = null; } bool FilterShipcall(int shipcallId) { bool result = true; if (shipcallId < 0) return result; if(_shipcalls.TryGetValue(shipcallId, out ShipcallControlModel? scm)) { if(this.checkboxMyOwnOnly.IsChecked ?? false) { foreach(ParticipantAssignment p in scm.AssignedParticipants.Values) { if (p.ParticipantId.Equals(App.Participant.Id)) return false; } } else { return false; } } return result; } #endregion } }