2015-06-04 13:15:54 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
2015-06-24 14:57:16 -04:00
|
|
|
|
using Filtration.ObjectModel.BlockItemBaseTypes;
|
|
|
|
|
using Filtration.ObjectModel.Enums;
|
2015-06-04 13:15:54 -04:00
|
|
|
|
|
2015-06-24 14:57:16 -04:00
|
|
|
|
namespace Filtration.ObjectModel
|
2015-06-04 13:15:54 -04:00
|
|
|
|
{
|
2015-06-24 14:57:16 -04:00
|
|
|
|
public class ItemFilterBlock
|
2015-06-04 13:15:54 -04:00
|
|
|
|
{
|
2015-06-11 17:01:52 -04:00
|
|
|
|
private ItemFilterBlockGroup _blockGroup;
|
|
|
|
|
|
2015-06-08 15:17:34 -04:00
|
|
|
|
public ItemFilterBlock()
|
2015-06-04 13:15:54 -04:00
|
|
|
|
{
|
2015-06-08 15:17:34 -04:00
|
|
|
|
BlockItems = new ObservableCollection<IItemFilterBlockItem> {new ActionBlockItem(BlockAction.Show)};
|
2015-07-06 13:19:05 -04:00
|
|
|
|
Enabled = true;
|
2015-06-04 13:15:54 -04:00
|
|
|
|
}
|
2015-07-06 13:19:05 -04:00
|
|
|
|
|
|
|
|
|
public bool Enabled { get; set; }
|
2015-06-04 13:15:54 -04:00
|
|
|
|
public string Description { get; set; }
|
2015-06-11 17:01:52 -04:00
|
|
|
|
|
|
|
|
|
public ItemFilterBlockGroup BlockGroup
|
|
|
|
|
{
|
|
|
|
|
get { return _blockGroup; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var oldBlockGroup = _blockGroup;
|
|
|
|
|
_blockGroup = value;
|
|
|
|
|
|
|
|
|
|
if (_blockGroup != null)
|
|
|
|
|
{
|
|
|
|
|
_blockGroup.BlockGroupStatusChanged += OnBlockGroupStatusChanged;
|
|
|
|
|
if (oldBlockGroup != null)
|
|
|
|
|
{
|
|
|
|
|
oldBlockGroup.BlockGroupStatusChanged -= OnBlockGroupStatusChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (oldBlockGroup != null)
|
|
|
|
|
{
|
|
|
|
|
oldBlockGroup.BlockGroupStatusChanged -= OnBlockGroupStatusChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 13:15:54 -04:00
|
|
|
|
|
|
|
|
|
public BlockAction Action
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var actionBlock = BlockItems.OfType<ActionBlockItem>().First();
|
|
|
|
|
return actionBlock.Action;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var actionBlock = BlockItems.OfType<ActionBlockItem>().First();
|
|
|
|
|
actionBlock.Action = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 15:17:15 -05:00
|
|
|
|
public ObservableCollection<IItemFilterBlockItem> BlockItems { get; }
|
2015-06-04 13:15:54 -04:00
|
|
|
|
|
|
|
|
|
public int BlockCount(Type type)
|
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
return BlockItems?.Count(b => b.GetType() == type) ?? 0;
|
2015-06-04 13:15:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddBlockItemAllowed(Type type)
|
|
|
|
|
{
|
2015-06-08 15:17:34 -04:00
|
|
|
|
var blockItem = (IItemFilterBlockItem)Activator.CreateInstance(type);
|
2015-06-04 13:15:54 -04:00
|
|
|
|
return BlockCount(type) < blockItem.MaximumAllowed;
|
|
|
|
|
}
|
2015-06-06 15:44:38 -04:00
|
|
|
|
|
|
|
|
|
public bool HasBlockItemOfType<T>()
|
|
|
|
|
{
|
|
|
|
|
return BlockItems.Count(b => b is T) > 0;
|
|
|
|
|
}
|
2015-06-11 17:01:52 -04:00
|
|
|
|
|
2015-06-14 11:50:08 -04:00
|
|
|
|
public bool HasBlockGroupInParentHierarchy(ItemFilterBlockGroup targetBlockGroup, ItemFilterBlockGroup startingBlockGroup)
|
|
|
|
|
{
|
|
|
|
|
if (startingBlockGroup == targetBlockGroup)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (BlockGroup == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-19 18:02:32 -05:00
|
|
|
|
return startingBlockGroup.ParentGroup != null && HasBlockGroupInParentHierarchy(targetBlockGroup, startingBlockGroup.ParentGroup);
|
2015-06-14 11:50:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 17:01:52 -04:00
|
|
|
|
private void OnBlockGroupStatusChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (BlockGroup.IsChecked == false && Action == BlockAction.Show)
|
|
|
|
|
{
|
|
|
|
|
Action = BlockAction.Hide;
|
|
|
|
|
}
|
2015-06-24 14:57:16 -04:00
|
|
|
|
else if (BlockGroup.IsChecked && Action == BlockAction.Hide)
|
2015-06-11 17:01:52 -04:00
|
|
|
|
{
|
|
|
|
|
Action = BlockAction.Show;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 13:15:54 -04:00
|
|
|
|
}
|
|
|
|
|
}
|