/*
 * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <QBuffer>
#include <QDebug>
#include <QFile>

#include <nostalgia/core/consts.hpp>

#include "imgconv.hpp"
#include "import_tilesheet_wizard.hpp"

namespace nostalgia::core {

ImportTilesheetWizardMainPage::ImportTilesheetWizardMainPage(const studio::Context *ctx) {
	m_ctx = ctx;
	addLineEdit(tr("&Tile Sheet Name:"), QString(TileSheetName) + "*", "", [this](QString) {
			auto importPath = field(ImportPath).toString();
			if (QFile(importPath).exists()) {
				return 0;
			} else {
				this->showValidationError(tr("Invalid image file: %1").arg(importPath));
				return 1;
			}
		}
	);
	auto fileTypes = "(*.png);;(*.bmp);;(*.jpg);;(*.jpeg)";
	addPathBrowse(tr("Tile Sheet &Path:"), QString(ImportPath) + "*", "",
	              QFileDialog::ExistingFile, fileTypes);
}

ImportTilesheetWizardPalettePage::ImportTilesheetWizardPalettePage(const studio::Context *ctx) {
	m_ctx = ctx;
	addLineEdit(tr("Palette &Name:"), PaletteName);
}

int ImportTilesheetWizardPalettePage::accept() {
	const auto tilesheetName = field(TileSheetName).toString();
	const auto importPath = field(ImportPath).toString();
	const auto paletteName = field(PaletteName).toString();
	const auto outPath = TileSheetDir + tilesheetName + FileExt_ng;
	if (!QFile(importPath).exists()) {
		return OxError(1);
	}
	auto ng = imgToNg(importPath, 0);
	if (!ng) {
		return OxError(1);
	}
	const auto paletteOutPath = PaletteDir + paletteName + FileExt_npal;
	core::NostalgiaPalette pal;
	pal = std::move(ng->pal);
	m_ctx->project->writeObj(paletteOutPath, &pal);
	auto defaultPalette = paletteOutPath.toUtf8();
	ng->defaultPalette = defaultPalette.data();
	m_ctx->project->writeObj(outPath, ng.get());
	return 0;
}

}
