from conans import ConanFile, CMake, tools
import os
import shutil

required_conan_version = ">=1.43.0"


class FreeImageConan(ConanFile):
    name = "freeimage"
    description = "Open Source library project for developers who would like to support popular graphics image formats"\
                  "like PNG, BMP, JPEG, TIFF and others as needed by today's multimedia applications."
    homepage = "https://freeimage.sourceforge.io"
    url = "https://github.com/conan-io/conan-center-index"
    license = "FreeImage", "GPL-3.0-or-later", "GPL-2.0-or-later"
    topics = ("freeimage", "image", "decoding", "graphics")
    generators = "cmake", "cmake_find_package"
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
        "with_jpeg": [False, "libjpeg", "libjpeg-turbo"],
        "with_png": [True, False],
        "with_tiff": [True, False],
        "with_jpeg2000": [True, False],
        "with_openexr": [True, False],
        "with_eigen": [True, False],
        "with_webp": [True, False],
        "with_raw": [True, False],
        "with_jxr": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
        "with_jpeg": "libjpeg",
        "with_png": True,
        "with_tiff": True,
        "with_jpeg2000": True,
        "with_openexr": True,
        "with_eigen": True,
        "with_webp": True,
        "with_raw": True,
        "with_jxr": True,
    }

    _cmake = None

    @property
    def _source_subfolder(self):
        return "source_subfolder"

    @property
    def _build_subfolder(self):
        return "build_subfolder"

    def export_sources(self):
        self.copy("CMakeLists.txt")
        for patch in self.conan_data.get("patches", {}).get(self.version, []):
            self.copy(patch["patch_file"])

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

    def configure(self):
        tools.check_min_cppstd(self, "11")
        if self.options.shared:
            del self.options.fPIC
        self.output.warn("G3 plugin and JPEGTransform are disabled.")
        if self.options.with_jpeg is not None:
            if self.options.with_tiff:
                self.options["libtiff"].jpeg = self.options.with_jpeg

    def requirements(self):
        self.requires("zlib/1.2.11")
        if self.options.with_jpeg == "libjpeg":
            self.requires("libjpeg/9d")
        elif self.options.with_jpeg == "libjpeg-turbo":
            self.requires("libjpeg-turbo/2.1.2")
        if self.options.with_jpeg2000:
            self.requires("openjpeg/2.4.0")
        if self.options.with_png:
            self.requires("libpng/1.6.37")
        if self.options.with_webp:
            self.requires("libwebp/1.2.2")
        if self.options.with_openexr:
            self.requires("openexr/2.5.7")
        if self.options.with_raw:
            self.requires("libraw/0.20.2")
        if self.options.with_jxr:
            self.requires("jxrlib/cci.20170615")
        if self.options.with_tiff:
            self.requires("libtiff/4.3.0")

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

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.definitions["WITH_JPEG"] = self.options.with_jpeg != False
        self._cmake.definitions["WITH_OPENJPEG"] = self.options.with_jpeg2000
        self._cmake.definitions["WITH_PNG"] = self.options.with_png
        self._cmake.definitions["WITH_WEBP"] = self.options.with_webp
        self._cmake.definitions["WITH_OPENEXR"] = self.options.with_openexr
        self._cmake.definitions["WITH_RAW"] = self.options.with_raw
        self._cmake.definitions["WITH_JXR"] = self.options.with_jxr
        self._cmake.definitions["WITH_TIFF"] = self.options.with_tiff
        self._cmake.configure(build_dir=self._build_subfolder)
        return self._cmake

    def build(self):
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibPNG"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibTIFF4"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibOpenJPEG"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibJXR"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibWebP"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "LibRawLite"))
        tools.rmdir(os.path.join(self._source_subfolder, "Source", "OpenEXR"))

        for patch in self.conan_data.get("patches", {}).get(self.version, {}):
            tools.patch(**patch)
        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        cmake = self._configure_cmake()
        cmake.install()
        self.copy("license-fi.txt", dst="licenses", src=self._source_subfolder)
        self.copy("license-gplv3.txt", dst="licenses", src=self._source_subfolder)
        self.copy("license-gplv2.txt", dst="licenses", src=self._source_subfolder)

    def package_info(self):
        def imageformats_deps():
            components = []
            components.append("zlib::zlib")
            if self.options.with_jpeg:
                components.append("{0}::{0}".format(self.options.with_jpeg))
            if self.options.with_jpeg2000:
                components.append("openjpeg::openjpeg")
            if self.options.with_png:
                components.append("libpng::libpng")
            if self.options.with_webp:
                components.append("libwebp::libwebp")
            if self.options.with_openexr:
                components.append("openexr::openexr")
            if self.options.with_raw:
                components.append("libraw::libraw")
            if self.options.with_jxr:
                components.append("jxrlib::jxrlib")
            if self.options.with_tiff:
                components.append("libtiff::libtiff")
            return components

        self.cpp_info.names["pkg_config"] = "freeimage"
        self.cpp_info.names["cmake_find_package"] = "FreeImage"
        self.cpp_info.names["cmake_find_package_multi"] = "FreeImage"
        self.cpp_info.components["FreeImage"].libs = ["freeimage"]
        self.cpp_info.components["FreeImage"].requires = imageformats_deps()
        self.cpp_info.components["FreeImagePlus"].libs = ["freeimageplus"]
        self.cpp_info.components["FreeImagePlus"].requires = ["FreeImage"]

        if not self.options.shared:
            self.cpp_info.components["FreeImage"].defines.append("FREEIMAGE_LIB")

[((65, 8, 65, 42), 'conans.tools.check_min_cppstd', 'tools.check_min_cppstd', ({(65, 31, 65, 35): 'self', (65, 37, 65, 41): '"""11"""'}, {}), "(self, '11')", False, 'from conans import ConanFile, CMake, tools\n'), ((95, 8, 96, 70), 'conans.tools.get', 'tools.get', (), '', False, 'from conans import ConanFile, CMake, tools\n'), ((101, 22, 101, 33), 'conans.CMake', 'CMake', ({(101, 28, 101, 32): 'self'}, {}), '(self)', False, 'from conans import ConanFile, CMake, tools\n'), ((114, 20, 114, 76), 'os.path.join', 'os.path.join', ({(114, 33, 114, 55): 'self._source_subfolder', (114, 57, 114, 65): '"""Source"""', (114, 67, 114, 75): '"""LibPNG"""'}, {}), "(self._source_subfolder, 'Source', 'LibPNG')", False, 'import os\n'), ((115, 20, 115, 78), 'os.path.join', 'os.path.join', ({(115, 33, 115, 55): 'self._source_subfolder', (115, 57, 115, 65): '"""Source"""', (115, 67, 115, 77): '"""LibTIFF4"""'}, {}), "(self._source_subfolder, 'Source', 'LibTIFF4')", False, 'import os\n'), ((116, 20, 116, 81), 'os.path.join', 'os.path.join', ({(116, 33, 116, 55): 'self._source_subfolder', (116, 57, 116, 65): '"""Source"""', (116, 67, 116, 80): '"""LibOpenJPEG"""'}, {}), "(self._source_subfolder, 'Source', 'LibOpenJPEG')", False, 'import os\n'), ((117, 20, 117, 76), 'os.path.join', 'os.path.join', ({(117, 33, 117, 55): 'self._source_subfolder', (117, 57, 117, 65): '"""Source"""', (117, 67, 117, 75): '"""LibJXR"""'}, {}), "(self._source_subfolder, 'Source', 'LibJXR')", False, 'import os\n'), ((118, 20, 118, 77), 'os.path.join', 'os.path.join', ({(118, 33, 118, 55): 'self._source_subfolder', (118, 57, 118, 65): '"""Source"""', (118, 67, 118, 76): '"""LibWebP"""'}, {}), "(self._source_subfolder, 'Source', 'LibWebP')", False, 'import os\n'), ((119, 20, 119, 80), 'os.path.join', 'os.path.join', ({(119, 33, 119, 55): 'self._source_subfolder', (119, 57, 119, 65): '"""Source"""', (119, 67, 119, 79): '"""LibRawLite"""'}, {}), "(self._source_subfolder, 'Source', 'LibRawLite')", False, 'import os\n'), ((120, 20, 120, 77), 'os.path.join', 'os.path.join', ({(120, 33, 120, 55): 'self._source_subfolder', (120, 57, 120, 65): '"""Source"""', (120, 67, 120, 76): '"""OpenEXR"""'}, {}), "(self._source_subfolder, 'Source', 'OpenEXR')", False, 'import os\n'), ((123, 12, 123, 32), 'conans.tools.patch', 'tools.patch', ({}, {}), '(**patch)', False, 'from conans import ConanFile, CMake, tools\n')]