// Copyright (c) 2023 schick Informatik // Description: Input control for outgoing shipcalls // using BreCalClient.misc.Model; using System; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Controls; using Xceed.Wpf.Toolkit; using static BreCalClient.Extensions; using System.Collections.Generic; using System.Linq; using System.Windows.Media; namespace BreCalClient { /// /// Interaction logic for EditTimesAgencyOutgoingControl.xaml /// public partial class EditTimesAgencyOutgoingControl : Window, IEditTimesControl { #region Fields bool _editing = false; #endregion #region Construction public EditTimesAgencyOutgoingControl() { InitializeComponent(); } #endregion #region Properties public ShipcallControlModel ShipcallModel { get; set; } = new(); public Times Times { get; set; } = new(); #endregion #region event handler private void Window_Loaded(object sender, RoutedEventArgs e) { this.comboBoxMooring.ItemsSource = BreCalLists.Participants_Mooring; this.comboBoxPilot.ItemsSource = BreCalLists.Participants_Pilot; this.comboBoxTug.ItemsSource = BreCalLists.Participants_Tug; this.comboBoxTerminal.ItemsSource = BreCalLists.Participants_Terminal; this.comboBoxDepartureBerth.ItemsSource = BreCalLists.Berths; this.CopyToControls(); this.Title = this.ShipcallModel.Title; Participant? p = null; if (this.ShipcallModel.AssignedParticipants.ContainsKey(ParticipantType.AGENCY)) p = BreCalLists.Participants.Find(x => x.Id == this.ShipcallModel.AssignedParticipants[ParticipantType.AGENCY].ParticipantId); bool allowBSMD = false; if (p != null) { allowBSMD = p.IsFlagSet(ParticipantFlag.ALLOW_BSMD); } _editing = (this.Times.ParticipantId == App.Participant.Id) || (App.Participant.IsTypeFlagSet(ParticipantType.BSMD) && allowBSMD); this.EnableControls(); } private void buttonOK_Click(object sender, RoutedEventArgs e) { if (!CheckValues(out string message)) { System.Windows.MessageBox.Show(message, BreCalClient.Resources.Resources.textWarning, MessageBoxButton.OK, MessageBoxImage.Warning); } else { this.CopyToModel(); this.DialogResult = true; this.Close(); } } private void buttonCancel_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; this.Close(); } #endregion #region private methods private bool CheckValues(out string message) { message = ""; if (this.datePickerETD.Value.HasValue && (this.datePickerETD.Value.Value < DateTime.Now) && (this.datePickerETD_End.Value == null)) { message = BreCalClient.Resources.Resources.textETDInThePast; return false; } if (this.datePickerETD_End.Value.HasValue && this.datePickerETD_End.Value < DateTime.Now) { message = BreCalClient.Resources.Resources.textETDInThePast; return false; } if (this.datePickerETD.Value.HasValue && this.datePickerETD_End.Value.HasValue && this.datePickerETD.Value > this.datePickerETD_End.Value) { message = BreCalClient.Resources.Resources.textEndValueBeforeStartValue; return false; } if (this.datePickerTidalWindowFrom.Value.HasValue && (this.datePickerTidalWindowFrom.Value.Value < DateTime.Now) && (this.datePickerTidalWindowTo.Value == null)) { message = BreCalClient.Resources.Resources.textTideTimesInThePast; return false; } if (this.datePickerTidalWindowTo.Value.HasValue && this.datePickerTidalWindowTo.Value < DateTime.Now) { message = BreCalClient.Resources.Resources.textTideTimesInThePast; return false; } if (this.datePickerTidalWindowFrom.Value.HasValue && this.datePickerTidalWindowTo.Value.HasValue && this.datePickerTidalWindowFrom.Value > this.datePickerTidalWindowTo.Value) { message = BreCalClient.Resources.Resources.textEndValueBeforeStartValue; return false; } if ((this.datePickerTidalWindowFrom.Value.HasValue && !this.datePickerTidalWindowTo.Value.HasValue) || (!this.datePickerTidalWindowFrom.Value.HasValue && this.datePickerTidalWindowTo.Value.HasValue)) { message = BreCalClient.Resources.Resources.textTidalBothValues; return false; } if (this.datePickerETD.Value.IsTooFar() || this.datePickerETD_End.Value.IsTooFar() || this.datePickerTidalWindowFrom.Value.IsTooFar() || this.datePickerTidalWindowTo.Value.IsTooFar()) { message = BreCalClient.Resources.Resources.textTooFarInTheFuture; return false; } if((this.datePickerETD_End.Value.HasValue && !this.datePickerETD.Value.HasValue) || (this.datePickerTidalWindowTo.Value.HasValue && !this.datePickerTidalWindowFrom.Value.HasValue)) { message = BreCalClient.Resources.Resources.textStartTimeMissing; return false; } return true; } private void CopyToModel() { if (this.ShipcallModel.Shipcall != null) { this.Times.EtdBerth = this.datePickerETD.Value; this.Times.EtdIntervalEnd = this.datePickerETD_End.Value; if (this.comboBoxPierside.SelectedIndex >= 0) { this.ShipcallModel.Shipcall.PierSide = (this.comboBoxPierside.SelectedIndex == 0); } else { this.ShipcallModel.Shipcall.PierSide = null; } this.Times.BerthId = (int?)this.comboBoxDepartureBerth.SelectedValue; this.Times.BerthInfo = this.textBoxBerthRemarks.Text.Trim(); this.ShipcallModel.Shipcall.Draft = (float?)this.doubleUpDownDraft.Value; this.ShipcallModel.Shipcall.TidalWindowFrom = this.datePickerTidalWindowFrom.Value; this.ShipcallModel.Shipcall.TidalWindowTo = this.datePickerTidalWindowTo.Value; this.ShipcallModel.Shipcall.Canceled = this.checkBoxCanceled.IsChecked; this.ShipcallModel.Shipcall.RecommendedTugs = this.integerUpDownRecommendedTugs.Value; this.ShipcallModel.Shipcall.MooredLock = this.checkBoxMooredLock.IsChecked; this.ShipcallModel.Shipcall.RainSensitiveCargo = this.checkBoxRainsensitiveCargo.IsChecked; if(!string.IsNullOrEmpty(this.textBoxRemarks.Text.Trim())) this.Times.Remarks = this.textBoxRemarks.Text.Trim(); Participant? participant = (Participant?)this.comboBoxMooring.SelectedItem; if (participant != null) { ParticipantAssignment participantAssignment = new() { ParticipantId = participant.Id, Type = (int)Extensions.ParticipantType.MOORING }; this.ShipcallModel.AssignedParticipants[Extensions.ParticipantType.MOORING] = participantAssignment; } participant = (Participant?)this.comboBoxPilot.SelectedItem; if (participant != null) { ParticipantAssignment participantAssignment = new() { ParticipantId = participant.Id, Type = (int)Extensions.ParticipantType.PILOT }; this.ShipcallModel.AssignedParticipants[Extensions.ParticipantType.PILOT] = participantAssignment; } participant = (Participant?)this.comboBoxTerminal.SelectedItem; if (participant != null) { ParticipantAssignment participantAssignment = new() { ParticipantId = participant.Id, Type = (int)Extensions.ParticipantType.TERMINAL }; this.ShipcallModel.AssignedParticipants[Extensions.ParticipantType.TERMINAL] = participantAssignment; } participant = (Participant?)this.comboBoxTug.SelectedItem; if (participant != null) { ParticipantAssignment participantAssignment = new() { ParticipantId = participant.Id, Type = (int)Extensions.ParticipantType.TUG }; this.ShipcallModel.AssignedParticipants[Extensions.ParticipantType.TUG] = participantAssignment; } } } private void CopyToControls() { if (this.ShipcallModel == null) return; if (this.ShipcallModel.Shipcall != null) { if (this.Times.EtdBerth.HasValue) { this.datePickerETD.Value = this.Times.EtdBerth.Value; } else { // if not set through times use value of BSMD entry if (this.ShipcallModel.Shipcall.Etd != DateTime.MinValue) this.datePickerETD.Value = this.ShipcallModel.Shipcall.Etd; } this.datePickerETD_End.Value = this.Times.EtdIntervalEnd; if (this.Times.BerthId.HasValue) this.comboBoxDepartureBerth.SelectedValue = this.Times.BerthId; else if (this.ShipcallModel.Shipcall.DepartureBerthId.HasValue) this.comboBoxDepartureBerth.SelectedValue = this.ShipcallModel.Shipcall.DepartureBerthId; if (this.ShipcallModel.Shipcall.PierSide.HasValue) { if (this.ShipcallModel.Shipcall.PierSide.Value) this.comboBoxPierside.SelectedIndex = 0; else this.comboBoxPierside.SelectedIndex = 1; } this.textBoxBerthRemarks.Text = this.Times.BerthInfo; this.doubleUpDownDraft.Value = this.ShipcallModel.Shipcall.Draft; this.datePickerTidalWindowFrom.Value = this.ShipcallModel.Shipcall.TidalWindowFrom; this.datePickerTidalWindowTo.Value = this.ShipcallModel.Shipcall.TidalWindowTo; this.checkBoxCanceled.IsChecked = this.ShipcallModel.Shipcall.Canceled ?? false; this.integerUpDownRecommendedTugs.Value = this.ShipcallModel.Shipcall.RecommendedTugs; this.checkBoxMooredLock.IsChecked = this.ShipcallModel.Shipcall.MooredLock ?? false; this.checkBoxRainsensitiveCargo.IsChecked = this.ShipcallModel.Shipcall.RainSensitiveCargo ?? false; if ((this.ShipcallModel.Shipcall.TimeRefPoint ?? 0) == 0) this.labelETD.Content = BreCalClient.Resources.Resources.textETDBerth; else this.labelETD.Content = string.Format("ETD {0}", BreCalLists.TimeRefs[this.ShipcallModel.Shipcall.TimeRefPoint ?? 0]); if (!string.IsNullOrEmpty(this.Times.Remarks)) this.textBoxRemarks.Text = this.Times.Remarks; if (this.ShipcallModel.AssignedParticipants.ContainsKey(ParticipantType.MOORING)) { if (BreCalLists.ParticipantLookupDict.ContainsKey(this.ShipcallModel.AssignedParticipants[ParticipantType.MOORING].ParticipantId)) { this.comboBoxMooring.SelectedValue = this.ShipcallModel.AssignedParticipants[ParticipantType.MOORING].ParticipantId; } } if (this.ShipcallModel.AssignedParticipants.ContainsKey(ParticipantType.PILOT)) { if (BreCalLists.ParticipantLookupDict.ContainsKey(this.ShipcallModel.AssignedParticipants[ParticipantType.PILOT].ParticipantId)) { this.comboBoxPilot.SelectedValue = this.ShipcallModel.AssignedParticipants[ParticipantType.PILOT].ParticipantId; } } if (this.ShipcallModel.AssignedParticipants.ContainsKey(ParticipantType.TERMINAL)) { if (BreCalLists.ParticipantLookupDict.ContainsKey(this.ShipcallModel.AssignedParticipants[ParticipantType.TERMINAL].ParticipantId)) { this.comboBoxTerminal.SelectedValue = this.ShipcallModel.AssignedParticipants[ParticipantType.TERMINAL].ParticipantId; } } if (this.ShipcallModel.AssignedParticipants.ContainsKey(ParticipantType.TUG)) { if (BreCalLists.ParticipantLookupDict.ContainsKey(this.ShipcallModel.AssignedParticipants[ParticipantType.TUG].ParticipantId)) { this.comboBoxTug.SelectedValue = this.ShipcallModel.AssignedParticipants[ParticipantType.TUG].ParticipantId; } } } } private void EnableControls() { this.datePickerETD.IsEnabled = _editing; this.datePickerETD_End.IsEnabled = _editing; this.comboBoxDepartureBerth.IsEnabled = _editing; this.comboBoxPierside.IsEnabled = _editing; this.textBoxBerthRemarks.IsReadOnly = !_editing; this.doubleUpDownDraft.IsEnabled = _editing; this.datePickerTidalWindowFrom.IsEnabled = _editing; this.datePickerTidalWindowTo.IsEnabled = _editing; this.checkBoxCanceled.IsEnabled = _editing; this.comboBoxTug.IsEnabled = _editing; this.integerUpDownRecommendedTugs.IsEnabled = _editing; this.comboBoxPilot.IsEnabled = _editing; this.comboBoxMooring.IsEnabled = _editing; this.checkBoxMooredLock.IsEnabled = _editing; this.comboBoxTerminal.IsEnabled = _editing; this.checkBoxRainsensitiveCargo.IsEnabled = _editing; this.textBoxRemarks.IsReadOnly = !_editing; CheckOKButton(); } private bool RequiredFieldsSet() { bool areSet = this.datePickerETD.Value.HasValue && this.doubleUpDownDraft.Value.HasValue && this.comboBoxDepartureBerth.SelectedIndex >= 0; if (areSet && this.datePickerETD_End.Value.HasValue) areSet &= (this.datePickerETD_End.Value > this.datePickerETD.Value); return areSet; } private void CheckOKButton() { this.buttonOK.IsEnabled = _editing && RequiredFieldsSet(); } #endregion #region event handlers private void contextMenuItemClearTug_Click(object sender, RoutedEventArgs e) { this.comboBoxTug.SelectedIndex = -1; this.ShipcallModel.AssignedParticipants.Remove(Extensions.ParticipantType.TUG); } private void contextMenuItemClearPilot_Click(object sender, RoutedEventArgs e) { this.comboBoxPilot.SelectedIndex = -1; this.ShipcallModel.AssignedParticipants.Remove(Extensions.ParticipantType.PILOT); } private void contextMenuItemClearMooring_Click(object sender, RoutedEventArgs e) { this.comboBoxMooring.SelectedIndex = -1; this.ShipcallModel.AssignedParticipants.Remove(Extensions.ParticipantType.MOORING); } private void contextMenuItemDepartureBerth_Click(object sender, RoutedEventArgs e) { this.comboBoxDepartureBerth.SelectedIndex = -1; this.ShipcallModel.Berth = ""; } private void contextMenuItemClearTerminal_Click(object sender, RoutedEventArgs e) { this.comboBoxTerminal.SelectedIndex = -1; this.ShipcallModel.AssignedParticipants.Remove(Extensions.ParticipantType.TERMINAL); } private void contextMenuItemClearPierside_Click(object sender, RoutedEventArgs e) { this.comboBoxPierside.SelectedIndex = -1; } private void datePickerETD_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { CheckOKButton(); } private void comboBoxDepartureBerth_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { CheckOKButton(); } private void doubleUpDownDraft_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { CheckOKButton(); } #endregion } }