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 MfwOfflineRoutingRecipe(ConanFile):
    name = "mfw-offline-routing"
    package_type = "library"
    user = "aurora"

    # Optional metadata
    license = "3-Clause BSD"
    author = "Vladislav Larionov <vladislav.larionov@fruct.org>"
    url = "https://hub.mos.ru/auroraos/libraries/mfw-offline-routing"
    description = "The MfwOfflineRouting plugin provides data for constructing a route based on coordinates," \
        "with hints for actions at path nodes, taking into account the type of transport and other settings"
    topics = ("mfw", "routing", "maps")

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

    def requirements(self):
        self.requires("protobuf/3.18.3@aurora")
        self.requires("valhalla/3.4.0@aurora")

    def source(self):
        git = Git(self)
        git.clone(url="https://gitlab.com/omprussia/libraries/mfw-offline-routing.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", "MfwOfflineRouting")
        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 = ["MfwOfflineRouting"]
        self.cpp_info.includedirs = []
        self.cpp_info.libdirs = ["qmlplugins/MfwOfflineRouting"]
