import random


def generate_problems(number_of_problems=2, maximum_integer=50, problem_type='Addition', number_of_pages=1):
    """
    Generates random example math problems in latex format for practicing Addition, Subtraction, or Multiplication
    :param number_of_problems: defines how many problems to generate
    :param maximum_integer: defines the maximum integer possible during the generation of problems
    :param problem_type: type of problems to generate. options are Addition, Subtraction, or Multiplication
    :return: contents of the latex document as a string
    """
    operator = {'Addition': r'+',
                'Subtraction': r'-',
                'Multiplication': r'\times'}

    lines = [r'\documentclass{article}',
             r'\usepackage{amsmath}',
             r'\usepackage{amsfonts}',
             r'\usepackage{amssymb}',
             r'\pagenumbering{gobble}',
             r'\usepackage{multicol}',
             r'\begin{document}']

    for index, page in enumerate(range(number_of_pages)):
        lines.append(r'{\Large ' + ' {} '.format(problem_type) + r' practice version 0.1\par}')
        lines.append(r'{\large using max integer = ' + str(maximum_integer) + r'\par}')
        lines.append(r'\begin{multicols}{2}')
        lines.append(r'\begin{large}')
        lines.append(r'\begin{enumerate}')

        for _ in range(number_of_problems):
            lines.append(r'\item')
            lines.append(r'\begin{tabular}{lr}')
            int_1 = random.randint(1, maximum_integer)
            int_2 = random.randint(1, maximum_integer)
            if int_1 < int_2:
                int_2, int_1 = int_1, int_2
            int_1_string = ' '.join(iter(str(int_1)))
            int_2_string = ' '.join(iter(str(int_2)))
            lines.append(r' & ' + int_1_string + r'\\')
            if problem_type == 'Mixed':
                lines.append(operator[random.choice(['Addition', 'Subtraction', 'Multiplication'])] + r'& ' + int_2_string + r'\\')
            elif problem_type == 'Mixed Addition Subtraction':
                lines.append(operator[random.choice(['Addition', 'Subtraction'])] + r'& ' + int_2_string + r'\\')
            else:
                lines.append(operator[problem_type] + r'& ' + int_2_string + r'\\')
            lines.append(r'\hline')
            lines.append(r' &    \\')
            lines.append(r'\end{tabular}')
            lines.append(r'\vspace*{50px}')

        lines.append(r'\end{enumerate}')
        lines.append(r'\end{large}')
        lines.append(r'\end{multicols}')

        # generate the next set of problems on a new page for student i
        if index != number_of_pages:
            lines.append(r'\newpage')

    lines.append(r'\end{document}')

    return '\n'.join(lines)