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

#include <turbine/gfx.hpp>

#include <studio/imguiutil.hpp>

#include "deleteconfirmation.hpp"

namespace studio {

DeleteConfirmation::DeleteConfirmation() noexcept {
	setTitle("Delete Item");
}

void DeleteConfirmation::openPath(ox::StringViewCR path) noexcept {
	open();
	m_path = path;
}

void DeleteConfirmation::open() noexcept {
	m_path = "";
	m_stage = Stage::Opening;
}

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

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

void DeleteConfirmation::draw(Context &ctx) noexcept {
	if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
		close();
		return;
	}
	switch (m_stage) {
		case Stage::Closed:
			break;
		case Stage::Opening:
			ImGui::OpenPopup(title().c_str());
			m_stage = Stage::Open;
			m_open = true;
			// require extended refresh in case new contents require resize
			// this can take around a second
			turbine::requireRefreshFor(ctx.tctx, 1000);
		[[fallthrough]];
		case Stage::Open:
			drawWindow(ctx.tctx, m_open, [this] {
				ImGui::Text("Are you sure you want to delete %s?", m_path.c_str());
				switch (ig::PopupControlsOkCancel(m_open, "Yes", "No")) {
					case ig::PopupResponse::None:
						break;
					case ig::PopupResponse::OK:
						deleteFile.emit(m_path);
						close();
						break;
					case ig::PopupResponse::Cancel:
						close();
						break;
				}
			});
			break;
	}
}

}
