#!/usr/bin/env python3

""" Using VIM to generate textfiles which are directly anki-importable.  """

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import argparse
from os import getenv
from os.path import join as path_join, expanduser, abspath

import ankivim
from ankivim.cards import create_card


def main():
    parser = argparse.ArgumentParser(
        description="Use VIM (or the editor of your choice) "
                    "to comfortably and quickly write textfiles "
                    "which are directly anki-importable. "
    )

    parser.add_argument(
        "deck", help='Name of the deck to write cards for.'
    )

    parser.add_argument(
        "--deckpath",
        help="Custom location for this deck (instead of `$HOME/.ankvim/decks/DECKNAME`)",
        default=None,
    )

    parser.add_argument(
        "-e", "--editor",
        help="Force using EDITOR, overwriting default behaviours.\n"
             "If set to anything else but vim(1), '--editor-args' must "
             "also be specified.\n"
             "Default behaviour for editor choices:\n"
             "1. read environment variable $EDITOR \n"
             "2. fall back to vim(1) if $EDITOR is not set.",
        action="store", dest="editor", default=None
    )

    parser.add_argument(
        "--editor-args",
        help="Arguments to pass to the editor upon calling.\n"
             'Expected format:"--arg1 VALUE1 --arg2 VALUE2 ..."\t\n'
             "(mind the quotes!)",
        action="store", dest="editor_args", default=None
    )

    args = parser.parse_args()

    if args.editor is None:
        editor = getenv("EDITOR", "vim")
    else:
        editor = args.editor

    if editor in ("vim", "nvim", "neovim"):
        # for various vim flavors, have default editor args ready to use.
        if not args.editor_args:
            # editor args below target vim 7.4, overwrite for other editor choices.
            editor_args = (
                # set cursor below headers
                "-c {}".format(r'/\v\%\n\zs(^$|^[^\%]{1}.*$)'),
                # use anki_vim snippets
                "-c set filetype=anki_vim",
                # latex syntax highlighting
                "-c set syntax=tex",
                # load anki-vim snippets for this buffer
                '-c let b:UltiSnipsSnippetDirectories=["UltiSnips", "{snippet_directory}"]'.format(
                    snippet_directory=abspath(path_join(
                        ankivim.__path__[0], "UltiSnips",))),
            )
        else:
            editor_args = args.editor_args.split(",")
    else:
        if not args.editor_args:
            editor_args = ()
        else:
            editor_args = args.editor_args.split(",")

    deckpath = args.deckpath or path_join(expanduser("~"), ".ankivim/decks", args.deck)

    while create_card(deckpath=deckpath, editor=editor, editor_args=editor_args):
        # If a card is closed without content or changes, stop
        pass


if __name__ == '__main__':
    main()
