import os

from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, rmdir
from conan.tools.gnu import PkgConfigDeps
from conan.tools.scm import Git

class LibreOfficeConan(ConanFile):
    name = "libreoffice"
    version = "24.2.0" 
    package_type = "library"
    user = "aurora"
    license = "MPL-2.0"
    author = "The LibreOffice Community"
    url = "https://github.com/LibreOffice/core"
    description = "LibreOffice core libraries"
    topics = ("office", "document", "libreoffice", "gui")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True, False],
    }
    default_options = {
        "shared": True,
    }

    def requirements(self):
        self.requires("icu/73.2@aurora")
        self.requires("libpng/1.6.44@aurora")
        self.requires("freetype/2.13.2@aurora")
        self.requires("harfbuzz/12.3.0@aurora")
        self.requires("libjpeg/9e@aurora")
        
        self.requires("zlib/1.3.1@aurora")
        self.requires("expat/2.7.4@aurora")
        self.requires("boost/1.84.0@aurora")

    def layout(self):
        cmake_layout(self, src_folder=".")

    def export_sources(self):
        export_conandata_patches(self)

    def source(self):
        git = Git(self)
        git.clone(url="https://github.com/LibreOffice/core.git", target=self.source_folder)
        git.checkout(commit="libreoffice-24.2.0.4")  # adjust tag/commit as required
        # LibreOffice uses submodules for many third‑party components.
        self.run("git submodule update --init --recursive", cwd=self.source_folder)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        pkg_deps = PkgConfigDeps(self)
        pkg_deps.generate()
        tc = CMakeToolchain(self)
        tc.cache_variables["BUILD_SHARED_LIBS"] = self.options.shared
        # Disable building the full office suite – we only need the core libraries for the examples.
        tc.cache_variables["BUILD_OFFICE"] = False
        tc.cache_variables["ENABLE_DEBUG"] = False
        tc.generate()

    def build(self):
        apply_conandata_patches(self)
        cmake = CMake(self)
        # Use the default CMake preset if present, otherwise fall back to a simple configure.
        cmake.configure()
        cmake.build()

    def package(self):
        copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
        cmake = CMake(self)
        cmake.install()
        # Remove pkg‑config files that are not needed for downstream consumption.
        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))

    def package_info(self):
        # The core libraries are exposed via the "libreoffice" pkg‑config name.
        self.cpp_info.set_property("pkg_config_name", "libreoffice")
        # LibreOffice produces many static/shared libraries; we expose the most commonly used one.
        self.cpp_info.libs = ["LibreOfficeKit"]
        # Header locations – the core include directory and a few important sub‑folders.
        self.cpp_info.includedirs = [
            "include",
            "include/LibreOfficeKit",
            "include/sal",
            "include/comphelper",
        ]
        if self.settings.os == "Linux":
            self.cpp_info.system_libs = ["dl", "pthread"]
        if self.settings.os == "Windows":
            self.cpp_info.system_libs = ["ws2_32", "dbghelp"]
