from typing import Tuple


def generate_patch_coords_from_box(height, width, box, patch_dimensions) -> Tuple[int, int, int, int]:
    """
    Generates safe coords for the patch given box

    :param height: Max height of the frame
    :param width:  Max width of the frame
    :param box: (x, y, w, h)
    :param patch_dimensions: Dimension of the patch in either side
    :return: (y1, x1, y2, x2)
    """
    dimension_on_either_side = patch_dimensions // 2

    x1, y1, w, h = box

    # get center points
    cx = x1 + (w // 2)
    cy = y1 + (h // 2)

    # get patch coordinates
    p_x1 = cx - dimension_on_either_side
    p_x1 = p_x1 if p_x1 > 0 else 0

    p_y1 = cy - dimension_on_either_side
    p_y1 = p_y1 if p_y1 > 0 else 0

    p_x2 = cx + dimension_on_either_side
    p_x2 = p_x2 if p_x2 < width else width

    p_y2 = cy + dimension_on_either_side
    p_y2 = p_y2 if p_y2 < height else height

    return int(p_y1), int(p_x1), int(p_y2), int(p_x2)