# Flutter Windows plugin — captchala.
#
# Runtime requirements (target machine):
#   Windows 10 (build 1809 or newer).

cmake_minimum_required(VERSION 3.14)
project(captchala LANGUAGES CXX)

# Plugin name — must match `pluginClass` casing-folded in pubspec.yaml
set(PLUGIN_NAME "captchala_plugin")

list(APPEND PLUGIN_SOURCES
    "captchala_plugin.cpp"
    "captchala_plugin.h"
    "captchala_plugin_c_api.cpp"
)

add_library(${PLUGIN_NAME} STATIC ${PLUGIN_SOURCES})

apply_standard_settings(${PLUGIN_NAME})

set_target_properties(${PLUGIN_NAME} PROPERTIES
    CXX_VISIBILITY_PRESET hidden
)

target_compile_definitions(${PLUGIN_NAME} PRIVATE
    FLUTTER_PLUGIN_IMPL
    UNICODE _UNICODE
)

if(MSVC)
    target_compile_options(${PLUGIN_NAME} PRIVATE /utf-8 /wd4505)
endif()

target_include_directories(${PLUGIN_NAME} INTERFACE
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

target_link_libraries(${PLUGIN_NAME} PRIVATE
    flutter
    flutter_wrapper_plugin
)

# Resolve where the prebuilt DLLs come from:
#   1. If env var CAPTCHALA_LOCAL_PREBUILDS is set and points to a directory
#      containing captchala_{ui,client}.dll, use those (offline / CI / dev).
#   2. Otherwise download from CDN, cache under build/captchala/, verify SHA256.
set(_CA_CACHE_DIR "${CMAKE_BINARY_DIR}/captchala")
set(_CA_ZIP       "${_CA_CACHE_DIR}/win32-x64.zip")
set(_CA_UI_DLL    "${_CA_CACHE_DIR}/captchala_ui.dll")
set(_CA_CLIENT_DLL "${_CA_CACHE_DIR}/captchala_client.dll")
set(_CA_SHA256    "fb1e2691a88360c22bab56fe40f11ad2b64034ffd97c2f660d2338388c44fdaf")
set(_CA_URLS
    "https://pkg.captcha-pkg.net/flutter/win32-x64.zip"
    "https://pkg.captcha-cdn.net/flutter/win32-x64.zip"
)

# Local override via env var: bypass CDN entirely.
if(DEFINED ENV{CAPTCHALA_LOCAL_PREBUILDS})
    set(_CA_LOCAL_DIR "$ENV{CAPTCHALA_LOCAL_PREBUILDS}")
    if(EXISTS "${_CA_LOCAL_DIR}/captchala_ui.dll" AND EXISTS "${_CA_LOCAL_DIR}/captchala_client.dll")
        message(STATUS "captchala: using local prebuilds from ${_CA_LOCAL_DIR}")
        file(MAKE_DIRECTORY "${_CA_CACHE_DIR}")
        file(COPY "${_CA_LOCAL_DIR}/captchala_ui.dll" "${_CA_LOCAL_DIR}/captchala_client.dll"
             DESTINATION "${_CA_CACHE_DIR}")
    else()
        message(FATAL_ERROR
            "captchala: CAPTCHALA_LOCAL_PREBUILDS=${_CA_LOCAL_DIR} but "
            "captchala_{ui,client}.dll not found there.")
    endif()
endif()

if(NOT EXISTS "${_CA_UI_DLL}" OR NOT EXISTS "${_CA_CLIENT_DLL}")
    file(MAKE_DIRECTORY "${_CA_CACHE_DIR}")
    set(_CA_DL_OK FALSE)
    foreach(_url IN LISTS _CA_URLS)
        message(STATUS "captchala: downloading SDK for win32-x64 from ${_url}")
        file(DOWNLOAD "${_url}" "${_CA_ZIP}"
            SHOW_PROGRESS
            STATUS _CA_DL_STATUS)
        list(GET _CA_DL_STATUS 0 _CA_DL_CODE)
        if(_CA_DL_CODE EQUAL 0)
            file(SHA256 "${_CA_ZIP}" _CA_ACTUAL_SHA)
            if(_CA_ACTUAL_SHA STREQUAL "${_CA_SHA256}")
                set(_CA_DL_OK TRUE)
                break()
            endif()
            message(WARNING "captchala: SHA256 mismatch from ${_url} "
                "(expected ${_CA_SHA256}, got ${_CA_ACTUAL_SHA}); trying next mirror.")
            file(REMOVE "${_CA_ZIP}")
        else()
            message(WARNING "captchala: download failed from ${_url} "
                "(${_CA_DL_STATUS}); trying next mirror.")
        endif()
    endforeach()
    if(NOT _CA_DL_OK)
        message(FATAL_ERROR "captchala: all CDN mirrors failed for win32-x64.zip.")
    endif()
    file(ARCHIVE_EXTRACT INPUT "${_CA_ZIP}" DESTINATION "${_CA_CACHE_DIR}")
endif()

set(captchala_bundled_libraries "${_CA_UI_DLL};${_CA_CLIENT_DLL}" PARENT_SCOPE)
