# Copyright (c) 2021 Trelent Inc.

def get_python_indentation(lines, function_name, function_argument_count):
    """
    Returns the indentation level of a Python function's docstring.

    Parameters:

        lines (list): A list of strings, each representing a line from the file.

        function_name (str): The name of the function whose docstring indentation we are looking for.

        function_argument_count (num): The number of arguments this function contains.


    Generated by Trelent
    """

    # Constants
    def_str = "def " + function_name + "("
    end_str = "):"

    # Main loop
    i = 0
    start_index = 0
    check_multi = False
    while i < len(lines):

        # Check if we are in a multi-line definition
        if(check_multi is True):
            if(end_str in lines[i]):

                # Combine lines from start to end
                combined_definition = '\n'.join(lines[start_index:i])
                native_args = len(combined_definition.split(','))

                # Ensure we are within index bounds and are not in a duplicate function
                if(native_args == function_argument_count and i+1 < len(lines)):
                    return len(lines[i+1]) - len(lines[i+1].lstrip())
        else:
            # Look for single-line definition
            if(def_str in lines[i]):
                # Now check if we need more lines
                if(end_str in lines[i]):
                    
                    # Compute the number of arguments in this function
                    native_args = len(lines[i].split(','))

                    # Ensure we are within index bounds and are not in a duplicate function
                    if(native_args == function_argument_count and i+1 < len(lines)):
                        return len(lines[i+1]) - len(lines[i+1].lstrip())
                else:
                    # Set our multi-line flag
                    start_index = i
                    check_multi = True
        i += 1

    return -1

def get_javascript_indentation(lines, function_name):
    """
    Returns the indentation level of a Javascript function's docstring.

    Parameters:

        lines (list): A list of strings, each representing a line from the file.

        function_name (str): The name of the function whose docstring indentation we are looking for.


    Generated by Trelent
    """

    # Function signature
    def_str = "function " + function_name + "("

    # Main loop
    i = 0
    while i < len(lines):
        if(def_str in lines[i]):
            return len(lines[i]) - len(lines[i].lstrip())
        i += 1

    return -1

def get_indentation(lines, function_name, function_argument_count, language_id):
    if(language_id == "python"):
        return get_python_indentation(lines, function_name, function_argument_count)
    elif(language_id == "javascript"):
        return get_javascript_indentation(lines, function_name)
    #elif(language_id == "java"):
    #    return get_java_indentation(lines, function_name, function_argument_count)
    else:
        # Default to python
        return get_python_indentation(lines, function_name, function_argument_count)