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


class OpenCVCoreImgCodecsRecipe(ConanFile):
    name = "opencv-core-imgcodecs"
    user = "aurora"
    package_type = "library"

    # Optional metadata
    license = "Apache-2.0"
    author = "Andrey Vasilyev <andrey.vasilyev@fruct.org>"
    url = "TBD"
    description = "OpenCV (Open Source Computer Vision Library) build with core and imgcodecs modules"
    topics = ("computer-vision", "image-processing")

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


    def config_options(self):
        if self.settings.os == "Windows":
            self.options.rm_safe("fPIC")


    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")


    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,imgcodecs"
        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"] = False
        tc.cache_variables["BUILD_PROTOBUF"] = False
        tc.cache_variables["OPENCV_DNN_OPENCL"] = False
        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"]
        self.cpp_info.includedirs = ["include/opencv4"]

