using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.PPC.Common;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.Settings;
namespace Tango.PPC.Jobs.ViewModels
{
///
/// Represents the twine catalog view model.
///
///
///
public class TwineCatalogViewVM : PPCViewModel, INavigationResultProvider
{
private bool _confirmed;
private ObservablesContext _db;
private ColorCatalog _catalog;
///
/// Gets or sets the catalog.
///
public ColorCatalog Catalog
{
get { return _catalog; }
set { _catalog = value; RaisePropertyChangedAuto(); }
}
private ColorCatalog _recent;
///
/// Gets or sets the recent catalog items as a complete catalog.
///
public ColorCatalog Recent
{
get { return _recent; }
set { _recent = value; RaisePropertyChangedAuto(); }
}
private String _filter;
///
/// Gets or sets the filter.
///
///
/// The filter.
///
public String Filter
{
get { return _filter; }
set { _filter = value; RaisePropertyChangedAuto(); }
}
private ColorCatalogsItem _selectedItem;
///
/// Gets or sets the selected item.
///
public ColorCatalogsItem SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value; RaisePropertyChangedAuto();
}
}
///
/// Gets or sets the OK command.
///
public RelayCommand OKCommand { get; set; }
///
/// Initializes a new instance of the class.
///
public TwineCatalogViewVM()
{
OKCommand = new RelayCommand(Confirm);
}
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
_db = ObservablesContext.CreateDefault();
}
///
/// Called when the navigation system has navigated to this VM view.
///
public override void OnNavigatedTo()
{
base.OnNavigatedTo();
_confirmed = false;
Filter = "CATALOG";
}
///
/// Confirms this instance.
///
private void Confirm()
{
if (SelectedItem != null)
{
var settings = SettingsManager.Default.GetOrCreate();
settings.AddRecentCatalogItem(Catalog, SelectedItem);
SettingsManager.Default.Save();
}
_confirmed = true;
NavigationManager.NavigateBack();
}
///
/// Called when the navigation system requests a result when it is navigating away from this instance.
///
///
public ColorCatalogsItem GetNavigationResult()
{
if (_confirmed)
{
return SelectedItem;
}
else
{
return null;
}
}
///
/// Called when the navigation object has been received
///
/// The brush stop.
public virtual void OnNavigationObjectReceived(TwineCatalogNavigationObject obj)
{
IsFree = false;
Filter = "CATALOG";
Catalog = obj.Catalog;
ColorCatalog recentCatalog = new ColorCatalog();
recentCatalog.Name = Catalog.Name;
var settings = SettingsManager.Default.GetOrCreate();
var settingsCatalog = settings.RecentCatalogsItems.SingleOrDefault(x => x.Guid == Catalog.Guid);
if (settingsCatalog != null)
{
var allItems = Catalog.ColorCatalogsGroups.SelectMany(x => x.ColorCatalogsItems).ToList();
foreach (var itemGuid in settingsCatalog.RecentItems)
{
var realItem = allItems.SingleOrDefault(x => x.Guid == itemGuid);
if (realItem != null)
{
var group = recentCatalog.ColorCatalogsGroups.SingleOrDefault(x => x.Guid == realItem.ColorCatalogsGroup.Guid);
if (group == null)
{
group = new ColorCatalogsGroup();
group.Guid = realItem.ColorCatalogsGroup.Guid;
group.Name = realItem.ColorCatalogsGroup.Name;
group.GroupIndex = realItem.ColorCatalogsGroup.GroupIndex;
recentCatalog.ColorCatalogsGroups.Add(group);
}
group.ColorCatalogsItems.Add(realItem);
}
}
}
Recent = recentCatalog;
SelectedItem = obj.SelectedItem;
IsFree = true;
}
}
}