from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir
import os

required_conan_version = ">=1.60.0 <2.0 || >=2.0.5"


class opencvRecipe(ConanFile):
    name = "opencv-dnn-ml"
    package_type = "library"
    user = "aurora"

    # Optional metadata
    homepage = "https://opencv.org"
    author = "Daniil Markevich <d.markevich@omp.ru>"
    url = "https://gitverse.ru/auroraos/mirror_opencv"
    license = "Apache License 2.0"
    description = "OpenCV: Open Source Computer Vision Library"
    topics = ("opencv", "computer-vision", "image-processing",
              "deep-learning", "machine-learning")

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

    def layout(self):
        cmake_layout(self)

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        tc.cache_variables["BUILD_opencv_apps"] = False
        tc.cache_variables["BUILD_TESTS"] = False
        tc.cache_variables["BUILD_PERF_TESTS"] = False
        tc.cache_variables["BUILD_LIST"] = "core,dnn,ml,imgcodecs"
        tc.cache_variables["BUILD_opencv_dnn"] = True
        tc.cache_variables["WITH_EIGEN"] = True
        tc.cache_variables["WITH_VULKAN"] = True
        tc.cache_variables["WITH_V4L"] = False
        tc.cache_variables["WITH_FFMPEG"] = False
        tc.cache_variables["WITH_GSTREAMER"] = False
        tc.cache_variables["WITH_MSMF"] = False
        tc.cache_variables["WITH_DSHOW"] = False
        tc.cache_variables["WITH_AVFOUNDATION"] = False
        tc.cache_variables["VIDEOIO_ENABLE_PLUGINS"] = False
        tc.cache_variables["HIGHGUI_ENABLE_PLUGINS"] = False
        tc.cache_variables["WITH_PROTOBUF"] = True
        tc.cache_variables["BUILD_PROTOBUF"] = True
        tc.cache_variables["WITH_OPENCL"] = True
        tc.cache_variables["OPENCV_DNN_OPENCL"] = True
        tc.cache_variables["BUILD_opencv_ml"] = True
        tc.cache_variables["BUILD_JAVA"] = False
        tc.cache_variables["BUILD_FAT_JAVA_LIB"] = False
        tc.cache_variables["ENABLE_PYLINT"] = False
        tc.cache_variables["ENABLE_FLAKE8"] = False
        tc.cache_variables["BUILD_opencv_python2"] = False
        tc.cache_variables["BUILD_opencv_python3"] = False
        tc.cache_variables["WITH_CAROTENE"] = False
        tc.generate()

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

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

    def package_info(self):
        self.cpp_info.libs = ["opencv_core", "opencv_imgproc",
                              "opencv_imgcodecs", "opencv_dnn", "opencv_ml"]
        self.cpp_info.includedirs = ["include/opencv4"]
