using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace PocketSharp.Models
{
///
/// Item containing all available data
/// see: http://getpocket.com/developer/docs/v3/retrieve
///
[JsonObject]
[DebuggerDisplay("Uri = {Uri}, Title = {Title}")]
public class PocketItem : IComparable
{
///
/// Gets or sets the ID.
///
///
/// The ID.
///
[JsonProperty("item_id")]
public string ID { get; set; }
///
/// Gets or sets the resolved identifier.
///
///
/// The resolved identifier.
///
[JsonProperty("resolved_id")]
public string ResolvedId { get; set; }
///
/// Gets or sets the normal URI.
///
///
/// The normal URI.
///
[JsonProperty("normal_url")]
private Uri _NormalUri { get; set; }
///
/// Gets or sets the given URI.
///
///
/// The given URI.
///
[JsonProperty("given_url")]
private Uri _GivenUri { get; set; }
///
/// Gets or sets the resolved URI.
///
///
/// The resolved URI.
///
[JsonProperty("resolved_url")]
private Uri _ResolvedUri { get; set; }
///
/// Gets or sets the URI.
///
///
/// The URI.
///
[JsonIgnore]
public Uri Uri
{
get { return _ResolvedUri ?? _GivenUri ?? _NormalUri; }
set { _NormalUri = value; _ResolvedUri = value; _GivenUri = value; }
}
///
/// Gets or sets the title.
///
///
/// The title.
///
[JsonProperty("resolved_title")]
private string _ResolvedTitle { get; set; }
///
/// Gets or sets the title.
///
///
/// The title.
///
[JsonProperty("title")]
private string _InternalTitle { get; set; }
///
/// Gets or sets the title.
///
///
/// The title.
///
[JsonIgnore]
public string Title
{
get { return _InternalTitle ?? _ResolvedTitle ?? FullTitle; }
set { _InternalTitle = value; _ResolvedTitle = value; }
}
///
/// Gets or sets the full title.
///
///
/// The full title.
///
[JsonProperty("given_title")]
public string FullTitle { get; set; }
///
/// Gets or sets the excerpt.
///
///
/// The excerpt.
///
[JsonProperty]
public string Excerpt { get; set; }
///
/// Gets or sets the status.
///
///
/// The status.
///
[JsonProperty]
public int Status { get; set; }
///
/// Gets or sets a value indicating whether this instance is favorite.
///
///
/// true if this instance is favorite; otherwise, false.
///
[JsonProperty("favorite")]
public bool IsFavorite { get; set; }
///
/// Gets a value indicating whether this instance is archive.
///
///
/// true if this instance is archive; otherwise, false.
///
[JsonIgnore]
public bool IsArchive { get { return Status == 1; } }
///
/// Gets a value indicating whether this instance is deleted.
///
///
/// true if this instance is deleted; otherwise, false.
///
[JsonIgnore]
public bool IsDeleted { get { return Status == 2; } }
///
/// Gets or sets a value indicating whether this instance is article.
///
///
/// true if this instance is article; otherwise, false.
///
[JsonProperty("is_article")]
public bool IsArticle { get; set; }
///
/// Gets or sets a value indicating whether this instance has image.
///
///
/// true if this instance has image; otherwise, false.
///
[JsonProperty("has_image")]
private PocketBoolean? _HasImage { get; set; }
///
/// Gets or sets a value indicating whether this instance has video.
///
///
/// true if this instance has video; otherwise, false.
///
[JsonProperty("has_video")]
private PocketBoolean? _HasVideo { get; set; }
///
/// Gets a value indicating whether [has image].
///
///
/// true if [has image]; otherwise, false.
///
[JsonIgnore]
public bool HasImage
{
get
{
return _HasImage == PocketBoolean.Yes || _HasImage == PocketBoolean.IsType;
}
}
///
/// Gets a value indicating whether [has video].
///
///
/// true if [has video]; otherwise, false.
///
[JsonIgnore]
public bool HasVideo
{
get
{
return _HasVideo == PocketBoolean.Yes || _HasVideo == PocketBoolean.IsType;
}
}
///
/// Gets a value indicating whether [is video].
///
///
/// true if [is video]; otherwise, false.
///
[JsonIgnore]
public bool IsVideo
{
get
{
return _HasVideo == PocketBoolean.IsType;
}
}
///
/// Gets a value indicating whether [is image].
///
///
/// true if [is image]; otherwise, false.
///
[JsonIgnore]
public bool IsImage
{
get
{
return _HasImage == PocketBoolean.IsType;
}
}
///
/// Gets or sets the word count.
///
///
/// The word count.
///
[JsonProperty("word_count")]
public int WordCount { get; set; }
///
/// Gets or sets the sort.
///
///
/// The sort.
///
[JsonProperty("sort_id")]
public int Sort { get; set; }
///
/// Gets or sets the add time.
///
///
/// The add time.
///
[JsonProperty("time_added")]
public DateTime? AddTime { get; set; }
///
/// Gets or sets the update time.
///
///
/// The update time.
///
[JsonProperty("time_updated")]
public DateTime? UpdateTime { get; set; }
///
/// Gets or sets the read time.
///
///
/// The read time.
///
[JsonProperty("time_read")]
public DateTime? ReadTime { get; set; }
///
/// Gets or sets the favorite time.
///
///
/// The favorite time.
///
[JsonProperty("time_favorited")]
public DateTime? FavoriteTime { get; set; }
///
/// Gets or sets the published time.
///
///
/// The time when the article was published.
///
[JsonProperty("date_published")]
public DateTime? PublishedTime { get; set; }
///
/// Gets or sets the tags as comma-separated strings.
///
///
/// The tags.
///
[JsonIgnore]
public string TagsString
{
get { return Tags != null ? String.Join(",", Tags.Select(tag => tag.Name)) : ""; }
}
///
/// Gets or sets the tags.
///
///
/// The tags.
///
[JsonProperty("tags")]
[JsonConverter(typeof(ObjectToArrayConverter))]
public IEnumerable Tags { get; set; }
///
/// Gets or sets the images.
///
///
/// The images.
///
[JsonProperty("images")]
[JsonConverter(typeof(ObjectToArrayConverter))]
public IEnumerable Images { get; set; }
///
/// Gets or sets the videos.
///
///
/// The videos.
///
[JsonProperty("videos")]
[JsonConverter(typeof(ObjectToArrayConverter))]
public IEnumerable Videos { get; set; }
///
/// Gets or sets the authors.
///
///
/// The authors.
///
[JsonProperty("authors")]
[JsonConverter(typeof(ObjectToArrayConverter))]
public IEnumerable Authors { get; set; }
///
/// Gets or sets a value indicating whether this instance is trending.
///
///
/// true if this instance is trending; otherwise, false.
///
[JsonProperty("trending")]
public bool IsTrending { get; set; }
///
/// Gets the lead image.
///
///
/// The lead image.
///
[JsonIgnore]
public PocketImage LeadImage
{
get { return Images != null && Images.Count() > 0 ? Images.First() : null; }
}
///
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
///
/// An object to compare with this instance.
///
/// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order.
///
int IComparable.CompareTo(object obj)
{
PocketItem item = (PocketItem)obj;
if (!AddTime.HasValue)
{
return 1;
}
if (!item.AddTime.HasValue)
{
return -1;
}
return DateTime.Compare(AddTime.Value, item.AddTime.Value);
}
///
/// Determines whether the specified , is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
PocketItem item = obj as PocketItem;
if (item == null)
{
return false;
}
return ID == item.ID;
}
///
/// Implements the operator ==.
///
/// A.
/// The b.
///
/// The result of the operator.
///
public static bool operator ==(PocketItem a, PocketItem b)
{
if (Object.ReferenceEquals(a, b))
{
return true;
}
PocketItem itemA = (PocketItem)a;
PocketItem itemB = (PocketItem)b;
if ((Object)itemA == null || (Object)itemB == null)
{
return false;
}
return itemA.ID == itemB.ID;
}
///
/// Implements the operator !=.
///
/// A.
/// The b.
///
/// The result of the operator.
///
public static bool operator !=(PocketItem a, PocketItem b)
{
return !(a == b);
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
return ID.GetHashCode();
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return ID;
}
}
}