using System.Collections.Generic;
using System.Linq;
using TINK.Model;
using TINK.Model.Connector;
using Xamarin.Forms;
namespace TINK.ViewModel.Map
{
/// Old name: MapPageFilter.
public class TinkKonradToggleViewModel : ITinkKonradToggleViewModel
{
/// Constructs a map page filter view model object from values.
/// Filters and options dictionary.
public TinkKonradToggleViewModel(IGroupFilterMapPage currentFilter)
{
FilterDictionary = currentFilter ?? new GroupFilterMapPage();
}
/// Gets the filter values.
public IGroupFilterMapPage FilterDictionary { get; }
/// Gets the activated filter.
public string CurrentFilter {
get
{
return FilterDictionary.FirstOrDefault(x => x.Value == FilterState.On).Key ?? string.Empty;
}
}
/// Gets value whether TINK is enabled or not.
public bool IsTinkEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter != FilterHelper.FILTERTINKGENERAL;
/// Gets color of the TINK button.
public Color TinkColor => CurrentFilter == FilterHelper.FILTERTINKGENERAL ? Color.Blue : Color.Gray;
/// Gets value whether Konrad is enabled or not.
public bool IsKonradEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter != FilterHelper.FILTERKONRAD;
/// Gets color of the Konrad button.
public Color KonradColor => CurrentFilter == FilterHelper.FILTERKONRAD ? Color.Red : Color.Gray;
/// Gets whether toggle functionality is visible or not.
public bool IsToggleVisible =>
FilterDictionary.ContainsKey(FilterHelper.FILTERKONRAD)
&& FilterDictionary.ContainsKey(FilterHelper.FILTERTINKGENERAL)
&& (IsTinkEnabled || IsKonradEnabled);
///
/// Toggles from bike group TINK to Konrad or vice versa.
/// Toggling means one of
/// - TINK => Konrad or
/// - TINK => Konrad.
///
/// Filter set to toggle.
///
public TinkKonradToggleViewModel DoToggle()
{
var l_oCurrentFilterSet = FilterDictionary.ToArray();
if (l_oCurrentFilterSet.Length < 2)
{
// There is nothing to toggle because filter set contains only one element.
return new TinkKonradToggleViewModel(FilterDictionary);
}
var l_oCurrentState = l_oCurrentFilterSet[l_oCurrentFilterSet.Length - 1].Value == FilterState.On
? FilterState.On
: FilterState.Off;
var l_oToggledFilterSet = new Dictionary();
foreach (var l_oFilterElement in l_oCurrentFilterSet)
{
l_oToggledFilterSet.Add(l_oFilterElement.Key, l_oCurrentState);
l_oCurrentState = l_oFilterElement.Value;
}
return new TinkKonradToggleViewModel(new GroupFilterMapPage(l_oToggledFilterSet));
}
}
}