from typing import List


def pull_image_files(fls_file: str,
                     check_align: bool = False) -> List[List[str]]:
    """Use .fls file to return ordered images for alignment.

    Initially it will read in .fls data, pull the number of files
    from the first line, and then locate the in-focus image. Then
    it separates the overfocus and underfocus images.

    If the check alignment is set, the returned images are the infocus image,
    and the nearest under-focused/over-focused on either side of the
    infocus image. Otherwise all image files are returned. The files are
    ordered from


                    [
                    [smallest underfocus,  ... , largest underfocus]
                    [infocus]
                    [smallest overfocus, ... , largest overfocus]
                    ]

    Args:
        fls_file: The filename for the fls file.
        check_align: Option for full alignment or parameter test.

    Returns:
        filenames: 2D list of under/in/over-focus images.
    """

    # Read data.
    with open(fls_file) as fls_text:
        fls_text = fls_text.read()

    # Find focus line demarcations in .fls file
    fls_lines = fls_text.splitlines()
    num_files = int(fls_lines[0])
    under_split = 1
    focus_split = num_files // 2 + under_split
    over_split = num_files // 2 + focus_split

    # Grab infocus image and defocus images.
    # If checking parameters, only grab one
    # file on either side of the infocus image.
    focus_file = [fls_lines[focus_split]]
    if check_align:
        under_files = [fls_lines[focus_split - 1]]
        over_files = [fls_lines[focus_split + 1]]
    else:
        under_files = fls_lines[under_split:focus_split]
        over_files = fls_lines[focus_split + 1: over_split + 1]

    # Reverse underfocus files due to how ImageJ opens images
    filenames = [under_files[::-1], focus_file, over_files]
    return filenames