from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.scm import Git
from conan.tools.files import patch
import os


class maplibre_nativeRecipe(ConanFile):
    name = "maplibre-native"
    package_type = "library"
    user = "aurora"

    # Optional metadata
    license = "BSD-2-Clause license"
    author = "Vladislav Larionov <vladislav.larionov@fruct.org>"
    url = "https://gitverse.ru/auroraos/mirror_maplibre-native"
    description = "MapLibre Native - Interactive vector tile maps for iOS, Android and other platforms."
    topics = ("maps", " maplibre")

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


    exports = '*.patch'

    def source(self):
        git = Git(self)
        git.clone(url="https://github.com/maplibre/maplibre-native.git", target=".")
        commit = self.conan_data["sources"][self.version]["commit"]
        git.checkout(commit=commit)
        git.run("submodule update --init --recursive")

    def layout(self):
        cmake_layout(self)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        tc.cache_variables["MBGL_QT_INSIDE_PLUGIN"] =  "OFF"
        tc.cache_variables["MBGL_WITH_QT"] = "ON"
        tc.cache_variables["MBGL_QT_WITH_INTERNAL_ICU"] = "ON"
        tc.cache_variables["MBGL_QT_LIBRARY_ONLY"] = "ON"
        tc.cache_variables["MBGL_QT_STATIC"] = "OFF"
        tc.cache_variables["MBGL_WITH_WERROR"] = "OFF"
        tc.cache_variables["MBGL_QT_WITH_HEADLESS"] = "ON"
        tc.generate()

    def build(self):
        static_cast_patch_file = os.path.join(self.recipe_folder, "patches", "static-cast.patch")
        patch(self, patch_file=static_cast_patch_file)
        cmake = CMake(self)
        cmake.configure(cli_args=["-GNinja"])
        cmake.build()

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

    def package_info(self):
        self.cpp_info.libs = ["QMapLibreGL"]
