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


class QtRecipe(ConanFile):
    name = "qt"
    version = "6.5.3"
    description = "Qt is a cute framework for creating graphical user interfaces"
    author = "Gernot Walzl"
    homepage = "https://www.qt.io/"
    url = "https://gernot-walzl.at/index.php?nav=C%2B%2B%2FQt"
    package_type = "library"
    settings = "os", "compiler", "build_type", "arch"
    _modules = [
        "qt3d",
        "qt5compat",
        "qtactiveqt",
        "qtbase",
        "qtcharts",
        "qtcoap",
        "qtconnectivity",
        "qtdatavis3d",
        "qtdeclarative",
        "qtdoc",
        "qtgrpc",
        "qthttpserver",
        "qtimageformats",
        "qtlanguageserver",
        "qtlocation",
        "qtlottie",
        "qtmqtt",
        "qtmultimedia",
        "qtnetworkauth",
        "qtopcua",
        "qtpositioning",
        "qtquick3d",
        "qtquick3dphysics",
        "qtquickeffectmaker",
        "qtquicktimeline",
        "qtremoteobjects",
        "qtscxml",
        "qtsensors",
        "qtserialbus",
        "qtserialport",
        "qtshadertools",
        "qtspeech",
        "qtsvg",
        "qttools",
        "qttranslations",
        "qtvirtualkeyboard",
        "qtwayland",
        "qtwebchannel",
        "qtwebengine",
        "qtwebsockets",
        "qtwebview"
    ]
    options = {
        "shared": [True, False],
        "build_tests": [True, False],
        "build_examples": [True, False],
        **{module: [True, False] for module in _modules}
    }
    default_options = {
        "shared": True,
        "build_tests": False,
        "build_examples": False,
        **{module: False for module in _modules},
        "qtbase": True,
        "qt5compat": True,         # required to build qtcreator 12
        "qtdeclarative": True,     # required to build qtcreator 12
        "qtimageformats": True,    # optional dependency of qtdeclarative
        "qtlanguageserver": True,  # optional dependency of qtdeclarative
        "qtshadertools": True,     # optional dependency of qtdeclarative
        "qtsvg": True,             # optional dependency of qtdeclarative
        "qttools": True,           # includes designer
        "qtactiveqt": True,        # optional dependency of qttools
        "qtwayland": True
    }

    def source(self):
        sourcefile = f"qt-everywhere-src-{self.version}.tar.xz"
        download = f"https://download.qt.io/archive/qt/6.5/{self.version}/single/{sourcefile}"
        md5sum = "755db0527410df135609b51defa1a689"
        get(self, download, md5=md5sum, strip_root=True)

    def layout(self):
        cmake_layout(self, src_folder="src")

    def generate(self):
        tc = CMakeToolchain(self, generator="Ninja")
        tc.generate()

    def build(self):
        args = [f"-prefix {self.package_folder}"]

        if self.options.shared:
            args.append("-shared")
        else:
            args.append("-static")

        if self.settings.build_type == "Debug":
            args.append("-debug")
        elif self.settings.build_type == "Release":
            args.append("-release")
        elif self.settings.build_type == "RelWithDebInfo":
            args.append("-release")
            args.append("-force-debug-info")

        if not self.options.build_tests:
            args.append("-nomake tests")
        if not self.options.build_examples:
            args.append("-nomake examples")

        for module in self._modules:
            if not getattr(self.options, module):
                args.append("-skip " + module)

        configure = f"{self.source_folder}/configure {' '.join(args)}"
        self.run(configure)

        cmake = CMake(self)
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_find_mode", "none")
        self.cpp_info.builddirs.append(os.path.join("lib", "cmake", "Qt6"))