using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.IO;
namespace Tango.Core.IO
{
///
/// Represents a temporary item base class.
///
///
public abstract class TemporaryItem : ITemporaryItem
{
///
/// Occurs when this item has been deleted.
///
public event EventHandler Deleted;
///
/// Gets the creation date of the temporary item.
///
public DateTime Date { get; protected set; }
///
/// Gets the path to the temporary item.
///
public string Path { get; protected set; }
///
/// Gets the optional item tag.
///
public Object Tag { get; protected set; }
///
/// Gets or sets a value indicating whether this should not be deleted by a parent item.
///
public bool Persist { get; set; }
///
/// Gets the collection of child items.
///
public IReadOnlyCollection Items { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The temporary item path.
/// The item tag.
public TemporaryItem(String path, Object tag)
{
Items = new ReadOnlyCollection(new List());
Date = DateTime.Now;
Path = path;
Tag = tag;
}
///
/// Initializes a new instance of the class.
///
/// The temporary item path.
public TemporaryItem(String path) : this(path, null)
{
}
///
/// Adds a child item.
///
/// The item.
public ITemporaryItem AddItem(ITemporaryItem item)
{
item.Deleted += Item_Deleted;
var list = Items.ToList();
list.Add(item);
Items = list.ToReadOnlyCollection();
return item;
}
///
/// Removes the specified child item.
///
public void RemoveItem(ITemporaryItem item)
{
item.Deleted -= Item_Deleted;
var list = Items.ToList();
list.Remove(item);
Items = list.ToReadOnlyCollection();
}
///
/// Handles the Deleted event of the child Item.
///
/// The source of the event.
/// The instance containing the event data.
private void Item_Deleted(object sender, EventArgs e)
{
RemoveItem(sender as ITemporaryItem);
}
///
/// Deletes the temporary item.
///
///
/// True is deletion was successful.
///
public virtual bool Delete()
{
List remained = new List();
var items = Items;
foreach (var item in items.Where(x => !x.Persist))
{
if (!item.Delete())
{
remained.Add(item);
}
else
{
Deleted?.Invoke(this, new EventArgs());
}
}
Items = new ReadOnlyCollection(remained);
return remained.Count == 0;
}
///
/// Returns true if the item exists.
///
///
public abstract bool Exists();
///
/// Deletes the temporary item asynchronously.
///
///
/// True is deletion was successful.
///
public Task DeleteAsync()
{
return Task.Factory.StartNew(() => Delete());
}
///
/// Makes the temporary item visible to the user somehow.
///
public abstract void Display();
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return Path;
}
///
/// Performs an explicit conversion from to .
///
/// The item.
///
/// The result of the conversion.
///
public static implicit operator String(TemporaryItem item)
{
return item.Path;
}
}
}