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

#pragma once

#include <glutils/glutils.hpp>

#include <studio/studio.hpp>

#include <nostalgia/gfx/context.hpp>
#include <nostalgia/gfx/gfx.hpp>

#include <jasper/world/world.hpp>

#include "../maptilehighlighter.hpp"

namespace jasper::world {

namespace ngfx = nostalgia::gfx;

class WorldEditorView: public ox::SignalHandler {

	private:
		studio::Context &m_sctx;
		ox::UPtr<ngfx::Context> m_nctx;
		WorldStatic const &m_worldStatic;
		int m_columns{}, m_rows{};
		World m_world;
		glutils::FrameBuffer m_frameBuffer;
		int m_scale = 1;
		ox::Size m_scaleSz = ngfx::gl::drawSize(m_scale);
		ox::Optional<studio::Selection> m_selection;
		MapTileHighlighter m_highlighter;
		bool m_animateWorld{};

	public:
		WorldEditorView(studio::Context &ctx, WorldStatic const &worldStatic);

		ox::Error setupTile(uint32_t lyr, uint32_t x, uint32_t y) noexcept;

		void setupTiles() noexcept;

		ox::Error setupWorld() noexcept;

		void draw(ox::Size const &targetSz) noexcept;

		void clearSelection() noexcept;

		void setSelection(studio::Selection const &sel) noexcept;

		[[nodiscard]]
		bool animate() const noexcept { return m_animateWorld; }

		void setAnimate(bool animate) noexcept { m_animateWorld = animate; }

		[[nodiscard]]
		glutils::FrameBuffer const &framebuffer() const noexcept;

		[[nodiscard]]
		constexpr int scale() const noexcept { return m_scale; }

		[[nodiscard]]
		ox::Size drawSize() const noexcept;

};

}
