# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
# 
# Copyright (c) 2018 Philippe Faist
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import sys
import fileinput
import argparse
import logging


from .. import latexwalker
from ..latex2text import LatexNodes2Text, _strict_latex_spaces_predef
from ..version import version_str


def main(argv=None):

    if argv is None:
        argv = sys.argv[1:]

    parser = argparse.ArgumentParser(prog='latex2text', add_help=False)

    codegroup = parser.add_argument_group("Input options")

    codegroup.add_argument('--code', '-c', action='store', default=None, metavar="LATEX_CODE",
                           help="Convert the given LATEX_CODE to unicode text instead of reading "
                           "from FILE or standard input.  You cannot specify FILEs if you use this "
                           "option, and any standard input is ignored.")


    codegroup.add_argument('files', metavar="FILE", nargs='*',
                           help="Input files to read LaTeX code from. If no FILE(s) is/are specified, "
                           "LaTeX code is read from standard input unless --code is specified")



    group = parser.add_argument_group("LatexWalker options")

    group.add_argument('--parser-keep-inline-math', action='store_const', const=True,
                       dest='parser_keep_inline_math', default=None,
                       help=argparse.SUPPRESS)
    group.add_argument('--no-parser-keep-inline-math', action='store_const', const=False,
                       dest='parser_keep_inline_math',
                       help=argparse.SUPPRESS)

    group.add_argument('--tolerant-parsing', action='store_const', const=True,
                       dest='tolerant_parsing', default=True)
    group.add_argument('--no-tolerant-parsing', action='store_const', const=False,
                       dest='tolerant_parsing',
                       help="Tolerate syntax errors when parsing, and attempt to continue (default yes)")

    # I'm not sure this flag is useful and if it should be exposed at all.
    # Accept it, but make it hidden.
    parser.add_argument('--strict-braces', action='store_const', const=True,
                        dest='strict_braces', default=False,
                        help=argparse.SUPPRESS)
    parser.add_argument('--no-strict-braces', action='store_const', const=False,
                        dest='strict_braces',
                        #help="Report errors for mismatching LaTeX braces (default no)"
                        help=argparse.SUPPRESS)

    group = parser.add_argument_group("LatexNodes2Text options")

    group.add_argument('--text-keep-inline-math', action='store_const', const=True,
                       dest='text_keep_inline_math', default=None,
                       help=argparse.SUPPRESS)
    group.add_argument('--no-text-keep-inline-math', action='store_const', const=False,
                       dest='text_keep_inline_math',
                       help=argparse.SUPPRESS)

    group.add_argument('--math-mode', action='store', dest='math_mode',
                       choices=['text', 'with-delimiters', 'verbatim', 'remove'],