import shutil
from pathlib import Path

from dev_tools.build.deps.jre import (
    build_deps_jre,
    get_java_version,
    cache_discover_java_modules_from_jars,
)
from dev_tools.build.deps.solr import cache_solr_release_package, build_deps_solr
from dev_tools.build.deps.tika import cache_tika_server, build_deps_tika

# detected by jdeps
DEPS_FROM_SOLR_AND_TIKA = {
    "java.base",
    "java.compiler",
    "java.datatransfer",
    "java.desktop",
    "java.instrument",
    "java.logging",
    "java.management",
    "java.management.rmi",
    "java.naming",
    "java.prefs",
    "java.rmi",
    "java.scripting",
    "java.security.jgss",
    "java.security.sasl",
    "java.sql",
    "java.xml",
    "java.xml.crypto",
    "jdk.javadoc",
    "jdk.management",
    "jdk.unsupported",
}


def build_deps(cache_dir: Path, deps_dir: Path, force: bool = False, skip_signing: bool = False):
    tika_dir_path, tika_version = cache_tika_server(cache_dir)
    solr_package_path, solr_version = cache_solr_release_package(cache_dir)
    java_version = get_java_version()

    fingerprint = (
        f"Clarabot Nano - dependency descriptor\n"
        f"Java:\n{java_version}\nSolr:\n{solr_version}\nTika:\n{tika_version}\n"
    )
    deps_fingerprint_path = deps_dir / ".build"
    rebuild = True
    if deps_fingerprint_path.is_file() and not force:
        cache_build_fingerprint = deps_fingerprint_path.read_text("utf-8")
        if cache_build_fingerprint == fingerprint:
            rebuild = False
    if rebuild:
        if deps_dir.exists():
            shutil.rmtree(deps_dir)
        deps_dir.mkdir(parents=True, exist_ok=True)
        # build dependencies ready for use
        tika_dir = build_deps_tika(deps_dir, tika_dir_path)
        solr_dir = build_deps_solr(deps_dir, solr_package_path)
        # build custom jre from system java
        jre_deps = set()
        jre_deps.update(
            cache_discover_java_modules_from_jars(cache_dir, solr_dir, "solr", solr_version, java_version, force)
        )
        jre_deps.update(
            cache_discover_java_modules_from_jars(cache_dir, tika_dir, "tika", tika_version, java_version, force)
        )
        assert (
            jre_deps == DEPS_FROM_SOLR_AND_TIKA
        ), f"extracted deps changed\n{sorted(jre_deps)}\n{sorted(DEPS_FROM_SOLR_AND_TIKA)}"
        # The modules jdeps finds is not complete. These are added by manually detected demand.
        # The modules that do not have a comment are included, because they sound relevant.
        jre_deps.update(
            [
                # "java.se",
                "jdk.charsets",  # Actually required by some Tika system (on Windows only?) for XML some docs.
                "jdk.localedata",
                # "jdk.naming.dns",
                # "jdk.naming.ldap",
                # "jdk.unsupported.desktop",
                "jdk.xml.dom",
                # "jdk.zipfs",
                # "org.openjsse",
                # These were from Tika 1.x, keep them for compatibility a little.
                "java.sql.rowset",
                "java.transaction.xa",
            ]
        )
        build_deps_jre(deps_dir, jre_deps, skip_signing=skip_signing)
        # finish
        deps_fingerprint_path.write_text(fingerprint, "utf-8")
