using System; using System.Collections.Generic; using System.Linq; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; using C4IT.FASD.Base; using static C4IT.Logging.cLogManager; namespace FasdDesktopUi.Basics.UserControls { public partial class TimerView : UserControl { #region Fields public event EventHandler OnPauseStarted; private static readonly List _startTimes = new List(); private static readonly List _endTimes = new List(); private static readonly List _pausedTimesStart = new List(); private static readonly List _pausedTimesEnd = new List(); public static List CaseTimes = new List(); private static Dictionary _finalWorkingTimes = new Dictionary(); private static DispatcherTimer _timer; private static TimeSpan _elapsedTime; private static TimeSpan _totalPausedTime = TimeSpan.Zero; private static TimeSpan _pauseDuration; #endregion #region public TimerView() { InitializeComponent(); } public static void ResetTimer() { _startTimes.Clear(); _endTimes.Clear(); _pausedTimesEnd.Clear(); _pausedTimesStart.Clear(); _finalWorkingTimes.Clear(); _elapsedTime = TimeSpan.Zero; CaseTimes.Clear(); StartTimer(); } public static void StartTimer() { try { _startTimes?.Add(DateTime.UtcNow); _timer?.Start(); } catch (Exception E) { LogException(E); } } public void StopTimer() { try { _endTimes?.Add(DateTime.UtcNow); _timer?.Stop(); UpdateTimerControl(); } catch (Exception E) { LogException(E); } } private void UserControl_Initialized(object sender, EventArgs e) { try { ResetTimer(); _timer = new DispatcherTimer(TimeSpan.FromSeconds(1.0), DispatcherPriority.Loaded, new EventHandler((s, args) => { try { UpdateTimerControl(); } catch { } }), Dispatcher.CurrentDispatcher); } catch (Exception E) { LogException(E); } } private void UpdateTimerControl() { try { TimerControl.Text = _elapsedTime.ToString(@"hh\:mm\:ss"); _elapsedTime = _elapsedTime.Add(TimeSpan.FromSeconds(1.0)); } catch (Exception E) { LogException(E); } } public static Dictionary GetWorkTimes() { try { var currentDate = DateTime.UtcNow.Date.ToString("d"); var startDateTime = _startTimes.First(); var endDateTime = DateTime.UtcNow; var bruttoWorkingTime = TimeSpan.Zero; var nettoWorkingTime = TimeSpan.Zero; if (_pausedTimesStart.Count == 0) { _totalPausedTime = TimeSpan.Zero; } if (_pausedTimesEnd.Count > 0) { _totalPausedTime = TimeSpan.Zero; for (int i = 0; i < _pausedTimesStart.Count; i++) { var start = _pausedTimesStart[i]; var end = _pausedTimesEnd[i]; _pauseDuration = end - start; _totalPausedTime += _pauseDuration; } } bruttoWorkingTime = endDateTime - startDateTime; nettoWorkingTime = bruttoWorkingTime - _totalPausedTime; _finalWorkingTimes = new Dictionary() { {"CurrentDate", currentDate}, {"StartTime", _startTimes.First()}, {"EndTime", endDateTime }, {"BruttoWorkingTime", bruttoWorkingTime }, {"NettoWorkingTime", nettoWorkingTime.TotalSeconds }, {"TotalPausedTime", _totalPausedTime } }; } catch (Exception E) { LogException(E); } return _finalWorkingTimes; } #endregion #region Click-Events public void EndPause() { try { if (_timer?.IsEnabled == true) return; _timer?.Start(); _pausedTimesEnd?.Add(DateTime.UtcNow); CaseTimes.Add(new cF4SDCaseTime() { StatusId = CaseStatus.InProgress, CaseTime = DateTime.UtcNow }); } catch (Exception E) { LogException(E); } } private void PauseButton_Click() { try { _timer?.Stop(); _pausedTimesStart?.Add(DateTime.UtcNow); CaseTimes.Add(new cF4SDCaseTime() { StatusId = CaseStatus.OnHold, CaseTime = DateTime.UtcNow }); if (OnPauseStarted != null) OnPauseStarted?.Invoke(this, new EventArgs()); } catch (Exception E) { LogException(E); } } private void Border_Click(object sender, InputEventArgs e) => PauseButton_Click(); #endregion } }