// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2014 Konrad Twardowski

#include "mod.h"

#include "commandline.h"
#include "config.h"
#include "mainwindow.h"
#include "utils.h"

#include <QLoggingCategory>
#include <QMenuBar>
#include <QPushButton>

// public

void Mod::applyMainWindowColors(MainWindow *mainWindow) {
// TODO: document properties
// TODO: layout mod
	QColor windowColor = getColor("ui-window-color", QColor());
	QColor windowText = getColor("ui-window-text", QColor());
	QColor buttonColorActive = getColor("ui-button-color-active", QColor());
	QColor buttonColorInactive = getColor("ui-button-color-inactive", QColor());
	QColor buttonText = QColor();

	QString theme = get("ui-theme", "").toString();
	if (theme == "classic") {
		windowColor = 0xA7DC6B;
		buttonColorActive = 0xDCD86A;
		buttonColorInactive = 0xDC6A6E;
		buttonText = mainWindow->active() ? Qt::black : Qt::white;
	}
	else if (theme == "solarized") {
		windowColor = 0x002B36;
		windowText = 0xEEE8D5;
		buttonColorActive = 0x859900;
		buttonColorInactive = 0xB58900;
	}
	else if (theme == "sky") {
		windowColor = 0xA4C0E4;
		buttonColorActive = 0xD8E8C2;
		buttonColorInactive = 0xFFBFBF;
	}

	QColor buttonColor = mainWindow->active() ? buttonColorActive : buttonColorInactive;
	if (buttonColor.isValid()) {
		QPalette buttonPalette;
		buttonPalette.setColor(QPalette::Button, buttonColor);
		buttonPalette.setColor(QPalette::ButtonText, buttonText.isValid() ? buttonText : getContrastBW(buttonColor));
		mainWindow->okCancelButton()->setPalette(buttonPalette);
	}

	if (windowColor.isValid()) {
		QPalette windowPalette;
		windowPalette.setColor(QPalette::Window, windowColor);
		QColor text = windowText.isValid() ? windowText : getContrastBW(windowColor);
		windowPalette.setColor(QPalette::WindowText, text);
		mainWindow->setPalette(windowPalette);
		
		QPalette palette;
		palette.setColor(QPalette::Button, windowColor);
		palette.setColor(QPalette::ButtonText, text);
		//palette.setColor(QPalette::Base, windowColor);
		//palette.setColor(QPalette::Text, text);
		mainWindow->m_actions->setPalette(palette);
		mainWindow->m_triggers->setPalette(palette);
		mainWindow->menuBar()->setPalette(palette);
	}
}

QVariant Mod::get(const QString &name, const QVariant &defaultValue) {
	return m_map->value(name, defaultValue);
}

bool Mod::getBool(const QString &name, const bool defaultValue) {
	return get(name, defaultValue).toBool();
}

QColor Mod::getColor(const QString &name, const QColor &defaultValue) {
	return get(name, defaultValue).value<QColor>();
}

QString Mod::getString(const QString &name, const QString &defaultValue) {
	return get(name, defaultValue).toString();
}

void Mod::init() {
	m_map = new QHash<QString, QVariant>();

	// read command line
	QString cliMod = CLI::getOption("mod");
	// read config
	QString configMod = Config::readString("Mod", "Value", "");

	QString mod = cliMod; // 1. - add value from command line

	if (!configMod.isEmpty()) { // 2. - add value from config - overrides command line entries
		if (!mod.isEmpty())
			mod += ',';
		mod += configMod;
	}

	// parse and initialize the mod list

	mod = mod.trimmed();
	
	if (mod.isEmpty())
		return;

	QLoggingCategory log("mod");

	qCDebug(log, "Value: %s", KS_S(mod));

	for (const QString &item : mod.split(',')) {
		QString token = item.trimmed();
		//qCDebug(log) << "Token:" << token;
		
		QString name;
		QVariant value;

		auto pair = Utils::splitPair(token, '=', true);
		if (!pair.isEmpty()) {
			name = pair.first();
			value = pair.last();
		}
		else {
			name = token;
			value = true;
		}

		qCDebug(log, "Insert: %s = %s", KS_S(name), KS_S(value.toString()));
		m_map->insert(name, value);
	}
}

// private

QColor Mod::getContrastBW(const QColor &base) {
	return Utils::isDark(base) ? Qt::white : Qt::black;
}
