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 copy
import os


class MfwOfflineGeocodingRecipe(ConanFile):
    name = "mfw-offline-geocoding"
    user = "aurora"
    package_type = "library"

    # Optional metadata
    license = "3-Clause BSD"
    author = "Vladislav Larionov <vladislav.larionov@fruct.org>"
    url = "https://hub.mos.ru/auroraos/libraries/mfw-offline-geocoding"
    description = "The MfwOfflineGeocoding plugin provides complete address information, including " \
        "a structured full address, postal code and other information."
    topics = ("mfw", "geocoding", "maps")

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

    def requirements(self):
        self.requires("postal/1.1@aurora")
        self.requires("marisa/0.2.6@aurora")
        self.requires("geocoder-nlp-tkrzw/1.0@aurora", transitive_headers=True)
        self.requires("tkrzw/1.72.0@aurora")

    def source(self):
        git = Git(self)
        git.clone(url="https://gitlab.com/omprussia/libraries/mfw-offline-geocoding.git", target=".")
        branch = self.conan_data["sources"][self.version]["branch"]
        git.checkout(commit=branch)

    def layout(self):
        cmake_layout(self)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        tc.cache_variables['MFW_INSTALL_PATH'] = os.path.join(self.package_folder, "qmlplugins", "MfwOfflineGeocoding")
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()
        copy(self, "LICENSE*", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))

    def package_info(self):
        self.cpp_info.libs = ["MfwOfflineGeocoding"]
        self.cpp_info.includedirs = []
        self.cpp_info.libdirs = ["qmlplugins/MfwOfflineGeocoding"]
