import conan
import conan.tools.build
import conan.tools.cmake

class noto_conan(conan.ConanFile):
  name = "noto"
  version = "latest"
  description = "C++23 embedded SQLite database management library"
  license = "MIT"
  package_type = "library"
  settings = "os", "arch", "compiler", "build_type"
  options = {
    "shared": [
      True,
      False,
    ],
    "fPIC": [
      True,
      False,
    ],
    "with_tests": [
      True,
      False,
    ],
  }
  default_options = {
    "shared": False,
    "fPIC": True,
    "with_tests": False,
  }
  exports = "modulus.json"
  exports_sources = (
    "CMakeLists.txt",
    "cmake/*.in",
    "include/*.hpp",
    "include/noto/*.hpp",
    "include/noto/detail/*.hpp",
    "src/*.cpp",
    "src/*.hpp",
    "tests/CMakeLists.txt",
    "tests/*.cpp",
    "tests/*.hpp",
  )

  def config_options(self):
    if self.settings.os == "Windows":
      del self.options.fPIC

  def configure(self):
    if self.options.shared:
      del self.options.fPIC

  def requirements(self):
    self.requires(
      "modulus/latest",
      transitive_headers=True,
      transitive_libs=True,
      options={
        "with_sqlite": True,
      },
    )

  def generate(self):
    deps = conan.tools.cmake.CMakeDeps(self)
    deps.generate()
    toolchain = conan.tools.cmake.CMakeToolchain(self)
    if hasattr(toolchain, "user_presets_path"):
      toolchain.user_presets_path = False
    toolchain.variables["BUILD_SHARED_LIBS"] = bool(self.options.shared)
    toolchain.variables["BUILD_TESTING"] = bool(self.options.with_tests)
    toolchain.generate()

  def build(self):
    cmake = conan.tools.cmake.CMake(self)
    cmake.configure()
    cmake.build()
    if self.options.with_tests and conan.tools.build.can_run(self):
      cmake.test(target="check")

  def package_id(self):
    self.info.options.rm_safe("with_tests")
    for requirement in (
      "openssl",
      "raylib",
      "glfw",
      "opengl",
      "miniaudio",
    ):
      if requirement in self.info.requires.pkg_names:
        self.info.requires.remove(requirement)

  def package(self):
    cmake = conan.tools.cmake.CMake(self)
    cmake.install()

  def package_info(self):
    self.cpp_info.set_property(
      "cmake_file_name",
      "noto",
    )
    self.cpp_info.set_property(
      "cmake_target_name",
      "noto::noto",
    )
    self.cpp_info.libs = ["noto"]
    self.cpp_info.requires = [
      "modulus::modulus",
      "modulus::boost_json",
      "modulus::sqlite",
    ]
