using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Umbraco.Core.Collections;
using Umbraco.Core.Exceptions;
namespace Umbraco.Core.Models
{
///
/// The culture names of a content's variants
///
public class ContentCultureInfosCollection : ObservableDictionary, IDeepCloneable
{
///
/// Initializes a new instance of the class.
///
public ContentCultureInfosCollection()
: base(x => x.Culture, StringComparer.InvariantCultureIgnoreCase)
{ }
///
/// Initializes a new instance of the class with items.
///
public ContentCultureInfosCollection(IEnumerable items)
: base(x => x.Culture, StringComparer.InvariantCultureIgnoreCase)
{
// make sure to add *copies* and not the original items,
// as items can be modified by AddOrUpdate, and therefore
// the new collection would be impacted by changes made
// to the old collection
foreach (var item in items)
Add(new ContentCultureInfos(item));
}
///
/// Adds or updates a instance.
///
public void AddOrUpdate(string culture, string name, DateTime date)
{
if (culture.IsNullOrWhiteSpace()) throw new ArgumentNullOrEmptyException(nameof(culture));
culture = culture.ToLowerInvariant();
if (TryGetValue(culture, out var item))
{
item.Name = name;
item.Date = date;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, item));
}
else
{
Add(new ContentCultureInfos(culture)
{
Name = name,
Date = date
});
}
}
///
public object DeepClone()
{
var clone = new ContentCultureInfosCollection();
foreach (var item in this)
{
var itemClone = (ContentCultureInfos) item.DeepClone();
itemClone.ResetDirtyProperties(false);
clone.Add(itemClone);
}
return clone;
}
}
}