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

#include <ox/std/string.hpp>

#include "editor.hpp"

namespace nostalgia::studio {

const ox::String &BaseEditor::itemDisplayName() const noexcept {
	return itemName();
}

void BaseEditor::cut() {
}

void BaseEditor::copy() {
}

void BaseEditor::paste() {
}

void BaseEditor::exportFile() {
}

void BaseEditor::keyStateChanged(core::Key, bool) {
}

void BaseEditor::close() {
	this->closed.emit(itemName());
}

void BaseEditor::save() noexcept {
	const auto err = saveItem();
	if (!err) {
		setUnsavedChanges(false);
	} else {
		if constexpr(ox::defines::Debug) {
			oxErrorf("Could not save file {}: {} ({}:{})", itemName(), toStr(err), err.file, err.line);
		} else {
			oxErrorf("Could not save file {}: {}", itemName(), toStr(err));
		}
	}
}

void BaseEditor::setUnsavedChanges(bool uc) {
    m_unsavedChanges = uc;
    unsavedChangesChanged.emit(uc);
}

bool BaseEditor::unsavedChanges() const noexcept {
	return m_unsavedChanges;
}

void BaseEditor::setExportable(bool exportable) {
	m_exportable = exportable;
	exportableChanged.emit(exportable);
}

bool BaseEditor::exportable() const {
	return m_exportable;
}

void BaseEditor::setCutEnabled(bool v) {
	m_cutEnabled = v;
	cutEnabledChanged.emit(v);
}

bool BaseEditor::cutEnabled() const {
	return m_cutEnabled;
}

void BaseEditor::setCopyEnabled(bool v) {
	m_copyEnabled = v;
	copyEnabledChanged.emit(v);
}

bool BaseEditor::copyEnabled() const {
	return m_copyEnabled;
}

void BaseEditor::setPasteEnabled(bool v) {
	m_pasteEnabled = v;
	pasteEnabledChanged.emit(v);
}

bool BaseEditor::pasteEnabled() const {
	return m_pasteEnabled;
}

ox::Error BaseEditor::saveItem() noexcept {
	return OxError(0);
}

Editor::Editor() noexcept {
	m_undoStack.changeTriggered.connect(this, &Editor::markUnsavedChanges);
}

ox::Error Editor::markUnsavedChanges(const UndoCommand*) noexcept {
	setUnsavedChanges(true);
	return OxError(0);
}

}
