import re

from dev_tools.utility.path import ROOT

PKG_AUTHORS = {
    "com.twelvemonkeys.common:common-image": "Harald Kuhr",
    "com.twelvemonkeys.common:common-io": "Harald Kuhr",
    "com.twelvemonkeys.common:common-lang": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-batik": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-bmp": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-clippath": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-core": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-hdr": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-iff": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-jpeg": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-metadata": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-pcx": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-pict": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-pnm": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-psd": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-sgi": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-tga": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-tiff": "Harald Kuhr",
    "com.twelvemonkeys.imageio:imageio-webp": "Harald Kuhr",
    "com.twelvemonkeys.servlet:servlet": "Harald Kuhr",
    "commons-io:commons-io": "Apache Software Foundation",
    "commons-logging:commons-logging": "Apache Software Foundation",
    "io.javalin:javalin": "David Åse",
    "javax.servlet:javax.servlet-api": "Oracle",
    "net.coobird:thumbnailator": "Chris Kroells",
    "net.sourceforge.argparse4j:argparse4j": "Tatsuhiro Tsujikawa, Johan Stuyts et al.",
    "org.apache.pdfbox:fontbox": "Apache Software Foundation",
    "org.apache.pdfbox:pdfbox": "Apache Software Foundation",
    "org.apache.pdfbox:pdfbox-debugger": "Apache Software Foundation",
    "org.apache.pdfbox:pdfbox-tools": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-anim": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-awt-util": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-bridge": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-codec": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-constants": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-css": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-dom": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-ext": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-gvt": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-i18n": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-parser": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-rasterizer": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-script": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-shared-resources": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-svg-dom": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-svggen": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-svgrasterizer": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-transcoder": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-util": "Apache Software Foundation",
    "org.apache.xmlgraphics:batik-xml": "Apache Software Foundation",
    "org.apache.xmlgraphics:xmlgraphics-commons": "Apache Software Foundation",
    "org.eclipse.jetty:jetty-client": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-http": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-io": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-security": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-server": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-servlet": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-util": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-util-ajax": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-webapp": "Webtide, LLC.",
    "org.eclipse.jetty:jetty-xml": "Webtide, LLC.",
    "org.eclipse.jetty.websocket:websocket-api": "Webtide, LLC.",
    "org.eclipse.jetty.websocket:websocket-client": "Webtide, LLC.",
    "org.eclipse.jetty.websocket:websocket-common": "Webtide, LLC.",
    "org.eclipse.jetty.websocket:websocket-server": "Webtide, LLC.",
    "org.eclipse.jetty.websocket:websocket-servlet": "Webtide, LLC.",
    "org.jetbrains:annotations": "JetBrains s.r.o.",
    "org.jetbrains.kotlin:kotlin-stdlib": "JetBrains s.r.o.",
    "org.jetbrains.kotlin:kotlin-stdlib-common": "JetBrains s.r.o.",
    "org.jetbrains.kotlin:kotlin-stdlib-jdk7": "JetBrains s.r.o.",
    "org.jetbrains.kotlin:kotlin-stdlib-jdk8": "JetBrains s.r.o.",
    "org.slf4j:slf4j-api": "QOS.ch Sarl (Switzerland)",
    "org.slf4j:slf4j-simple": "QOS.ch Sarl (Switzerland)",
    "xalan:serializer": "Apache Software Foundation",
    "xalan:xalan": "Apache Software Foundation",
    "xml-apis:xml-apis": "Apache Software Foundation",
    "xml-apis:xml-apis-ext": "Apache Software Foundation",
}


def get_frozen_java_dependencies():
    d = dict()
    gradle_lockfiles = [(ROOT / "clarabot_nano" / "picoture" / "gradle.lockfile")]
    req_expr_re = re.compile(r"^([\w.:-]+):([\d\w.-]+)$")
    for item in gradle_lockfiles:
        reqs_lines = [line.partition("#")[0].partition("=")[0].strip() for line in item.read_text().splitlines()]
        reqs_lines = [line for line in reqs_lines if line]
        for reqs_line in reqs_lines:
            if reqs_line == "empty":
                continue
            m = req_expr_re.match(reqs_line)
            if not m:
                raise Exception(f"REQ expr: {reqs_line}")
            pkg_name = m.group(1)
            if pkg_name in d and d[pkg_name] != m.group(2):
                raise Exception(f"REQ {pkg_name} version conflicts")
            d[pkg_name] = m.group(2)
    missing_authors_for = set(d.keys()).difference(PKG_AUTHORS.keys())
    if missing_authors_for:
        print("Missing authors for")
        for x in sorted(missing_authors_for):
            print(x)
        raise Exception("Missing authors")
    lines = []
    for name in sorted(d.keys()):
        lines.append((name, d[name], PKG_AUTHORS[name]))
    return lines


def get_frozen_java_dependencies_tablines():
    return "\n".join(["\t".join(line) for line in get_frozen_java_dependencies()])
