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

#include <imgui.h>

#include "lib/context.hpp"
#include "lib/imguiuitl.hpp"

#include "newmenu.hpp"

namespace nostalgia {

NewMenu::NewMenu() noexcept {
	setTitle("New Item");
	setSize({225, 110});
}

void NewMenu::open() noexcept {
	m_stage = Stage::Opening;
	m_selectedType = 0;
}

void NewMenu::close() noexcept {
	m_stage = Stage::Closed;
	m_open = false;
}

bool NewMenu::isOpen() const noexcept {
	return m_open;
}

void NewMenu::draw(core::Context *ctx) noexcept {
	switch (m_stage) {
		case Stage::Opening:
			ImGui::OpenPopup(title().c_str());
			m_stage = Stage::NewItemType;
			m_open = true;
			[[fallthrough]];
		case Stage::NewItemType:
			drawNewItemType(ctx);
			break;
		case Stage::NewItemName:
			drawNewItemName(ctx);
			break;
		case Stage::Closed:
			m_open = false;
			break;
	}
}

void NewMenu::addItemMaker(ox::UniquePtr<studio::ItemMaker> im) noexcept {
	m_types.emplace_back(std::move(im));
}

void NewMenu::drawNewItemType(core::Context *ctx) noexcept {
	drawWindow(ctx, &m_open, [this] {
		auto items = ox_malloca(m_types.size() * sizeof(const char*), const char*, nullptr);
		for (auto i = 0u; const auto &im : m_types) {
			items[i] = im->name.c_str();
			++i;
		}
		ImGui::ListBox("Item Type", &m_selectedType, items.get(), m_types.size());
		drawFirstPageButtons();
		ImGui::EndPopup();
	});
}

void NewMenu::drawNewItemName(core::Context *ctx) noexcept {
	drawWindow(ctx, &m_open, [this, ctx] {
		const auto typeIdx = static_cast<std::size_t>(m_selectedType);
		if (typeIdx < m_types.size()) {
			ImGui::InputText("Name", m_itemName.data(), m_itemName.cap());
		}
		drawLastPageButtons(ctx);
		ImGui::EndPopup();
	});
}

void NewMenu::drawFirstPageButtons() noexcept {
	ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 80);
	ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20);
	if (ImGui::Button("Next")) {
		m_stage = Stage::NewItemName;
	}
	ImGui::SameLine();
	if (ImGui::Button("Quit")) {
		ImGui::CloseCurrentPopup();
		m_stage = Stage::Closed;
	}
}

void NewMenu::drawLastPageButtons(core::Context *ctx) noexcept {
	ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 138);
	ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20);
	if (ImGui::Button("Back")) {
		m_stage = Stage::NewItemType;
	}
	ImGui::SameLine();
	if (ImGui::Button("Finish")) {
		finish(ctx);
	}
	ImGui::SameLine();
	if (ImGui::Button("Quit")) {
		ImGui::CloseCurrentPopup();
		m_stage = Stage::Closed;
	}
}

void NewMenu::finish(core::Context *ctx) noexcept {
	const auto err = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, m_itemName.c_str());
	if (err) {
		oxDebugf("NewMenu::finish() error: {}", toStr(err));
		oxLogError(err);
		return;
	}
	finished.emit("");
	m_stage = Stage::Closed;
}

}
