#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import re
import sys

from url2bib.core import (
    build_bibtex,
    create_bib_id,
    get_dblp_bibtexs,
    maybeprint,
    parse_bibtex,
    set_verbosity,
    url2bibtex,
)

VERBOSE = True
USE_DBLP = True


def use_dblp(bibdict: dict) -> dict:
    """
    Query DBLP for a publication matching the given title.

    Args:
        bibdict (dict): The BibTeX dictionary to be updated.

    Returns:
        dict: The updated bibdict if a match is found, or the input bibdict if no match is found.
    """

    print("Querying DBLP...")

    title = bibdict.get("title", "")
    if not title:
        return bibdict

    dblp_bibtexs = get_dblp_bibtexs(title)
    if not dblp_bibtexs:
        return bibdict

    # Prefer entries not mentioning CoRR / arXiv
    preferred = []
    deprioritized = []

    for bibtex_entry in dblp_bibtexs:
        if re.search(r"(corr|arxiv)", bibtex_entry, re.I):
            deprioritized.append(bibtex_entry)
        else:
            preferred.append(bibtex_entry)

    if len(preferred) + len(deprioritized) < 0:
        maybeprint("No matching publications found in DBLP")
        return bibdict

    if len(preferred) > 0:
        print("Choosing paper from venue.")
        chosen_bibtex = preferred[0]
    else:
        print("Choosing paper from arXiv.")
        chosen_bibtex = deprioritized[0]

    new_bibdict = parse_bibtex(chosen_bibtex)
    return new_bibdict


def main():
    set_verbosity(VERBOSE)

    parser = argparse.ArgumentParser(description="Convert URLs to BibTeX entries")
    parser.add_argument("url", type=str, help="The URL to fetch DOIs from.")
    args = parser.parse_args()

    if args.url:
        bibtex = url2bibtex(args.url)
        if bibtex:
            # Parse and rebuild to ensure consistent formatting
            bibdict = parse_bibtex(bibtex)

            # Try DBLP (optional)
            if USE_DBLP:
                bibdict = use_dblp(bibdict)

            bibdict["ID"] = create_bib_id(bibdict)
            print()
            print(build_bibtex(bibdict))
        else:
            print("No matching publications found", file=sys.stderr)
            exit(1)
    else:
        parser.print_help()
        exit(1)


if __name__ == "__main__":
    main()
