/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#include "storyitem.h"

namespace NewsBlur
{
/*!
  \class StoryItem
  \brief The StoryItem class represents a story of a NewsBlur feed.
*/
/*!
  \property StoryItem::isRead
  True if the story is marked as read,
  otherwise returns false.
*/
/*!
  \property StoryItem::contents
  The contents of the story. It's usually HTML formatted rich text.
*/
/*!
  \property StoryItem::url
  The url of the story.
*/
/*!
  \property StoryItem::starred
  True if the story is starred or saved,
  otherwise returns false.
*/
/*!
  \property StoryItem::starredBusy
  True if the operation of starring the story is in progress,
  otherwise returns false.
*/
/*!
  \property StoryItem::date
  The publishing date of the story in UTC form.
*/
/*!
  \property StoryItem::formattedDate
  The textual representation of the story's publishing date,
  in the application local time zone.
*/
/*!
  \property StoryItem::formattedDateTime
  The textual representation of the story's publishing date and time,
  in the application local time zone.
*/
/*!
  \property StoryItem::feedId
  Returns the identifier of the story's related feed.
*/
/*!
  \property StoryItem::tags
  The list of tags associated with the story.
*/
/*!
  \property StoryItem::author
  The author of the story.
*/
/*!
  \property StoryItem::itemClassification
  The current classification of the story.
*/

/*!
  Constructs a StoryItem object with the given \a parent and \a service object.
*/
StoryItem::StoryItem(PageItemPtr parent, AbstractFeedAggreagatorService* service) :
    NewsBlurItem(parent, service),
    m_read(true),
    m_starred(false),
    m_starredBusy(false),
    m_feedId(0)
{
}

/*!
  Reimplemented from NewsBlurItem::itemType().
*/
NewsBlurItem::ItemType StoryItem::itemType() const
{
    return NewsBlurItem::StoryItem;
}

bool StoryItem::isRead() const
{
    return m_read;
}

void StoryItem::setRead(bool read)
{
    m_read = read;
    emit readChanged();
    emit dataChanged(StoryItemWeakRef(this));
}

/*!
  Sets the read status of the story item without
  emitting the StoryItem::dataChanged signal.
  It's useful for setting the read status of multiple
  stories at once without emitting the StoryItem::dataChanged
  signal for every single story.
*/
void StoryItem::setReadWithoutNotify(bool read)
{
    m_read = read;
    emit readChanged();
}

QString StoryItem::contents() const
{
    return m_contents;
}

void StoryItem::setContents(const QString &contents)
{
    m_contents = contents;
    emit contentsChanged();
}

QString StoryItem::url() const
{
    return m_url;
}

void StoryItem::setUrl(const QString &url)
{
    m_url = url;
    emit urlChanged();
}

bool StoryItem::isStarred() const
{
    return m_starred;
}

void StoryItem::setStarred(bool starred)
{
    m_starred = starred;
    emit starredChanged();
    emit dataChanged(StoryItemWeakRef(this));
}

bool StoryItem::isStarredBusy() const
{
    return m_starredBusy;
}

void StoryItem::setStarredBusy(bool busy)
{
    m_starredBusy = busy;
    emit starredBusyChanged();
}

QDateTime StoryItem::date() const
{
    return m_date;
}

QString StoryItem::formattedDate() const
{
    return m_fromattedDate;
}

QString StoryItem::formattedDateTime() const
{
    return m_formattedDateTime;
}

void StoryItem::setDate(const QDateTime &date)
{
    m_date = date;
    if(!m_fromattedDate.length() && !m_date.isNull())
    {
        QDateTime localDate = m_date.toLocalTime();
        if(localDate.date() == QDate::currentDate())
            m_fromattedDate = localDate.time().toString(Qt::DefaultLocaleShortDate);
        else
            m_fromattedDate = localDate.date().toString(Qt::DefaultLocaleShortDate);
        m_formattedDateTime = localDate.toString(Qt::DefaultLocaleShortDate);
    }
    else
    {
        m_formattedDateTime.clear();
        m_fromattedDate.clear();
    }
}

int StoryItem::feedId() const
{
    return m_feedId;
}

void StoryItem::setFeedId(int feedId)
{
    m_feedId = feedId;
}

int StoryItem::itemClassification() const
{
    return m_itemClassifier;
}

void StoryItem::setItemClassification(int classification)
{
    m_itemClassifier = NewsBlurEnum::ClassifierValue(classification);
    emit itemClassificationChanged();
    emit dataChanged(StoryItemWeakRef(this));
}


QStringList StoryItem::tags() const
{
    return m_tags;
}

void StoryItem::setTags(const QStringList &tags)
{
    m_tags = tags;
}

QString StoryItem::author() const
{
    return m_author;
}

void StoryItem::setAuthor(const QString &author)
{
    m_author = author;
}
} // namespace NewsBlur
