from tkinter import messagebox
import textwrap
import globalVariables
from globalVariables import windows
debugPrintLevel = 0  # controls how much debug printing we do. 0 = none, 10 = a lot
indentLevelInc = 4  # number of spaces to add for each indent level
_errorCount = 0  # Used to count the number of errors in a test.
_totalErrorCount = 0  # Used to count the total number of errors since beginning to read input spreadsheet.
# Consider using the Python logger to handle printing as described here:
# https://docs.python.org/3.1/library/logging.html#basic-example
# I haven't done this yet because the output shows up in red on the PyCharm console and it doesn't give
# me an easy way to set the fine print levels I use for debug printing.


def outputError(errorString: str, countThisError: bool = True):
    """
    Output an error message string and count the total number of times errors occurred.
    :param errorString: str: The string to output. There are 2 special values that are used to control the
        counting of errors.
            "zeroErrorCount" will zero the count of the number of times this routine has been called.
            "printErrorCount" will print the count of the number of times this routine has been called.
    :param countThisError: Optional parameter. Set to false to just print errorString in red but don't count
            it as an error.
    :return: number of errors since we last call to zeroErrorCount
    """
    global _errorCount
    global _totalErrorCount

    if errorString == 'zeroErrorCount':
        _errorCount = 0
    elif errorString == 'zeroTotalErrorCount':
        _errorCount = 0
        _totalErrorCount = 0
    elif errorString == 'printErrorCount':
        line = f'Test completed with {_errorCount} errors.'
        if _errorCount == 0:
            windows[0].addLine(line)
        else:
            windows[0].addLine(line, 'red')
        line = f'Total errors since starting to read input spreadsheet: {_totalErrorCount}.'
        if _totalErrorCount == 0:
            windows[0].addLine(line)
        else:
            windows[0].addLine(line, 'red')
    else:
        if globalVariables.debugging:
            messagebox.showinfo(title='Error',
                                message=errorString)
        else:
            my_wrap = textwrap.TextWrapper(width=95)
            wrap_list = my_wrap.wrap(text=errorString)
            myIndentLevel = 0
            for line in wrap_list:
                windows[0].addLine(myIndentLevel * ' ' + line, 'red')
                myIndentLevel = 1

        if countThisError:
            _errorCount += 1
            _totalErrorCount += 1
    return _errorCount


def debugPrint(debugPrintString: str):
    """Output a string useful for debugging"""
    windows[0].addLine('DEBUG: ' + debugPrintString)


def testPrint(indentLevel: int, outputString: str, makeBold: bool = False):
    """
    Print out a string that describes what is being done.
    :param indentLevel: number of spaces to indent, except -1 means don't print at all.
    :param outputString: the string to output
    :param makeBold: if true, format the outputString in bold font
    :return:
    """
    boldString = 'none'
    if makeBold:
        boldString = 'bold'
    if indentLevel == 0 or outputString == '':
        windows[0].addLine(' ')
    elif indentLevel < 0 or outputString is None:
        return
    if outputString[0:4] == '____':  # This is a command we may need to execute in the debugger.
        myWidth = 999  # force all the text to be on one line so it can be parsed by the debugger.
    else:
        myWidth = 95  # allow non-commands (primarily comments) to wrap to make them more readable.
    my_wrap = textwrap.TextWrapper(width=myWidth - indentLevel)
    wrap_list = my_wrap.wrap(text=outputString)
    myIndentLevel = indentLevel
    for line in wrap_list:
        windows[0].addLine(myIndentLevel * ' ' + line, boldString)
        myIndentLevel = indentLevel + 1
