# The hungryeditor application.
#
# Application logic lives in the `hungryeditor_core` static library so that it
# can be exercised by the test tree; `hungryeditor` is a thin entry point.

find_package(Qt6 REQUIRED COMPONENTS Widgets WebEngineWidgets WebChannel Concurrent PrintSupport)

qt_standard_project_setup()

# Embed the tree-sitter highlight queries as C++ string literals.
include(EmbedQueries)
set(_md "${CMAKE_SOURCE_DIR}/third_party/tree-sitter-markdown")
set(_gr "${CMAKE_SOURCE_DIR}/third_party/grammars")
hungryeditor_embed_queries("${CMAKE_CURRENT_BINARY_DIR}/generated/HighlightQueries.h"
    "kMarkdownHighlights|${_md}/tree-sitter-markdown/queries/highlights.scm"
    "kMarkdownInjections|${_md}/tree-sitter-markdown/queries/injections.scm"
    "kMarkdownInlineHighlights|${_md}/tree-sitter-markdown-inline/queries/highlights.scm"
    "kMarkdownInlineInjections|${_md}/tree-sitter-markdown-inline/queries/injections.scm"
    "kLangBash|${_gr}/bash/queries/highlights.scm"
    "kLangC|${_gr}/c/queries/highlights.scm"
    "kLangCpp|${_gr}/c/queries/highlights.scm|${_gr}/cpp/queries/highlights.scm"
    "kLangCSharp|${_gr}/c-sharp/queries/highlights.scm"
    "kLangCss|${_gr}/css/queries/highlights.scm"
    "kLangGo|${_gr}/go/queries/highlights.scm"
    "kLangHtml|${_gr}/html/queries/highlights.scm"
    "kLangJava|${_gr}/java/queries/highlights.scm"
    "kLangJavaScript|${_gr}/javascript/queries/highlights.scm"
    "kLangJson|${_gr}/json/queries/highlights.scm"
    "kLangPhp|${_gr}/php/queries/highlights.scm"
    "kLangPython|${_gr}/python/queries/highlights.scm"
    "kLangRuby|${_gr}/ruby/queries/highlights.scm"
    "kLangRust|${_gr}/rust/queries/highlights.scm"
    "kLangSql|${_gr}/sql/queries/highlights.scm"
    "kLangToml|${_gr}/toml/queries/highlights.scm"
    "kLangTypeScript|${_gr}/javascript/queries/highlights.scm|${_gr}/typescript/queries/highlights.scm"
    "kLangYaml|${_gr}/yaml/queries/highlights.scm")

# Empty today: no WebView2 backend source exists yet (a follow-on branch,
# not this phase — see PreviewBackendFactory.cpp), so there is nothing to
# add even when HUNGRYEDITOR_PREVIEW_BACKEND=webview2. This is the point
# where its sources will join the target once that branch lands.
set(_webview2_backend_sources)

add_library(hungryeditor_core STATIC
    app/CommandLine.cpp
    app/CommandLine.h
    app/MainWindow.cpp
    app/MainWindow.h
    app/StatePaths.cpp
    app/StatePaths.h
    app/TabBar.cpp
    app/TabBar.h
    diagnostics/CrashReporter.cpp
    diagnostics/CrashReporter.h
    editor/CaretHistory.cpp
    editor/CaretHistory.h
    editor/Document.cpp
    editor/Document.h
    editor/DocumentManager.cpp
    editor/DocumentManager.h
    editor/Editor.cpp
    editor/Editor.h
    editor/MarkdownTable.cpp
    editor/MarkdownTable.h
    export/HtmlDocument.cpp
    export/HtmlDocument.h
    highlight/CaptureStyles.cpp
    highlight/CaptureStyles.h
    highlight/CodeHighlighter.cpp
    highlight/CodeHighlighter.h
    highlight/GrammarRegistry.cpp
    highlight/GrammarRegistry.h
    highlight/QueryPredicates.cpp
    highlight/QueryPredicates.h
    highlight/TreeSitterEngine.cpp
    highlight/TreeSitterEngine.h
    highlight/HighlightWorker.cpp
    highlight/HighlightWorker.h
    highlight/HighlightController.cpp
    highlight/HighlightController.h
    io/AssetWriter.cpp
    io/AssetWriter.h
    io/DraftStore.cpp
    io/DraftStore.h
    io/Preferences.cpp
    io/Preferences.h
    io/RecentFiles.cpp
    io/RecentFiles.h
    io/SessionStore.cpp
    io/SessionStore.h
    io/TextFile.cpp
    io/TextFile.h
    markdown/FrontMatter.cpp
    markdown/FrontMatter.h
    markdown/ImageResolver.cpp
    markdown/ImageResolver.h
    markdown/Outline.cpp
    markdown/Outline.h
    markdown/Md4cRenderer.cpp
    markdown/Md4cRenderer.h
    preview/PreviewBackend.h
    preview/PreviewBackendFactory.cpp
    preview/PreviewBackendFactory.h
    preview/PreviewBridge.cpp
    preview/PreviewBridge.h
    preview/PreviewController.cpp
    preview/PreviewController.h
    preview/PreviewProtocol.cpp
    preview/PreviewProtocol.h
    preview/QtWebEnginePreview.cpp
    preview/QtWebEnginePreview.h
    theme/Theme.cpp
    theme/Theme.h
    theme/ThemeFile.cpp
    theme/ThemeFile.h
    ui/CommandPalette.cpp
    ui/CommandPalette.h
    ui/FileTreePanel.cpp
    ui/FileTreePanel.h
    ui/FindReplaceBar.cpp
    ui/FindReplaceBar.h
    ui/FuzzyMatch.cpp
    ui/FuzzyMatch.h
    ui/OutlinePanel.cpp
    ui/OutlinePanel.h
    ui/PreferencesDialog.cpp
    ui/PreferencesDialog.h
    ui/SearchResultsPanel.cpp
    ui/SearchResultsPanel.h
    ui/TabSwitcher.cpp
    ui/TabSwitcher.h
    workspace/FileIndex.cpp
    workspace/FileIndex.h
    workspace/FileOperations.cpp
    workspace/FileOperations.h
    workspace/FileSearch.cpp
    workspace/FileSearch.h
    workspace/WorkspaceStore.cpp
    workspace/WorkspaceStore.h
    ${_webview2_backend_sources}
)
add_library(hungryeditor::core ALIAS hungryeditor_core)

set_target_properties(hungryeditor_core PROPERTIES AUTOMOC ON)

# The preview shell page itself: a real .html file rather than an inlined
# C++ string literal, so a future preview backend (see PreviewBackend.h) can
# load the identical file instead of forking its mermaid/KaTeX lazy-render
# logic, error surfaces and scroll-sync math.
qt_add_resources(hungryeditor_core "preview_shell"
    PREFIX "/hungryeditor/preview"
    BASE "${CMAKE_SOURCE_DIR}/src/preview/shell"
    FILES "${CMAKE_SOURCE_DIR}/src/preview/shell/shell.html")

# Preview-page assets, served to the QtWebEngine shell over qrc: so diagrams and
# math render with no network access. Vendored under third_party/.
qt_add_resources(hungryeditor_core "preview_assets"
    PREFIX "/hungryeditor/preview"
    BASE "${CMAKE_SOURCE_DIR}/third_party/mermaid"
    FILES "${CMAKE_SOURCE_DIR}/third_party/mermaid/mermaid.min.js")

# KaTeX: the script, its stylesheet, and the woff2 faces the stylesheet asks
# for. Kept at the same prefix so katex.min.css resolves url(fonts/…) here.
file(GLOB _katex_fonts CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/third_party/katex/fonts/*.woff2")
qt_add_resources(hungryeditor_core "katex_assets"
    PREFIX "/hungryeditor/preview"
    BASE "${CMAKE_SOURCE_DIR}/third_party/katex"
    FILES
        "${CMAKE_SOURCE_DIR}/third_party/katex/katex.min.js"
        "${CMAKE_SOURCE_DIR}/third_party/katex/katex.min.css"
        ${_katex_fonts})

target_include_directories(hungryeditor_core
    PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}"
    PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")

target_compile_definitions(hungryeditor_core PRIVATE
    HUNGRYEDITOR_VERSION="${PROJECT_VERSION}"
)

target_link_libraries(hungryeditor_core PUBLIC
    Qt6::Widgets
    hungryeditor::scintilla
    hungryeditor::lexilla
    hungryeditor::treesitter
)

# Preview engine only — kept private so tests and code that never touch the
# preview do not drag Chromium into their link.
target_link_libraries(hungryeditor_core PRIVATE
    Qt6::WebEngineWidgets
    Qt6::WebChannel
    Qt6::Concurrent
    Qt6::PrintSupport
    hungryeditor::md4c
)

# Header-only today: no WebView2 backend source links against it yet (see
# _webview2_backend_sources above), but the include directory is proven to
# resolve as soon as it is fetched.
if(HUNGRYEDITOR_PREVIEW_BACKEND STREQUAL "webview2")
    target_link_libraries(hungryeditor_core PRIVATE hungryeditor::webview2_sdk)
endif()

hungryeditor_set_warnings(hungryeditor_core)

# --- entry point ---------------------------------------------------------
add_executable(hungryeditor app/main.cpp)

set_target_properties(hungryeditor PROPERTIES
    WIN32_EXECUTABLE ON
    MACOSX_BUNDLE OFF)

target_compile_definitions(hungryeditor PRIVATE
    HUNGRYEDITOR_VERSION="${PROJECT_VERSION}"
)

target_link_libraries(hungryeditor PRIVATE hungryeditor::core)

hungryeditor_set_warnings(hungryeditor)

# The window/taskbar icon, as plain PNGs at several sizes rather than the
# hicolor SVG below: loading an SVG through QIcon needs the Qt SVG plugin,
# which is not one of this project's dependencies, while PNG decoding is
# always built into Qt. QApplication::setWindowIcon() in main.cpp picks the
# closest size at runtime.
qt_add_resources(hungryeditor "app_icon"
    PREFIX "/hungryeditor/icons"
    BASE "${CMAKE_SOURCE_DIR}/resources/icons/app"
    FILES
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-16.png"
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-32.png"
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-48.png"
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-64.png"
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-128.png"
        "${CMAKE_SOURCE_DIR}/resources/icons/app/icon-256.png")

# The .exe's own icon (Explorer, taskbar before the app has even started),
# set via a classic Win32 resource script rather than a Qt mechanism.
# WIN32_EXECUTABLE/MACOSX_BUNDLE above only pick the subsystem; they don't
# touch the binary's icon resource.
if(WIN32)
    set(HUNGRYEDITOR_ICON_PATH "${CMAKE_SOURCE_DIR}/resources/icons/hungryeditor.ico")
    configure_file("${CMAKE_SOURCE_DIR}/resources/windows/hungryeditor.rc.in"
        "${CMAKE_CURRENT_BINARY_DIR}/generated/hungryeditor.rc" @ONLY)
    target_sources(hungryeditor PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated/hungryeditor.rc")
endif()
