from tkinter.messagebox import showinfo
import openpyxl
from errorHandler import testPrint


class UserForm:
    """This class handles the user_form worksheet. All it does in save the contents of the worksheet and
    output them the end of a successful test so the user can record things like their name and the
    checksum of the program."""

    def __init__(self):
        """
        Just create the object. Method initFromExcel will fill it in.
        """
        self.formLines = []  # Will contain the list of lines in the form the user will fill in.
        self.partName = 'user_form'

    def initFromExcel(self, s: openpyxl.workbook.workbook.Worksheet):
        """
        This routine reads text from non-empty cells in column A and saves the results in self.formLines
        :param s: openpyxl.workbook.workbook.Worksheet: The worksheet that we will process
        :return:
        """
        # loop over rows of the worksheet, skipping first row
        for r in s.iter_rows(2):
            formLine = r[0].value  # get the data in the first column of this row
            if formLine is not None and formLine != '':  # We have a non blank cell
                self.formLines.append(formLine)

    def outputForm(self):
        """
        This routine simply outputs the lines of the form an prompts the user to fill them in.
        :return:
        """
        for formLine in self.formLines:  # loop over lines in the form
            testPrint(0, formLine, True)

        showinfo(title='Warning', message='Please fill out the form at the end of the output then '
                                          'save the test results using the File menu.')
