#!/usr/bin/env python3
"""
@file
@defgroup rtctk_clients_guiWidgets RTCTK Interfaces GUI Widgets
@ingroup rtctk_clients

@brief Control button widgets for each MAL interface used in the Control and Monitoring Tool.

The MAL interfaces are the standard SRTC component interfaces, such as FuncCmds, LoopCmds, etc,
found in rtctkif.xml.

@copyright
  SPDX-FileCopyrightText: 2024-2026 European Southern Observatory (ESO) @n
  SPDX-License-Identifier: LGPL-3.0-only
"""

import waflib
from wtools import module

# The declare_pyqtprogram instruction will automatically find
# the QT Resources, and the UI files, and run the necessary
# commands to produce the python versions of them.


def options(opt):
    opt.load("compiler_c python")


def configure(conf):
    conf.load("compiler_c python")


def build(bld: waflib.Build.BuildContext):
    """Builds a python qt package, and install the package and its dist-info metadata"""
    pkg_name = "rtctk.ctrlmontool.gui.widgets"
    # We do not use the project version, as it may come with the release part.
    # pip enforces release to conform to certain pattern. pre1 will always become rc1.
    # which can make deployment confusing, and would require transformation of the version.
    # Instead, we rely on bumpversion to produce the version string without release part.
    prj_version = "6.0.0"
    # NOTE: cnf.env.PROJECT_VERSION gives you the project version, but it comes with the release.

    # We create a python package using wtools
    bld.auto_pypackage(
        target="rtctk",
        with_ut_lib="pytest",
        with_unit_test=True,
        with_doctest=False,
        # This still needs to reference the version-specific pyqt5 because the
        # "feature" in waf is still called that way, but it also works for qt6.
        # There is no pyqt6 feature and using it would emit a warning from waf.
        features="pyqt5 buildcopy",
        use=["componentFramework.interfaces.rtctkif-python"],
        ut_attrs=dict(
            use=lambda pkgs, _: pkgs
            + [
                "componentFramework.pybindings",
            ]
        ),
    )

    # We determine the "sources" and "build sources" directory.
    build_dir = bld.path.find_or_declare("src").abspath()
    sources_dir = bld.path.find_node("src").abspath()

    # We build a list of every file in the dist directory, and save it into files_dist.
    dist_list = [
        "direct_url.json",
        "entry_points.txt",
        "INSTALLER",
        "METADATA",
        "RECORD",
        "REQUESTED",
        "top_level.txt",
        "WHEEL",
    ]
    files_dist = [
        bld.path.find_or_declare(
            "src/lib/python{}/site-packages/{}-{}.dist-info/{}".format(
                bld.env.PYTHON_VERSION, pkg_name, prj_version, n
            )
        )
        for n in dist_list
    ]

    copy_pyproject = bld(
        features="subst", source="pyproject.toml", target="pyproject.toml", is_copy=True
    )
    copy_setup = bld(features="subst", source="setup.py", target="setup.py", is_copy=True)

    # We create a new Task that will run a custom command.
    # The command is pip install, and puts the products in the build directory.
    pip_install = bld(
        rule="{} -m pip install --no-build-isolation --ignore-installed --find-links /usr/share/python-wheels --no-deps --prefix {} {}/../".format(
            "${PYTHON}",
            build_dir,
            build_dir,
        ),
        target=files_dist,
        cwd=build_dir,
        run_after=[copy_pyproject, copy_setup],
    )

    dir_dist_name = ""
    # We are only interest in the "dist" directory, the one that contains the metadata of pip.
    # In particular, it contians entry_points.txt, which carries the plugin information.
    dir_dist = bld.path.find_or_declare("src").ant_glob(f"**/{pkg_name}*", dir=True, remove=False)
    if len(dir_dist) != 0:
        dir_dist_name = dir_dist[0].abspath().split("/")[-1]

    # We add an install task. This will install the list of files "files_dist" into the python
    # library directory.
    install_task = bld.install_files(
        dest="{}/lib/python{}/site-packages/{}".format(
            bld.env.PREFIX, bld.env.PYTHON_VERSION, dir_dist_name
        ),
        files=files_dist,
        run_after=[pip_install],
        postpone=True,
    )
