#pragma once

#include "../core/CommandHistory.h"
#include "../core/MaterialManager.h"
#include "IconManager.h"
#include "MaterialPreviewRenderer.h"
#include "NodeThumbnailCache.h"
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/valueTypeName.h>
#include <pxr/base/vt/value.h>
#include <imgui_node_editor.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

namespace UsdLayerManager {

/// Node-graph editor for authoring UsdShade material networks, with a
/// shader-ball preview rendered through Hydra.
class MaterialEditorPanel {
public:
    MaterialEditorPanel();
    ~MaterialEditorPanel();

    void SetStage(pxr::UsdStageRefPtr stage);
    void SetMaterialManager(MaterialManager* mgr) { m_materialManager = mgr; }
    void SetCommandHistory(CommandHistory* history) { m_commandHistory = history; }
    void SetIconManager(IconManager* icons) { m_iconManager = icons; }

    /// Forwarded to the shader-ball preview so it matches the main viewport's
    /// global color-correction settings.
    void SetColorCorrectionFromPrefs(int ccMode, const std::string& ocioDisplay,
                                     const std::string& ocioView,
                                     const std::string& ocioColorSpace,
                                     const std::string& ocioLook);

    /// Opens the material at pathStr in the graph work area, creating it (as a
    /// "Material" prim) if it doesn't exist yet. Also selects it in the browser.
    void OpenOrCreateMaterial(const std::string& pathStr);

    /// Tracks the current selection (mirrors PropertyPanel/ViewportPanel's
    /// SetSelectedPrimPath) so the toolbar can offer "create + bind" / "bind"
    /// affordances, and so selecting a prim with an already-bound material
    /// auto-loads it into the canvas.
    void SetTargetPrimPath(const std::string& path);

    /// Splitter-adjustable column widths, persisted in preferences.ini by
    /// Application. Setter clamps to the same range as the splitters.
    void GetColumnWidths(float& browserWidth, float& previewWidth) const {
        browserWidth = m_browserWidth;
        previewWidth = m_previewWidth;
    }
    void SetColumnWidths(float browserWidth, float previewWidth);

    void Render();

private:
    /// Resolved endpoint of a rendered pin, keyed by its ax::NodeEditor PinId.
    struct PinInfo {
        pxr::SdfPath nodePath;
        std::string  name;
        bool         isOutput = false;
        pxr::SdfValueTypeName typeName;
    };
    /// Resolved endpoint of a rendered link, keyed by its ax::NodeEditor LinkId.
    struct LinkInfo {
        pxr::SdfPath destNode;
        std::string  destInput;
    };

    void RenderToolbar();
    /// Property editor for the node selected in the graph, shown below the
    /// shader-ball preview. Values apply live while a widget is being
    /// dragged; one undoable command is pushed when the edit ends.
    void RenderSelectedNodeProperties();
    void RenderInputValueWidget(const ShaderGraphNode& node, const ShaderPinInfo& input,
                                const pxr::UsdPrim& prim);
    void CommitInputEdit(const ShaderGraphNode& node, const ShaderPinInfo& input);
    /// Hypershade-style browser: lists every material on the stage; the
    /// selected one is loaded into the graph work area via "Show Graph"
    /// (or double-click).
    void RenderMaterialBrowser();
    /// Creates a uniquely-named empty material under /Materials and opens it.
    void CreateNewMaterial();
    /// Preset graphs for the open material (one undoable command each):
    /// UsdPreviewSurface + UsdUVTextures fed by an st primvar reader, or a
    /// MaterialX standard_surface + image nodes fed by a texcoord node.
    void CreateUsdPresetGraph();
    void CreateMaterialXPresetGraph();
    void CreateArnoldPresetGraph();
    void RenderNodeGraphCanvas();
    void RenderPreviewPanel();
    void HandleCreateAndDelete();
    /// Nuke-style TAB popup: type-to-filter shader node list; Enter or click
    /// creates the highlighted node at canvasPos. When linkSource is non-null
    /// (a connection was dragged onto empty canvas), the list is filtered to
    /// nodes with a type-connectable pin and the chosen node is auto-wired to
    /// linkSource.
    void RenderNodeSearchMenu(const ImVec2& canvasPos, const PinInfo* linkSource = nullptr);
    /// Creates the node and returns its prim path (empty on failure).
    pxr::SdfPath CreateShaderNode(const std::string& shaderId, const ImVec2& canvasPos);
    /// Nudges a creation point right until the new node's estimated rectangle
    /// clears every existing node's rendered rectangle. Overlapping nodes
    /// fight over the editor's hit test: the buried one becomes unmovable and
    /// its pins unhoverable. Must be called with the editor context current.
    ImVec2 FindFreeCanvasSpot(ImVec2 desired, const std::string& shaderId) const;
    /// Binds an existing material to targetPath, undoably, restoring whatever
    /// direct binding (if any) targetPath had before.
    void BindMaterialToTarget(const pxr::SdfPath& materialPath, const pxr::SdfPath& targetPath);
    /// Creates a new material named after m_targetPrimPath under /Materials,
    /// binds it there, and opens it in the canvas.
    void CreateAndBindMaterialForTarget();
    void CreateConnection(const PinInfo& destInput, const PinInfo& sourceOutput);
    void DeleteNode(const pxr::SdfPath& nodePath);
    void DisconnectAttr(const pxr::SdfPath& destNode, const std::string& destInput);
    /// Resolved on-disk path of a texture node's image (its Asset-typed `file`
    /// input), or empty if unauthored/unresolvable. Feeds the thumbnail cache.
    std::string ResolveTextureFilePath(const ShaderGraphNode& node) const;
    void SyncFromUsd();
    void PersistNodePosition(ax::NodeEditor::NodeId nodeId);
    bool IsPinLinked(const pxr::SdfPath& nodePath, const std::string& pinName, bool isOutput) const;

    static bool SaveNodeSettingsCallback(ax::NodeEditor::NodeId nodeId,
                                         const char* data, size_t size,
                                         ax::NodeEditor::SaveReasonFlags reason,
                                         void* userPointer);

    ax::NodeEditor::EditorContext* m_editorContext = nullptr;
    pxr::UsdStageRefPtr m_stage;
    MaterialManager*    m_materialManager = nullptr;
    CommandHistory*     m_commandHistory  = nullptr;
    IconManager*        m_iconManager     = nullptr;

    /// Material currently shown in the graph work area (empty = none).
    pxr::SdfPath m_materialPath;
    /// Material highlighted in the browser list; becomes m_materialPath when
    /// the user clicks "Show Graph" (or double-clicks the entry).
    pxr::SdfPath m_browserSelection;
    /// Currently-selected prim, tracked for the "create + bind" / "bind"
    /// toolbar affordances; empty when nothing (or a non-prim path) is selected.
    pxr::SdfPath m_targetPrimPath;

    ShaderGraphSnapshot m_graph;
    /// NodeId (uintptr_t) -> prim path, for nodes already known to the editor
    /// this session (seeded position, so re-syncing won't fight live drags).
    std::unordered_map<uintptr_t, pxr::SdfPath> m_nodeIdToPath;
    /// Rebuilt every frame from the current m_graph while rendering.
    std::unordered_map<uintptr_t, PinInfo>  m_pinIdToInfo;
    std::unordered_map<uintptr_t, LinkInfo> m_linkIdToInfo;

    MaterialPreviewRenderer m_preview;
    /// Per-node in-canvas thumbnails (texture images + material shader balls).
    NodeThumbnailCache m_thumbnails;

    ImVec2 m_pendingCreateNodePos{0.0f, 0.0f};

    /// Alt+RMB drag distance not yet converted into a discrete wheel-zoom
    /// step (the node editor only zooms in wheel increments).
    float m_zoomDragAccum = 0.0f;

    char m_nodeSearchBuf[128] = "";
    int  m_nodeSearchSelected = 0;

    /// Nuke-style drag-to-create: set when a connection was dragged from a pin
    /// onto empty canvas. m_openLinkDragMenu requests the create-node popup on
    /// the next frame (deferred so OpenPopup runs while the editor is
    /// suspended); m_linkDragSourcePin is the pin to auto-wire the new node to.
    bool    m_openLinkDragMenu = false;
    PinInfo m_linkDragSourcePin;

    /// Column widths of the browser (left) and preview (right) sections,
    /// user-adjustable via the splitters between them and the canvas.
    float m_browserWidth = 220.0f;
    float m_previewWidth = 320.0f;

    /// Fraction of the browser column's height given to the materials list;
    /// the create-node list takes the rest. User-adjustable via the horizontal
    /// splitter between them.
    float m_browserListRatio = 0.45f;

    /// Inline-rename state for the materials list (double-click a row to start;
    /// mirrors SceneHierarchyPanel): the material being renamed (empty = none),
    /// the edit buffer, and a one-frame flag to focus the InputText on start.
    pxr::SdfPath m_renamingMaterial;
    char         m_materialRenameBuf[128] = "";
    bool         m_materialRenameJustStarted = false;

    /// Search text of the browser's persistent create-node list.
    char m_browserNodeSearchBuf[128] = "";
    /// Shader id clicked in the browser's create-node list; created at the
    /// canvas view center on the next RenderNodeGraphCanvas (ScreenToCanvas
    /// is only valid inside the editor's Begin/End).
    std::string m_pendingCreateShaderId;

    /// Debug event-trace state (see the logging block at the end of
    /// RenderNodeGraphCanvas): whether the last LMB press landed on a node
    /// rect, whether that press's drag-start has been logged yet, and the
    /// editor NodeIds hit at press time (their live positions are logged on
    /// every drag-move so a node that stops following the mouse is visible).
    bool m_debugPressOnNode = false;
    bool m_debugDragLogged  = false;
    std::vector<uintptr_t> m_debugDragNodeIds;

    /// First node currently selected in the graph editor (empty = none);
    /// drives the properties section under the preview.
    pxr::SdfPath m_selectedNodePath;
    /// Value/authored-state of the input being edited, captured when its
    /// widget activates, so the commit command can restore it on undo. Only
    /// one ImGui widget can be active at a time, so one slot suffices.
    pxr::VtValue m_preEditValue;
    bool         m_preEditWasAuthored = false;
};

} // namespace UsdLayerManager
