// Copyright (c) 2023 schick Informatik
// Description: Search filter bar for top window, generates events if filter criteria change
//
using BreCalClient.misc.Model;
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
namespace BreCalClient
{
///
/// Interaction logic for SearchFilterControl.xaml
///
public partial class SearchFilterControl : UserControl
{
#region private fields
private SearchFilterModel _model = new();
#endregion
#region Construction
public SearchFilterControl()
{
InitializeComponent();
this.DataContext = this._model;
}
#endregion
#region events
///
/// historically we love a clickable logo and see what will happen
///
public event Action? LogoImageClicked;
///
/// if the user somewhat changes the filters trigger this
///
internal event Action? SearchFilterChanged;
#endregion
#region Properties
public string FilterAsJson { get; set; } = "";
internal SearchFilterModel SearchFilter { get { return _model; } }
#endregion
#region public methods
public void SetBerths(List berths)
{
this.comboBoxBerths.ItemsSource = berths;
}
public void SetAgencies(List agencies)
{
this.comboBoxAgencies.ItemsSource = agencies;
}
public void ClearFilters()
{
this._model = new SearchFilterModel();
this.comboBoxAgencies.UnSelectAll();
this.comboBoxBerths.UnSelectAll();
this.comboBoxCategories.UnSelectAll();
this.datePickerETAFrom.SelectedDate = DateTime.Now.AddDays(-2);
this.datePickerETATo.SelectedDate = null;
this.textBoxSearch.Clear();
this.upDownShiplengthFrom.Value = null;
this.upDownShiplengthTo.Value = null;
this.checkBoxOwn.IsChecked = false;
}
internal void SetFilterFromModel(SearchFilterModel sfm)
{
this.ClearFilters();
if(sfm.Berths != null)
{
foreach(Berth berth in this.comboBoxBerths.ItemsSource)
{
if (sfm.Berths.Contains(berth.Id)) this.comboBoxBerths.SelectedItems.Add(berth);
}
}
if(sfm.Agencies != null)
{
foreach(Participant p in this.comboBoxAgencies.ItemsSource)
{
if(sfm.Agencies.Contains(p.Id)) this.comboBoxAgencies.SelectedItems.Add(p);
}
}
if(sfm.Categories != null)
{
EnumToStringConverter enumToStringConverter = new();
foreach (ShipcallType category in sfm.Categories)
{
this.comboBoxCategories.SelectedItems.Add(enumToStringConverter.Convert(category, typeof(ShipcallControl), new object(), System.Globalization.CultureInfo.CurrentCulture));
}
}
if (sfm.SearchString != null) this.textBoxSearch.Text = sfm.SearchString;
this.upDownShiplengthFrom.Value = sfm.ShipLengthFrom;
this.upDownShiplengthTo.Value = sfm.ShipLengthTo;
if (sfm.EtaFrom != null)
this.datePickerETAFrom.SelectedDate = sfm.EtaFrom;
else
this.datePickerETAFrom.SelectedDate = DateTime.Now.AddDays(-2);
this.datePickerETATo.SelectedDate = sfm.EtaTo;
this.checkBoxOwn.IsChecked = sfm.MineOnly;
this._model = sfm;
SearchFilterChanged?.Invoke();
}
#endregion
#region event handler
private void logoImage_MouseUp(object sender, MouseButtonEventArgs e)
{
LogoImageClicked?.Invoke();
}
private void datePickerETAFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
this._model.EtaFrom = this.datePickerETAFrom.SelectedDate?.Date;
SearchFilterChanged?.Invoke();
}
private void datePickerETATo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
this._model.EtaTo = datePickerETATo.SelectedDate?.Date;
SearchFilterChanged?.Invoke();
}
private void comboBoxCategories_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
{
EnumToStringConverter enumToStringConverter = new();
_model.Categories.Clear();
foreach (string categoryString in comboBoxCategories.SelectedItems)
{
ShipcallType? type = (ShipcallType?)enumToStringConverter.ConvertBack(categoryString, typeof(ShipcallType), new object(), System.Globalization.CultureInfo.CurrentCulture);
if(type != null)
_model.Categories.Add(type.Value);
}
SearchFilterChanged?.Invoke();
}
private void upDownShiplengthFrom_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs