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

#pragma once

#include <studio/studio.hpp>

#include <jasper/world/worldobject.hpp>

#include "collisionmapview.hpp"

namespace jasper::world {

class WorldObjectSetEditorImGui: public studio::Editor {

	private:
		studio::Context &m_sctx;
		ox::String m_itemPath;
		WorldObjectSet m_doc;
		keel::AssetRef<ngfx::TileSheet> m_tileSheet;
		ngfx::TileSheet::SubSheet const*m_visibleSubsheet = nullptr;
		ox::Vector<ox::String> m_paletteDisplayNames;
		size_t m_selectedObj{};
		size_t m_selectedPal{};
		ox::String m_tilesheetPath{};
		studio::FilePickerPopup m_tsPicker;
		studio::FilePickerPopup m_addPalPopup;
		CollisionView m_colView{m_sctx};
		struct {
			ox::IString<256> nameBuff;
			int frames{};
			int interval{};
			size_t palette{};
		} m_objEditor;

	public:
		WorldObjectSetEditorImGui(studio::Context &sctx, ox::StringParam path);

		void draw(studio::Context&) noexcept final;

		void onActivated() noexcept override;

		void navigateTo(ox::StringViewCR arg) noexcept override;

	private:
		WorldObject const &activeObj() const noexcept;

		WorldObject &activeObj() noexcept;

		void buildPaletteDisplayNameList() noexcept;

		void drawTileSheetSelector() noexcept;

		void drawObjSelector() noexcept;

		ox::Error loadObj() noexcept;

		void drawObjEditor() noexcept;

		void drawSubSheetNode(ngfx::TileSheet::SubSheet const &ss) noexcept;

		void drawPaletteList() noexcept;

		void drawPaletteListItems() noexcept;

		void drawAddPalettePopup() noexcept;

		void drawTileSheetPicker() noexcept;

		void addPalette(ox::StringViewCR path) noexcept;

		void showAddPalette() noexcept;

		void rmSelectedPalette() noexcept;

		ox::Error setTileSheet(ox::StringViewCR path) noexcept;

		ox::Error handleCmd(studio::UndoCommand const*cmd) noexcept;

		ox::Error saveItem() noexcept final;

};

}
