#!/usr/bib/env python
from __future__ import print_function
import sys
import bibtexparser
import argparse
import textwrap
# from sci2pdf.libgen import download_pdf_from_bibs, download_from_doi
# from sci2pdf.libgen import download_from_title
from scihub2pdf.scihub import download_pdf_from_bibs, download_from_doi
from scihub2pdf.scihub import download_from_title, download_from_arxiv
import re


def main():
    parser = argparse.ArgumentParser(
        prog="scihub2pdf",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent('''\
        SciHub to PDF
        ----------------------------------------------------
        Downloads pdfs via a DOI number, article title
        or a bibtex file, using the database of libgen(sci-hub).

        Given a bibtex file

        $ scihub2pdf -i input.bib

        Given a DOI number...

        $ scihub2pdf 10.1038/s41524-017-0032-0

        Given a title...

        $ scihub2pdf --title An useful paper

        Arxiv...

        $ scihub2pdf arxiv:0901.2686

        $ scihub2pdf --title arxiv:Periodic table for topological insulators

        ## Download from list of items

        Given a text file like

        ```
        10.1038/s41524-017-0032-0
        10.1063/1.3149495
        .....
        ```
        download all pdf's
        ```
        $ scihub2pdf -i dois.txt --txt
        ```
        Given a text file like

        ```
        Some Title 1
        Some Title 2
        .....
        ```
        download all pdf's
        ```
        $ scihub2pdf -i titles.txt --txt --title
        ```
        Given a text file like

        ```
        arXiv:1708.06891
        arXiv:1708.06071
        arXiv:1708.05948
        .....
        ```
        download all pdf's
        ```
        $ scihub2pdf -i arxiv_ids.txt --txt
        ```
       -----------------------------------------------------
            @author: Bruno Messias
            @email: messias.physics@gmail.com
            @telegram: @brunomessias
            @github: https://github.com/bibcure/sci2pdf
        ''')
    )
    parser.add_argument(
        "--input", "-i",
        dest="inputfile",
        type=argparse.FileType("r"),
        help="bibtex input file"
    )
    parser.add_argument(
        "--title", "-t",
        dest="title",
        action="store_true",
        help="download from title"
                        )
    parser.add_argument(
        "--uselibgen",
        dest="uselibgen",
        action="store_true",
        help="Use libgen.io instead sci-hub. Sci-hub has annoying captcha but is stable. Libgen has no captcha but is unstable."
                        )
    parser.add_argument(
        "--location", "-l",
        help="folder, ex: -l 'folder/'"
    )
    parser.add_argument(
        "--txt",
        action="store_true",
        help="Just create a file with DOI's or titles"
    )

    parser.set_defaults(title=False)
    parser.set_defaults(uselibgen=False)
    parser.set_defaults(txt=False)
    parser.set_defaults(location="")

    args = parser.parse_known_args()
    title_search = args[0].title
    is_txt = args[0].txt
    use_libgen = args[0].uselibgen
    inline_search = len(args[1]) > 0
    location = args[0].location

    if use_libgen:
        print("\n\t Using Libgen.\n")
    else:
        print("\n\t Using Sci-Hub.\n")

    if inline_search:
        value = " ".join(args[1])
        is_arxiv = bool(re.match("arxiv:", value, re.I))
        if is_arxiv:
            field = "ti" if title_search else "id"
            download_from_arxiv(value, field, location)
        elif title_search:
            download_from_title(value, location, use_libgen)
        else:
            download_from_doi(value, location, use_libgen)
    else:
        if is_txt:
            file_values = args[0].inputfile.read()
            for value in file_values.split("\n"):
                is_arxiv = bool(re.match("arxiv:", value, re.I))
                if value != "":
                    if is_arxiv:
                        field = "ti" if title_search else "id"
                        download_from_arxiv(value, field, location)
                    elif title_search:
                        download_from_title(value, location, use_libgen)
                    else:
                        download_from_doi(value, location, use_libgen)

        else:
            bibtex = bibtexparser.loads(args[0].inputfile.read())
            bibs = bibtex.entries
            if len(bibs) == 0:
                print("Input File is empty or corrupted.")
                sys.exit(1)

            download_pdf_from_bibs(bibs, location, use_libgen)


if __name__ == "__main__":
    main()
