/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2015 Ricardo Villalba <rvm@users.sourceforge.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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "images.h"
#include <QFile>
#include <QDebug>
#include <QResource>

//#ifdef SMCODE
#include "global.h"
#include "preferences.h"
#include "paths.h"
using namespace Global;
//#endif

QString Images::current_theme;
QString Images::themes_path;

bool Images::is_internal = false;

//#ifdef USE_RESOURCES
QString Images::last_resource_loaded;
bool Images::has_rcc = false;

QString Images::resourceFilename() {
    QString filename = QString::null;

    if ((!themes_path.isEmpty()) && (!current_theme.isEmpty())) {
        filename = themes_path +"/"+ current_theme +"/"+ current_theme +".rcc";
    }

    qDebug() << "Images::resourceFilename:" << filename;

    return filename;
}
//#endif


void Images::setTheme(const QString & name) {
    /*current_theme = name;

    is_internal = (QFile::exists(":/" + current_theme + "/style.qss"));
    if (is_internal) {
        qDebug() << "Images::setTheme:" << current_theme << "is an internal theme";
        setThemesPath("");
        #ifdef USE_RESOURCES
        has_rcc = false;
        #endif
        return;
    }

//#ifdef SMCODE
    QString dir = Paths::configPath() + "/themes/" + name;
    if (QFile::exists(dir)) {
        setThemesPath(Paths::configPath() + "/themes/");
    } else {
        setThemesPath(Paths::themesPath());
    }
//#endif

//#ifdef USE_RESOURCES
    if (!last_resource_loaded.isEmpty()) {
        qDebug() << "Images::setTheme: unloading" << last_resource_loaded;
        QResource::unregisterResource(last_resource_loaded);
        last_resource_loaded = QString::null;
    }

    QString rs_file = resourceFilename();
    if ((!rs_file.isEmpty()) && (QFile::exists(rs_file))) {
        qDebug() << "Images::setTheme: loading" << rs_file;
        QResource::registerResource(rs_file);
        last_resource_loaded = rs_file;
        has_rcc = true;
    } else {
        has_rcc = false;
    }
    qDebug() << "Images::setTheme: has_rcc:" << has_rcc;
//#endif*/
}

void Images::setThemesPath(const QString & folder) {
    themes_path = folder;
    qDebug() << "Images::setThemesPath:" << themes_path;
}

QString Images::file(const QString & name) {
    QString icon_name;
    icon_name = ":/res/" + name + ".png";
    return icon_name;
}


QPixmap Images::icon(QString name, int size) {
    QString icon_name = file(name);
    QPixmap p(icon_name);

    if (!p.isNull()) {
        if (size != -1) {
            p = resize(&p, size);
        }
    }

    return p;
}

QPixmap Images::resize(QPixmap *p, int size) {
	return QPixmap::fromImage( (*p).toImage().scaled(size,size,Qt::IgnoreAspectRatio,Qt::SmoothTransformation) );
}

QPixmap Images::flip(QPixmap *p) {
	return QPixmap::fromImage( (*p).toImage().mirrored(true, false) );
}

QPixmap Images::flippedIcon(QString name, int size) {
	QPixmap p = icon(name, size);
	p = flip(&p);
	return p;
}
