// 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 = null;
this.datePickerETATo.SelectedDate = null;
this.textBoxSearch.Clear();
this.upDownShiplengthFrom.Value = null;
this.upDownShiplengthTo.Value = null;
}
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)
{
foreach(int category in sfm.Categories)
this.comboBoxCategories.SelectedItems.Add((Extensions.TypeEnum)category);
}
if (sfm.SearchString != null) this.textBoxSearch.Text = sfm.SearchString;
this.upDownShiplengthFrom.Value = sfm.ShipLengthFrom;
this.upDownShiplengthTo.Value = sfm.ShipLengthTo;
this.datePickerETAFrom.SelectedDate = sfm.EtaFrom;
this.datePickerETATo.SelectedDate = sfm.EtaTo;
this._model = sfm;
SearchFilterChanged?.Invoke();
}
#endregion
#region event handler
private void logoImage_MouseUp(object sender, MouseButtonEventArgs e)
{
LogoImageClicked?.Invoke();
}
private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
this.comboBoxCategories.ItemsSource = Enum.GetValues(typeof(Extensions.TypeEnum));
}
private void datePickerETAFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
this._model.EtaFrom = this.datePickerETAFrom.SelectedDate;
SearchFilterChanged?.Invoke();
}
private void datePickerETATo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
this._model.EtaTo = datePickerETATo.SelectedDate;
SearchFilterChanged?.Invoke();
}
private void comboBoxCategories_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
{
_model.Categories.Clear();
foreach(int category in comboBoxCategories.SelectedItems)
_model.Categories.Add(category);
SearchFilterChanged?.Invoke();
}
private void upDownShiplengthFrom_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs