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

#include "fileinfo.hpp"

namespace studio {

FileInfo::FileInfo(Context &sctx) noexcept:
	Popup("File Info"),
	m_sctx(sctx) {}

ox::Error FileInfo::open(ox::StringParam filePath) noexcept {
	m_filePath = std::move(filePath);
	auto &fs = m_sctx.project->romFs();
	OX_REQUIRE(fileBuff, fs.read(m_filePath));
	auto [hdr, err] = keel::readAssetHeader(fileBuff);
	if (!err) {
		m_fileInfo.emplace(Info{
			.assetId = hdr.uuid.toString(),
			.typeName = std::move(hdr.clawHdr.typeName),
			.typeVersion = hdr.clawHdr.typeVersion,
			.format = [&hdr] {
				switch (hdr.clawHdr.fmt) {
					case ox::ClawFormat::Metal:
						return ox::StringLiteral{"Metal Claw"};
					case ox::ClawFormat::Organic:
						return ox::StringLiteral{"Organic Claw"};
					default:
						return ox::StringLiteral{"Other"};
				}
			}(),
			.dataSize = hdr.clawHdr.dataSize,
		});
	}
	Popup::open();
	return {};
}

void FileInfo::draw(Context&) noexcept {
	if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
		close();
		return;
	}
	switch (m_stage) {
		case Stage::Closed:
			break;
		case Stage::Opening:
			ImGui::OpenPopup(m_title.c_str());
			m_stage = Stage::Open;
			[[fallthrough]];
		case Stage::Open: {
			constexpr auto modalFlags =
					ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
			ig::centerNextWindow();
			auto open = true;
			if (ImGui::BeginPopupModal(m_title.c_str(), &open, modalFlags)) {
				drawTable();
				switch (auto const r = ig::PopupControlsOk(m_open, "Close")) {
					case ig::PopupResponse::None:
						break;
					case ig::PopupResponse::OK:
						response.emit(r);
						m_fileInfo.reset();
						m_filePath = ox::String{};
						close();
						break;
					case ig::PopupResponse::Cancel:
						break;
				}
				ImGui::EndPopup();
			}
			if (!open) {
				m_stage = Stage::Closed;
			}
			break;
		}
	}
}

void FileInfo::drawTable() const noexcept {
	auto const scale = ig::dpiScale();
	ig::IDStackItem const idStackItem{"FileInfo"};
	ImGui::Text("%s", m_filePath.c_str());
	if (m_fileInfo && ImGui::BeginTable(
		"Table", 2,
			ImGuiTableFlags_Borders |
			ImGuiTableFlags_RowBg)) {
		ImGui::TableSetupColumn("Field", ImGuiTableColumnFlags_WidthFixed, 70 * scale);
		ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_NoHide);
		// asset id
		ImGui::TableNextRow();
		ImGui::TableSetColumnIndex(0);
		ImGui::Text("Asset ID:");
		ImGui::TableSetColumnIndex(1);
		ImGui::Text("%s", m_fileInfo->assetId.c_str());
		// typeName
		ImGui::TableNextRow();
		ImGui::TableSetColumnIndex(0);
		ImGui::Text("Type Name:");
		ImGui::TableSetColumnIndex(1);
		ImGui::Text("%s", m_fileInfo->typeName.c_str());
		// typeVersion
		ImGui::TableNextRow();
		ImGui::TableSetColumnIndex(0);
		ImGui::Text("Type Version:");
		ImGui::TableSetColumnIndex(1);
		ImGui::Text("%d", m_fileInfo->typeVersion);
		// format
		ImGui::TableNextRow();
		ImGui::TableSetColumnIndex(0);
		ImGui::Text("Format:");
		ImGui::TableSetColumnIndex(1);
		ImGui::Text("%s", m_fileInfo->format.c_str());
		// size
		ImGui::TableNextRow();
		ImGui::TableSetColumnIndex(0);
		ImGui::Text("Data Size:");
		ImGui::TableSetColumnIndex(1);
		ImGui::Text("%lluB", m_fileInfo->dataSize);
		ImGui::EndTable();
	} else {
		ImGui::Text("No information available");
	}
}

}
