// Copyright (c) 2023 schick Informatik // Description: Input control for incoming shipcalls // using BreCalClient.misc.Model; using System; using System.Windows; using static BreCalClient.Extensions; namespace BreCalClient { /// /// Interaction logic for EditTimesAgencyIncomingControl.xaml /// public partial class EditTimesAgencyIncomingControl : Window, IEditTimesControl { #region Fields bool _editing = false; #endregion #region Construction public EditTimesAgencyIncomingControl() { 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.comboBoxArrivalBerth.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)) { 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.datePickerETA.Value != this.Times.EtaBerth) || (this.datePickerETA_End.Value != this.Times.EtaIntervalEnd)) // something has changed { if (datePickerETA.Value.IsTooOld() || datePickerETA_End.Value.IsTooOld()) { message = BreCalClient.Resources.Resources.textETAInThePast; return false; } if (this.datePickerETA.Value.HasValue && this.datePickerETA_End.Value.HasValue && this.datePickerETA.Value > this.datePickerETA_End.Value) { message = BreCalClient.Resources.Resources.textEndValueBeforeStartValue; return false; } } if((this.datePickerTidalWindowFrom.Value != this.ShipcallModel.Shipcall?.TidalWindowFrom) || (this.datePickerTidalWindowTo.Value != this.ShipcallModel.Shipcall?.TidalWindowTo)) // something has changed { if(datePickerTidalWindowTo.Value.IsTooOld() || this.datePickerTidalWindowFrom.Value.IsTooOld()) { 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.datePickerETA.Value.IsTooFar() || this.datePickerETA_End.Value.IsTooFar() || this.datePickerTidalWindowFrom.Value.IsTooFar() || this.datePickerTidalWindowTo.Value.IsTooFar()) { message = BreCalClient.Resources.Resources.textTooFarInTheFuture; return false; } if((this.datePickerETA_End.Value.HasValue && !this.datePickerETA.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.EtaBerth = this.datePickerETA.Value; this.Times.EtaIntervalEnd = this.datePickerETA_End.Value; if (this.comboBoxPierside.SelectedIndex >= 0) { this.ShipcallModel.Shipcall.PierSide = (this.comboBoxPierside.SelectedIndex == 0); } else { this.ShipcallModel.Shipcall.PierSide = null; } this.Times.BerthInfo = this.textBoxBerthRemarks.Text.Trim(); this.Times.BerthId = (int?)this.comboBoxArrivalBerth.SelectedValue; 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.Anchored = this.checkBoxAnchored.IsChecked; this.ShipcallModel.Shipcall.RecommendedTugs = this.integerUpDownRecommendedTugs.Value; this.ShipcallModel.Shipcall.MooredLock = this.checkBoxMooredLock.IsChecked; this.ShipcallModel.Shipcall.Bunkering = this.checkBoxBunkering.IsChecked; this.ShipcallModel.Shipcall.ReplenishingTerminal = this.checkBoxReplenishingTerminal.IsChecked; this.ShipcallModel.Shipcall.ReplenishingLock = this.checkBoxReplenishingLock.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.EtaBerth.HasValue) { this.datePickerETA.Value = this.Times.EtaBerth.Value; } else { // if not set through times use value of BSMD entry if (this.ShipcallModel.Shipcall.Eta != DateTime.MinValue) this.datePickerETA.Value = this.ShipcallModel.Shipcall.Eta; } this.datePickerETA_End.Value = this.Times.EtaIntervalEnd; if (Times.BerthId.HasValue) this.comboBoxArrivalBerth.SelectedValue = Times.BerthId; else if (this.ShipcallModel.Shipcall.ArrivalBerthId.HasValue) this.comboBoxArrivalBerth.SelectedValue = this.ShipcallModel.Shipcall.ArrivalBerthId; 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.checkBoxAnchored.IsChecked = this.ShipcallModel.Shipcall.Anchored ?? false; this.integerUpDownRecommendedTugs.Value = this.ShipcallModel.Shipcall.RecommendedTugs; this.checkBoxMooredLock.IsChecked = this.ShipcallModel.Shipcall.MooredLock ?? false; this.checkBoxBunkering.IsChecked = this.ShipcallModel.Shipcall.Bunkering ?? false; this.checkBoxReplenishingLock.IsChecked = this.ShipcallModel.Shipcall.ReplenishingLock ?? false; this.checkBoxReplenishingTerminal.IsChecked = this.ShipcallModel.Shipcall.ReplenishingTerminal ?? false; if ((this.ShipcallModel.Shipcall.TimeRefPoint ?? 0) == 0) this.labelETA.Content = BreCalClient.Resources.Resources.textETABerth; else this.labelETA.Content = string.Format("ETA {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.datePickerETA.IsEnabled = _editing; this.datePickerETA_End.IsEnabled = _editing; this.comboBoxArrivalBerth.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.checkBoxAnchored.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.checkBoxBunkering.IsEnabled = _editing; this.checkBoxReplenishingTerminal.IsEnabled = _editing; this.checkBoxReplenishingLock.IsEnabled = _editing; this.textBoxRemarks.IsReadOnly = !_editing; CheckOKButton(); } private bool RequiredFieldsSet() { bool areSet = this.datePickerETA.Value.HasValue && this.doubleUpDownDraft.Value.HasValue && this.comboBoxArrivalBerth.SelectedIndex >= 0; if (areSet && this.datePickerETA_End.Value.HasValue) areSet &= (this.datePickerETA.Value < this.datePickerETA_End.Value); return areSet; } private void CheckOKButton() { this.buttonOK.IsEnabled = _editing && RequiredFieldsSet(); } #endregion #region event handlers private void contextMenuItemArrivalBerth_Click(object sender, RoutedEventArgs e) { this.comboBoxArrivalBerth.SelectedIndex = -1; this.ShipcallModel.Berth = ""; } 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 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 doubleUpDownDraft_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { this.CheckOKButton(); } private void datePickerETA_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { this.CheckOKButton(); } private void comboBoxArrivalBerth_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { this.CheckOKButton(); } private void datePickerETA_End_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { this.CheckOKButton(); } #endregion } }