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

#pragma once

#include <studio/studio.hpp>

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

namespace nostalgia::gfx {

class PaletteEditorImGui: public studio::Editor {

	private:
		class PageRenameDialog {
			private:
				ox::IString<50> m_name;
				bool m_show = false;
			public:
				ox::Signal<ox::Error(ox::StringView name)> inputSubmitted;
				constexpr void show(ox::StringView const &name) noexcept {
					m_show = true;
					m_name = name;
				}
				constexpr void close() noexcept {
					m_show = false;
				}
				[[nodiscard]]
				constexpr bool isOpen() const noexcept { return m_show; }
				void draw() noexcept;
		} m_pageRenameDlg;
		studio::Context &m_sctx;
		Palette m_pal;
		size_t m_selectedColorRow = 0;
		size_t m_page = 0;
		enum class FocusCmd {
			None,
			Red,
			Green,
			Blue,
		};
		FocusCmd m_focusCmd;

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

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

	protected:
		ox::Error saveItem() noexcept final;

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

	private:
		static void drawColumnLeftAlign(ox::CStringViewCR txt) noexcept;

		static void drawColumn(ox::CStringViewCR txt) noexcept;

		static void drawColumn(ox::Integer_c auto i) noexcept {
			drawColumn(ox::intToStr(i));
		}

		static void numShortcuts(size_t &val, size_t sizeRange) noexcept;

		void colorInput(ox::CStringViewCR label, int &v, bool &inputFocused, FocusCmd colorChannel) noexcept;

		void drawColorsEditor() noexcept;

		void drawPagesEditor() noexcept;

		void drawColorEditor() noexcept;

		ox::Error renamePage(ox::StringViewCR name) noexcept;

		ox::Error handleCommand(studio::UndoCommand const*) noexcept;
};

}
