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

#include <imgui.h>

#include "projectexplorer.hpp"

namespace studio {

ProjectExplorer::ProjectExplorer(keel::Context &kctx) noexcept:
	FileExplorer{kctx, true} {
}

ox::Error ProjectExplorer::refreshProjectTreeModel(ox::StringViewCR) noexcept {
	OX_REQUIRE_M(model, buildFileTreeModel(*this, "Project", "/", nullptr));
	setModel(std::move(model));
	return {};
}

void ProjectExplorer::fileOpened(ox::StringViewCR path) const noexcept {
	fileChosen.emit(path);
}

void ProjectExplorer::fileDeleted(ox::StringViewCR path) const noexcept {
	deleteItem.emit(path);
}

void ProjectExplorer::fileMoved(ox::StringViewCR src, ox::StringViewCR dst) const noexcept {
	moveItem.emit(src, dst);
}

void ProjectExplorer::dirMoved(ox::StringViewCR src, ox::StringViewCR dst) const noexcept {
	moveDir.emit(src, dst);
}

void ProjectExplorer::fileContextMenu(ox::StringViewCR path) const noexcept {
	if (ImGui::BeginPopupContextItem("FileMenu", ImGuiPopupFlags_MouseButtonRight)) {
		if (ImGui::MenuItem("Open")) {
			fileChosen.emit(path);
		}
		if (ImGui::MenuItem("Get Info")) {
			getInfo.emit(path);
		}
		ImGui::Separator();
		if (ImGui::MenuItem("Delete")) {
			deleteItem.emit(path);
		}
		if (ImGui::MenuItem("Rename")) {
			renameItem.emit(path);
		}
		if (ImGui::MenuItem("Make Copy")) {
			makeCopy.emit(path);
		}
		ImGui::EndPopup();
	}
}

void ProjectExplorer::dirContextMenu(ox::StringViewCR path) const noexcept {
	if (ImGui::BeginPopupContextItem("DirMenu", ImGuiPopupFlags_MouseButtonRight)) {
		if (ImGui::MenuItem("Add Item")) {
			addItem.emit(path);
		}
		if (ImGui::MenuItem("Add Directory")) {
			addDir.emit(path);
		}
		if (path.size() && ImGui::MenuItem("Delete")) {
			deleteItem.emit(path);
		}
		ImGui::EndPopup();
    }
}

}
