/*
 * Copyright 2010  Omar Plummer <rkoder@gmail.com>
 *           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 "episodeinformationwidget.h"

#include "informationlistwidget.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QTextLayout>
#include <QTextLine>

#include <kseparator.h>
#include <kdialog.h>
#include <kfileitem.h>

struct EpisodeInformationWidget::EpisodeInformationWidgetPrivate
{
    EpisodeInformationWidgetPrivate()
    {
    }
    
    ~EpisodeInformationWidgetPrivate()
    {
    }
    
    QLabel *m_nameLabel;
    InformationListWidget *m_informationListWidget;
};

EpisodeInformationWidget::EpisodeInformationWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f), d(new EpisodeInformationWidgetPrivate)
{
    d->m_nameLabel = new QLabel(this);
    QFont font = d->m_nameLabel->font();
    font.setBold(true);
    d->m_nameLabel->setFont(font);
    d->m_nameLabel->setAlignment(Qt::AlignHCenter);
    d->m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setSpacing(KDialog::spacingHint());

    d->m_informationListWidget = new InformationListWidget(this);

    layout->addWidget(d->m_nameLabel);
    layout->addWidget(new KSeparator());
    layout->addWidget(d->m_informationListWidget);
    layout->addStretch();
}

EpisodeInformationWidget::~EpisodeInformationWidget()
{
    delete d;
}

void EpisodeInformationWidget::setTitle(const QString &title)
{
    QTextOption textOption;
    textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);

    QTextLayout textLayout(title);
    textLayout.setFont(d->m_nameLabel->font());
    textLayout.setTextOption(textOption);

    QString wrappedText;
    wrappedText.reserve(title.length());

    // wrap the text to fit into the width of m_nameLabel
    textLayout.beginLayout();
    QTextLine line = textLayout.createLine();
    while (line.isValid()) {
        line.setLineWidth(d->m_nameLabel->width());
        wrappedText += title.mid(line.textStart(), line.textLength());

        line = textLayout.createLine();
        if (line.isValid()) {
            wrappedText += QChar::LineSeparator;
        }
    }
    textLayout.endLayout();

    d->m_nameLabel->setText(wrappedText);    
}

void EpisodeInformationWidget::clear()
{
    d->m_nameLabel->setText("");
    d->m_informationListWidget->clear();
}

void EpisodeInformationWidget::setAttribute ( const QString& label, const QString& value )
{
    d->m_informationListWidget->addRow(label, value);
}
