/***************************************************************************
 *   Copyright 2007-2008 Wouter Haffmans <cheetah@simply-life.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) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   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 "themeloader.h"

#include <KSvgRenderer>
#include <QFile>
#include <KStandardDirs>

#include <QtDebug>

ThemeLoader::ThemeLoader(const QString& name, QObject* parent)
    : QObject(parent)
{
    m_renderer = new KSvgRenderer(this);
    m_lineWidth = -1;
    m_loaded = false;
    m_dirty = m_svgDirty = true;

    if (!name.isEmpty()) {
        loadTheme(name);
    }
}

bool ThemeLoader::loadTheme(const QString& name) {
    QFile file(KStandardDirs::locate("appdata", "themes/" + name + "/kiddraw-buttons.svg"));
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Could not load theme" << name;
        return false;
    }

    QDomDocument newTheme("theme");
    if (!newTheme.setContent(&file)) {
        qDebug() << "Could not load theme" << name;
        file.close();
        return false;
    }
    file.close();

    // Theme SVG loaded
    QDomElement docElem = newTheme.documentElement();
    if (docElem.tagName() != "svg") {
        qDebug() << "Could not load theme" << name;
        return false;
    }
    m_styleElement = newTheme.createElement("style");
    m_styleElement.setAttribute("type", "text/css");

    docElem.insertBefore(m_styleElement, docElem.firstChild());

    m_themeName = name;

    // Save theme document
    m_theme = newTheme;
    m_dirty = true;

    writeStyle();
    m_loaded = true;
    return true;
}

void ThemeLoader::writeStyle() {
    QString content;
    if (colorValue().isValid()) {
        content += ".kiddrawFillValue { fill: " + colorValue().name() + "; }";
        content += ".kiddrawStrokeValue { stroke: " + colorValue().name() + "; }";
    }
    if (borderColor().isValid()) {
        content += ".kiddrawFillBorder { fill: " + borderColor().name() + "; }";
        content += ".kiddrawStrokeBorder { stroke: " + borderColor().name() + "; }";
    }
    if (backgroundColor().isValid()) {
        content += ".kiddrawFillBackground { fill: " + backgroundColor().name() + "; }";
        content += ".kiddrawStrokeBackground { stroke: " + backgroundColor().name() + "; }";
    }
    if (lineWidth() >= 0) {
        content += ".kiddrawStrokeWidth { stroke-width: " + QString::number(lineWidth()) + "; }";
    }

    // Write content
    while (m_styleElement.hasChildNodes()) {
        m_styleElement.removeChild(m_styleElement.firstChild());
    }
    QDomCDATASection dataNode = m_theme.createCDATASection(content);
    m_styleElement.appendChild(dataNode);

    m_svgDirty = true;
}

void ThemeLoader::reloadSvg() {
    m_renderer->load(m_theme.toByteArray(0));
}

QColor ThemeLoader::colorValue() const {
    return m_value;
}

void ThemeLoader::setColorValue(const QColor& color) {
    m_value = color;
    m_dirty = true;
}

QColor ThemeLoader::borderColor() const {
    return m_border;
}

void ThemeLoader::setBorderColor(const QColor& color) {
    m_border = color;
    m_dirty = true;
}

QColor ThemeLoader::backgroundColor() const {
    return m_background;
}

void ThemeLoader::setBackgroundColor(const QColor& color) {
    m_background = color;
    m_dirty = true;
}

int ThemeLoader::lineWidth() const {
    return m_lineWidth;
}

void ThemeLoader::setLineWidth(int width) {
    m_lineWidth = width;
    m_dirty = true;
}


QString ThemeLoader::themeName() const {
    return m_themeName;
}

bool ThemeLoader::isLoaded() {
    return m_loaded;
}

QSizeF ThemeLoader::elementSize(const QString& elementId) {
    if (m_dirty) {
        writeStyle();
    }
    if (m_svgDirty) {
        reloadSvg();
    }

    QRectF r(m_renderer->boundsOnElement(elementId));
    return r.size();
}

void ThemeLoader::render(QPainter* painter, const QString& elementId, const QRectF& bounds) {
    if (!isLoaded()) {
        return;
    }
    if (m_dirty) {
        writeStyle();
    }
    if (m_svgDirty) {
        reloadSvg();
    }

    m_renderer->render(painter, elementId, bounds);
}

#include "themeloader.moc"
