if (fmt_FOUND)
  get_target_property(FMT_INCLUDE_DIR fmt::fmt INTERFACE_INCLUDE_DIRECTORIES)
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  add_definitions(-DNOGDI -DNOMINMAX)
endif()

add_definitions(-DBOOST_FILESYSTEM_NO_DEPRECATED)

include_directories(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_SOURCE_DIR}/src
  ${VENDOR_INSTALL_DIR}/include
  ${Boost_INCLUDE_DIRS}
  ${FMT_INCLUDE_DIR}
  ${CMAKE_SOURCE_DIR}/proto/cpp
)

file(GLOB PROTO_SRC ${CMAKE_SOURCE_DIR}/proto/cpp/vereign/client_library/*.cc)
list(APPEND PROTO_SRC
  ${CMAKE_SOURCE_DIR}/proto/cpp/google/api/annotations.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/google/api/http.pb.cc

  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/versions/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/audit-log-agent/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/entities-management-agent/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/hyperledger-agent/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/image-storage-agent/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/restful-api/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/signing-service-agent/api/api.pb.cc
  ${CMAKE_SOURCE_DIR}/proto/cpp/code.vereign.com/code/viam-apis/passport-generation-agent/api/api.pb.cc
)
add_library(vereignproto STATIC ${PROTO_SRC})
set_property(TARGET vereignproto PROPERTY POSITION_INDEPENDENT_CODE ON)

target_link_libraries(
  vereignproto
  protobuf::libprotobuf
  OpenSSL::SSL
  OpenSSL::Crypto
  $<$<CXX_COMPILER_ID:MSVC>:CRYPT32.LIB>
)
if (VEREIGN_USE_PRECOMPILED_HEADERS)
  file(GLOB PROTO_HEADERS ${CMAKE_SOURCE_DIR}/proto/cpp/vereign/client_library/*pb.h)
  target_precompile_headers(vereignproto PUBLIC ${PROTO_HEADERS})
endif()

set(VEREIGNLIB_SRC
  vereign/core/rand.cc
  vereign/core/temp.cc
  vereign/core/fs.cc
  vereign/core/string.cc

  vereign/restapi/detail/http_reader.cc
  vereign/restapi/client.cc

  # vereign/grpc/gen/gen.cc
  vereign/grpc/json/encoder.cc
  vereign/grpc/service_registry.cc
  vereign/grpc/server.cc

  vereign/sqlite/statement.cc
  vereign/sqlite/connection.cc

  vereign/bytes/view_dump.cc
  vereign/bytes/buffer.cc

  vereign/encoding/binary.cc
  vereign/encoding/base64.cc
  vereign/encoding/hex.cc

  vereign/crypto/aes.cc
  vereign/crypto/rsa.cc
  vereign/crypto/bio.cc
  vereign/crypto/digest.cc

  vereign/kvstore/lock.cc
  vereign/kvstore/detail/base_crypto_storage.cc
  vereign/kvstore/sqlite_storage.cc
  vereign/kvstore/crypto_storage.cc

  vereign/identity/provider.cc

  vereign/service/gen/passport_service.cc
  vereign/service/passport_service.cc
  vereign/service/identity_service.cc

  # FIXME: remove this
  vereign/service/gen/identity_service.cc
  vereign/service/gen/passport_service.cc
)


if (LINUX)
  list(APPEND VEREIGNLIB_SRC
    vereign/kvstore/detail/linux_crypto_storage.cc
  )
elseif (WIN32)
  list(APPEND VEREIGNLIB_SRC
    vereign/ncrypt/errors.cc
    vereign/ncrypt/unique_ptr.cc
    vereign/ncrypt/rsa.cc
    vereign/kvstore/detail/win_crypto_storage.cc
  )
endif()

# file(GLOB GENERATED_SERVICES_SRC vereign/service/gen/*.cc)
# list(APPEND VEREIGNLIB_SRC ${GENERATED_SERVICES_SRC})

add_library(vereignlib STATIC ${VEREIGNLIB_SRC})
set_property(TARGET vereignlib PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(vereignlib PRIVATE
  nlohmann_json::nlohmann_json
)
target_link_libraries(vereignlib PUBLIC
  vereignproto
  fmt::fmt
  gRPC::grpc++_reflection
  gRPC::grpc++
  Boost::filesystem
  $<$<CXX_COMPILER_ID:MSVC>:Boost::date_time>
  SQLite::SQLite3
  $<$<CXX_COMPILER_ID:MSVC>:ncrypt.lib>
  $<$<CXX_COMPILER_ID:MSVC>:cryptui.lib>
)

add_library(vereign SHARED
  vereign/vereign.cc
)
target_include_directories(vereign
  PRIVATE ${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(vereign PRIVATE
  vereignlib
)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  # Set the DLLEXPORT variable to export symbols
  target_compile_definitions(vereign PRIVATE WIN_EXPORT)
endif()

set(csandbox_sources
  csandbox.cc
)

#add_library(csandboxlib STATIC ${csandboxlib_src})
#target_link_libraries(csandboxlib ${LIBS})

add_executable(csandbox ${csandbox_sources})

target_link_libraries(csandbox
  PRIVATE vereignlib
  $<$<CXX_COMPILER_ID:MSVC>:Boost::date_time>
  Boost::filesystem
  # Boost::file
  # Boost::thread
  # vereign
  # fmt::fmt
  # Boost::regex
  # Threads::Threads
  # OpenSSL::SSL
  # $<$<CXX_COMPILER_ID:MSVC>:CRYPT32.LIB>
)
if (VEREIGN_USE_TIME_TRACE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  target_compile_options(csandbox
    PRIVATE "-ftime-trace"
  )
endif()

# Generates the gRPC and Vereign services source code.
add_custom_target(
  vcl-gen
  COMMAND vcl-gen generate -v --templates-path=templates --output-path src
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)