/****************************************************************************
**
** 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 "folderitem.h"

namespace NewsBlur
{
/*!
  \class FolderItem
  \brief The FolderItem class represents a NewsBlur folder, which can only contain
  additional FolderItems or FeedItems.
*/
/*!
  \property FolderItem::unreadCount
  The number of related unread stories with neutral classification.
*/
/*!
  \property FolderItem::posUnreadCount
  The number of related unread stories with positive classification.
*/
/*!
  \property FolderItem::negUnreadCount
  The number of related unread stories with negative classification.
*/
/*!
  \property FolderItem::unreadCountText
  The number of related unread stories in textual representation.
*/

/*!
  Constructs a FolderItem object with the given \a parent and \a service object.
*/
FolderItem::FolderItem(PageItemPtr parent, AbstractFeedAggreagatorService* service) :
    NewsBlurItem(parent, service),
    m_unreadCount(0),
    m_posUnreadCount(0),
    m_negUnreadCount(0)
{
}

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

int FolderItem::unreadCount() const
{
    return m_unreadCount;
}

void FolderItem::setUnreadCount(int unreadCount)
{
    if(unreadCount < 0)
        unreadCount = 0;
    m_unreadCount = unreadCount;
    updateUnreadCountText();
    emit unreadCountChanged();    
    emit dataChanged(FolderItemWeakRef(this));
}
int FolderItem::posUnreadCount() const
{
    return m_posUnreadCount;
}

void FolderItem::setPosUnreadCount(int unreadCount)
{
    if(unreadCount < 0)
        unreadCount = 0;
    m_posUnreadCount = unreadCount;
    updateUnreadCountText();
    emit posUnreadCountChanged();
    emit dataChanged(FolderItemWeakRef(this));
}

int FolderItem::negUnreadCount() const
{
    return m_negUnreadCount;
}

void FolderItem::setNegUnreadCount(int unreadCount)
{
    if(unreadCount < 0)
        unreadCount = 0;
    m_negUnreadCount = unreadCount;
    updateUnreadCountText();
    emit negUnreadCountChanged();
    emit dataChanged(FolderItemWeakRef(this));
}

QString FolderItem::unreadCountText() const
{
    return m_unreadCountText;
}

void FolderItem::setUnreadCountText(const QString &text)
{
    m_unreadCountText = text;
    emit unreadCountTextChanged();
}

/*!
  Updates the FolderItem::unreadCountText property.
*/
void FolderItem::updateUnreadCountText()
{
    QString text;
    QStringList unreadCounts;
    if(negUnreadCount() > 0)
        unreadCounts << QString("<span style=\"color:%1\">%2</span>").arg(NewsBlurEnum::s_negativeHighlightColor).arg(m_negUnreadCount);
    if(unreadCount() > 0)
        unreadCounts << QString("<span style=\"color:%1\">%2</span>").arg(NewsBlurEnum::s_highlightColor).arg(m_unreadCount);
    if(posUnreadCount() > 0)
        unreadCounts << QString("<span style=\"color:%1\">%2</span>").arg(NewsBlurEnum::s_positiveHighlightColor).arg(m_posUnreadCount);
    if(unreadCounts.count() > 0)
        text = QString(tr("%1 unread stories")).arg(unreadCounts.join("+"));
    setUnreadCountText(text);
}
} // namespace NewsBlur
