#pragma once

#include "../core/RenderLayerManager.h"
#include "../core/CommandHistory.h"
#include "SceneHierarchyPanel.h"
#include "IconManager.h"
#include <pxr/usd/usd/stage.h>
#include <string>

namespace UsdLayerManager {

/// Maya-Render-Layers-style panel: layer list with one-click activation,
/// create/rename/delete, and a membership editor for whichever layer is
/// selected (independent of which one is active — you can inspect/edit a
/// layer's members without switching to it).
class RenderLayerPanel {
public:
    RenderLayerPanel();
    ~RenderLayerPanel();

    void SetRenderLayerManager(RenderLayerManager* mgr) { m_renderLayerManager = mgr; }
    void SetCommandHistory(CommandHistory* history) { m_commandHistory = history; }
    /// Optional — powers the "Add Selected" membership button.
    void SetSceneHierarchyPanel(SceneHierarchyPanel* panel) { m_sceneHierarchyPanel = panel; }
    void SetIconManager(IconManager* icons) { m_iconManager = icons; }
    void SetStage(pxr::UsdStageRefPtr stage);
    void Render();

    /// Whether Application should auto-save the active render layer's SdfLayer
    /// shortly after each edit. Owned here because the checkbox lives in this
    /// panel; Application reads it back every frame and when persisting
    /// preferences (same setter/getter shape as MaterialEditorPanel's column
    /// widths).
    void SetAutoSaveEnabled(bool enabled) { m_autoSave = enabled; }
    bool IsAutoSaveEnabled() const { return m_autoSave; }

private:
    void RenderLayerList();
    void RenderMembershipPanel();
    void RenderRenameModal();

    RenderLayerManager*  m_renderLayerManager = nullptr;
    CommandHistory*        m_commandHistory      = nullptr;
    SceneHierarchyPanel*   m_sceneHierarchyPanel = nullptr;
    IconManager*            m_iconManager         = nullptr;
    pxr::UsdStageRefPtr     m_stage;

    /// Which layer's membership the lower panel shows — independent of which
    /// layer is currently active.
    bool         m_defaultSelected = true;
    std::string  m_selectedLayerId;

    char         m_newLayerNameBuf[128] = "RenderLayer";

    bool         m_autoSave = true;

    bool         m_openRenameModal = false;
    std::string  m_renameTargetId;
    char         m_renameBuf[128] = {};
};

} // namespace UsdLayerManager
