#pragma once

#include "ImGuiContext.h"
#include "IconManager.h"
#include "StageEditorPanel.h"
#include "SceneHierarchyPanel.h"
#include "ViewportPanel.h"
#include "PropertyPanel.h"
#include "TimelinePanel.h"
#include "CurveEditorPanel.h"
#include "MaterialEditorPanel.h"
#include "../core/UsdStageManager.h"
#include "../core/LayerManager.h"
#include "../core/PropertyManager.h"
#include "../core/MaterialManager.h"
#include "../core/CommandHistory.h"
#include "../utils/MovieEncoder.h"
#include <memory>
#include <string>

namespace UsdLayerManager {

/// Global (non-per-viewport) rendering preferences persisted to preferences.ini.
struct AppPreferences {
    std::string renderDelegate      = "HdStormRendererPlugin";
    int         colorCorrectionMode = 1; ///< ColorCorrectionMode cast to int (1 = sRGB)
    std::string ocioDisplay;
    std::string ocioView;
    std::string ocioColorSpace;
    std::string ocioLook;
    float materialBrowserWidth = 220.0f; ///< Material Editor left-column width
    float materialPreviewWidth = 320.0f; ///< Material Editor right-column width
};

class Application {
public:
    Application();
    ~Application();

    bool Initialize(const std::string& windowTitle = "USD Layer Manager", int width = 1280, int height = 720);
    void Run();
    void Shutdown();

private:
    void Update();
    void RenderUI();
    void RenderMenuBar();
    void RenderStatusBar();
    void RenderStageInfo();
    void RefreshManagers();

    // File operations
    void OpenUsdFile();
    void CreateNewUsdFile();
    void SaveUsdFile();
    void SaveUsdFileAs();
    void CloseUsdFile();

    // Stage editing operations
    void AddReferenceToStage();
    void CreatePrimOnStage(const std::string& typeName);

    // Playblast
    void CapturePlayblastFrame();

    // Preferences
    void LoadPreferences();
    void SavePreferences();
    void ApplyPrefsToAllViewports();
    void RenderPreferencesDialog();

    std::unique_ptr<ImGuiContext> m_imguiContext;
    std::unique_ptr<IconManager> m_iconManager;
    std::unique_ptr<UsdStageManager> m_stageManager;
    std::unique_ptr<LayerManager> m_layerManager;
    std::unique_ptr<PropertyManager> m_propertyManager;
    std::unique_ptr<MaterialManager> m_materialManager;
    CommandHistory m_commandHistory;
    std::unique_ptr<StageEditorPanel> m_stageEditorPanel;
    std::unique_ptr<SceneHierarchyPanel> m_sceneHierarchyPanel;
    std::unique_ptr<ViewportPanel> m_viewportPanel;
    std::unique_ptr<PropertyPanel> m_propertyPanel;
    std::unique_ptr<TimelinePanel> m_timelinePanel;
    std::unique_ptr<CurveEditorPanel> m_curveEditorPanel;
    std::unique_ptr<MaterialEditorPanel> m_materialEditorPanel;
    bool m_showDemoWindow;
    bool m_showStageInfo;
    bool m_showStageEditor;
    bool m_showSceneHierarchy;
    bool m_showViewport;
    bool m_showPropertyPanel;
    bool m_showTimeline;
    bool m_showCurveEditor;
    bool m_showMaterialEditor;
    bool m_running;

    MovieEncoder   m_movieEncoder;
    std::string    m_viewportSettingsPath;
    std::string    m_prefsPath;
    AppPreferences m_prefs;
    bool           m_showPreferences = false;

    // OCIO edit buffers for Preferences dialog
    char m_prefOcioDisplayBuf[128]    = {};
    char m_prefOcioViewBuf[128]       = {};
    char m_prefOcioColorSpaceBuf[128] = {};
    char m_prefOcioLookBuf[128]       = {};
    bool m_prefOcioFieldsSynced       = false;
};

} // namespace UsdLayerManager
