/*
 * Copyright 2010  Jonas Emanuel Müller <zanoi@zanoi.net>
 *
 * This program 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 2 of 
 * the License, or (at your option) any later version.

 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "episodeviewdelegate.h"
#include "episodemodel.h"

#include <QObject>
#include <QPainter>
#include <QPoint>
#include <QAbstractItemModel>
#include <QStyleOptionProgressBar>
#include <QApplication>
#include <QDateTime>

#include <kfileitem.h>

namespace Videocatcher {

EpisodeViewDelegate::EpisodeViewDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

EpisodeViewDelegate::~EpisodeViewDelegate()
{
}

void EpisodeViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    if(index.column() == Episode_Status) {
        int status = index.data().toInt();
        if(status == Episode_Status_Not_Downloaded) {
            painter->save();
            if(option.state & QStyle::State_Selected) {
                painter->fillRect(option.rect, option.palette.highlight());
                painter->setBrush(option.palette.highlightedText());
            }

            QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter, QApplication::palette(),  option.state & QStyle::State_Enabled, QString(""));
            painter->restore();
        } else if (status == Episode_Status_Downloaded) {
            painter->save();
            if(option.state & QStyle::State_Selected) {
                painter->fillRect(option.rect, option.palette.highlight());
                painter->setBrush(option.palette.highlightedText());
            }

            QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter, QApplication::palette(), option.state & QStyle::State_Enabled, QString("downloaded"));
            painter->restore();
        } else if (status == Episode_Status_Unavailable) {
            painter->save();
            if(option.state & QStyle::State_Selected) {
                painter->fillRect(option.rect, option.palette.highlight());
                painter->setBrush(option.palette.highlightedText());
            }

            QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter, QApplication::palette(), option.state & QStyle::State_Enabled, QString("not available"));
            painter->restore();
        }else if (status == Episode_Status_Paused) {
            //this is based on the Qt torrent example
            QStyleOptionProgressBar progressBarOption;
            progressBarOption.state = QStyle::State_Enabled;
            progressBarOption.direction = QApplication::layoutDirection();
            progressBarOption.rect = option.rect;
            progressBarOption.fontMetrics = QApplication::fontMetrics();
            progressBarOption.minimum = 0;
            progressBarOption.maximum = 100;
            progressBarOption.textAlignment = Qt::AlignCenter;
            progressBarOption.textVisible = true;
              
            painter->save();
            if(option.state & QStyle::State_Selected) {
                painter->fillRect(option.rect, option.palette.highlight());
                painter->setBrush(option.palette.highlightedText());
            }
                
            int progress = index.data().toInt();
                  
            progressBarOption.progress = progress < 0 ? 0 : progress;
            progressBarOption.text = QString("Paused");
                    
            QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
            painter->restore();            
        } else if (status >= 0) { //downloading
            //this is based on the Qt torrent example
            QStyleOptionProgressBar progressBarOption;
            progressBarOption.state = QStyle::State_Enabled;
            progressBarOption.direction = QApplication::layoutDirection();
            progressBarOption.rect = option.rect;
            progressBarOption.fontMetrics = QApplication::fontMetrics();
            progressBarOption.minimum = 0;
            progressBarOption.maximum = 100;
            progressBarOption.textAlignment = Qt::AlignCenter;
            progressBarOption.textVisible = true;
              
            painter->save();
            if(option.state & QStyle::State_Selected) {
                painter->fillRect(option.rect, option.palette.highlight());
                painter->setBrush(option.palette.highlightedText());
            }
                
            int progress = index.data().toInt();
                    
            progressBarOption.progress = progress < 0 ? 0 : progress;
            progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);
                    
            QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
            painter->restore();
        }
    } else if (index.column() == Episode_Video_Size) {
        painter->save();
        if(option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());
            painter->setBrush(option.palette.highlightedText());
        }
            
        QString text = KIO::convertSize(index.data().toDouble());

        QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter, QApplication::palette(), option.state & QStyle::State_Enabled, text);
        painter->restore();
    } else if (index.column() == Episode_Date_Published) {
        painter->save();
        if(option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());
            painter->setBrush(option.palette.highlightedText());
        }
            
        double date_published = index.data().toDouble();
            
        QString text;
        if(date_published == 0) {
            text = "unavailable";
        } else {
            QDateTime dt;
            dt.setTime_t(date_published);
            text = dt.toString(Qt::DefaultLocaleShortDate);
        }

        QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter, QApplication::palette(), option.state & QStyle::State_Enabled, text);
        painter->restore();
    }
}

} //namespace Videocatcher
