import os
import pathlib

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

import modulus.config

class modulus_recipe(conan.ConanFile):
  name = "modulus"
  version = "latest"
  description = "Shared Conan/CMake tooling and C++23 utility framework"
  license = "MIT"
  package_type = "library"
  settings = "os", "arch", "compiler", "build_type"
  options = {
    "shared": [
      True,
      False,
    ],
    "fPIC": [
      True,
      False,
    ],
    "with_tests": [
      True,
      False,
    ],
    "with_openssl": [
      True,
      False,
    ],
    "with_sqlite": [
      True,
      False,
    ],
    "with_raylib": [
      True,
      False,
    ],
    "with_miniaudio": [
      True,
      False,
    ],
  }
  default_options = {
    "shared": False,
    "fPIC": True,
    "with_tests": False,
    "with_openssl": False,
    "with_sqlite": False,
    "with_raylib": False,
    "with_miniaudio": False,
    "raylib/*:camera_system": False,
    "raylib/*:custom_frame_control": True,
    "raylib/*:customize_build": True,
    "raylib/*:gestures_system": True,
    "raylib/*:module_raudio": False,
    "raylib/*:opengl_version": "ES-2.0",
    "raylib/*:rprand_generator": False,
    "glfw/*:with_wayland": False,
    "glfw/*:with_x11": False,
  }
  exports = "modulus/*.py", "modulus.json"
  exports_sources = (
    "CMakeLists.txt",
    "cmake/*.cmake",
    "cmake/*.in",
    "include/*.hpp",
    "include/modulus/*.hpp",
    "src/*.cpp",
    "tests/CMakeLists.txt",
    "tests/*.cpp",
    "tests/*.hpp",
    "schemas/*.json",
    "tools/*.py",
    "tools/nvim/*.lua",
    "tools/conan/profiles/*",
    "tools/docker/*",
    "README.md",
  )
  def config_options(self):
    settings = self.settings
    options = self.options
    if settings.os == "Windows":
      del options.fPIC
  def configure(self):
    options = self.options
    if options.shared:
      del options.fPIC
  def requirements(self):
    config = modulus.config.load_recipe_config(self)
    self.requires(modulus.config.boost_ref(config), transitive_headers=True, transitive_libs=True, options=modulus.config.boost_requirement_options(config))
    if self.options.with_openssl:
      self.requires(
        "openssl/4.0.1",
        transitive_headers=True,
        transitive_libs=True,
      )
    if self.options.with_sqlite:
      self.requires(
        "sqlite3/3.53.2",
        transitive_headers=True,
        transitive_libs=True,
      )
    if self.options.with_raylib:
      self.requires(
        "raylib/6.0",
        transitive_headers=True,
        transitive_libs=True,
      )
    if self.options.with_miniaudio:
      self.requires(
        "miniaudio/0.11.22",
        transitive_headers=True,
        transitive_libs=True,
      )
  def generate(self):
    options = self.options
    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(options.shared)
    toolchain.variables["BUILD_TESTING"] = bool(options.with_tests)
    toolchain.variables["MODULUS_WITH_OPENSSL"] = bool(options.with_openssl)
    toolchain.variables["MODULUS_WITH_SQLITE"] = bool(options.with_sqlite)
    toolchain.variables["MODULUS_WITH_RAYLIB"] = bool(options.with_raylib)
    toolchain.variables["MODULUS_WITH_MINIAUDIO"] = bool(options.with_miniaudio)
    toolchain.generate()
  def build(self):
    options = self.options
    cmake = conan.tools.cmake.CMake(self)
    cmake.configure()
    cmake.build()
    if 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")
  def package(self):
    cmake = conan.tools.cmake.CMake(self)
    cmake.install()
    package = pathlib.Path(self.package_folder)
    conan.tools.files.copy(self, "*.py", src=pathlib.Path(self.recipe_folder) / "modulus", dst=package / "modulus", keep_path=True)
    conan.tools.files.copy(self, "*", src=pathlib.Path(self.source_folder) / "tools", dst=package / "tools", keep_path=True)
  def package_info(self):
    cpp_info = self.cpp_info
    cpp_info.set_property("cmake_file_name", "modulus")
    cpp_info.set_property("cmake_target_name", "modulus::all")
    cpp_info.set_property(
      "cmake_build_modules",
      [os.path.join("lib", "cmake", "modulus", "modulus_third_party.cmake")],
    )
    components = cpp_info.components
    components["modulus"].set_property("cmake_target_name", "modulus::modulus")
    components["modulus"].libs = ["modulus"]
    components["modulus"].requires = ["boost::json"]
    components["boost_json"].set_property("cmake_target_name", "modulus::boost_json")
    components["boost_json"].requires = ["boost::json"]
    if self.options.with_openssl:
      components["openssl"].set_property("cmake_target_name", "modulus::openssl")
      components["openssl"].requires = ["openssl::ssl", "openssl::crypto"]
    if self.options.with_sqlite:
      components["sqlite"].set_property("cmake_target_name", "modulus::sqlite")
      components["sqlite"].requires = ["sqlite3::sqlite"]
    if self.options.with_raylib:
      components["raylib"].set_property("cmake_target_name", "modulus::raylib")
      components["raylib"].requires = ["raylib::raylib"]
      if self.settings.os == "Linux":
        components["raylib"].system_libs = [
          "EGL",
          "GLESv2",
          "drm",
          "gbm",
          "atomic",
          "dl",
        ]
    if self.options.with_miniaudio:
      components["miniaudio"].set_property("cmake_target_name", "modulus::miniaudio")
      components["miniaudio"].requires = ["miniaudio::miniaudio"]
