import sys
import os

from . import img_factory
from . import meme_db
from . import meme_otron
from . import VERSION

if __name__ == "__main__":
    meme_db.load_memes()
    img_factory.load_fonts()

    # TODO better arguments reading (-h, -o, -v)

    if len(sys.argv) <= 1 or sys.argv[1].lower().strip() == "help" or "-h" in sys.argv:
        print(f"Meme-Otron v{VERSION}"
              "python -m meme_otron -h\n"
              "python -m meme_otron (meme_id) \"[text 1]\" \"[text 2]\" ... > file.jpg\n"
              "python -m meme_otron -o file.jpg (meme_id) \"[text 1]\" \"[text 2]\" ...",
              file=sys.stderr)
        sys.exit(1)
    else:
        output_file = None
        if "-o" in sys.argv:
            i = sys.argv.index("-o")
            if len(sys.argv) >= i:
                output_file = sys.argv[i + 1]
                del sys.argv[i + 1]
            del sys.argv[i]
        img = meme_otron.compute(*sys.argv[1:])
        if img is None:
            proposal = meme_db.find_nearest(sys.argv[1])
            if proposal is not None:
                print(f"Did you mean '{proposal}'?", file=sys.stderr)
            sys.exit(1)
        if output_file is None:
            with os.fdopen(os.dup(sys.stdout.fileno())) as output:
                img.save(output, format="jpeg")
        else:
            try:
                img.save(output_file)
                print(f"Wrote '{output_file}'")
            except OSError as e:
                print(f"Cannot write '{output_file}': {e}", file=sys.stderr)
                sys.exit(1)
            except ValueError as e:
                print(f"Cannot write '{output_file}': {e}", file=sys.stderr)
                sys.exit(1)
