from typing import List
from typing import Optional
from typing import Tuple


def _get_file_name(path: List[str]) -> Optional[Tuple[str, str]]:
    """Fetches the file name from the path.

    Args:
        path: The list of elements in the path

    Returns:
        None is returned on failure. If the path contains
        a valid file path the tuple will contain the path in the first
        element. The second element will be the extension of the file.
    """
    # The path may not be empty
    if len(path) < 1:
        return None

    # Check that the path is sanitized
    # Path elements may only contain alphabetic characters, numbers and
    # dashes. Only the last path element may have exactly one dot.
    for i in range(len(path)):
        is_last = i == len(path) - 1
        has_dot = False
        has_chars = False
        for c in path[i]:
            if c == ".":
                if not is_last:
                    return None  # dots not in last element
                if has_dot:
                    return None  # multiple dots
                if not has_chars:
                    return None  # element starts with a dot
                has_dot = True
            elif c not in ("abcdefghijklmnopqrstuvwxyz"
                           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                           "0123456789-"):
                return None  # illegal character
            has_chars = True

    # Construct the file name
    fn = "/".join(path)
    if "." not in fn:
        return None  # no file extension
    dot_idx = fn.index(".")

    f = "./res/%s" % fn
    ext = fn[(dot_idx + 1):]
    return f, ext