import math


def fixation_radius_normalized(
    theta_deg: float,
    distance_cm: float,
    screen_width_cm: float,
    screen_height_cm: float,
    resolution_x: int,
    resolution_y: int,
    method: str = "max",
):
    """
    Compute the PyGaze fixation radius for normalized gaze data in [0, 1].
    """
    # Visual angle to physical distance (cm)
    delta_cm = 2 * distance_cm * math.tan(math.radians(theta_deg) / 2)

    # Physical distance to pixels
    delta_px_x = delta_cm * (resolution_x / screen_width_cm)
    delta_px_y = delta_cm * (resolution_y / screen_height_cm)

    # Pixel radius
    if method == "max":
        r_px = max(delta_px_x, delta_px_y)
    else:
        r_px = math.sqrt(delta_px_x**2 + delta_px_y**2)

    # Pixel radius to normalized radius
    r_norm_x = r_px / resolution_x
    r_norm_y = r_px / resolution_y

    if method == "max":
        return max(r_norm_x, r_norm_y)
    return math.sqrt(r_norm_x**2 + r_norm_y**2)


def run_example():
    # Example: 55" 4k monitor
    screen_width_cm = 3 * 121.8
    screen_height_cm = 68.5
    resolution_x = 3 * 3840
    resolution_y = 2160
    distance_to_screen_cm = 120
    max_angle = 1.0

    maxdist_px = fixation_radius_normalized(
        theta_deg=max_angle,
        distance_cm=distance_to_screen_cm,
        screen_width_cm=screen_width_cm,
        screen_height_cm=screen_height_cm,
        resolution_x=resolution_x,
        resolution_y=resolution_y,
        method="max",
    )
    print("PyGaze max_dist (max):", maxdist_px)

    maxdist_px = fixation_radius_normalized(
        theta_deg=max_angle,
        distance_cm=distance_to_screen_cm,
        screen_width_cm=screen_width_cm,
        screen_height_cm=screen_height_cm,
        resolution_x=resolution_x,
        resolution_y=resolution_y,
        method="euclid",
    )
    print("PyGaze max_dist (euclid):", maxdist_px)


def main():
    run_example()


if __name__ == "__main__":
    main()

# Reference
# https://osdoc.cogsci.nl/4.0/de/visualangle/
# https://reference.org/facts/Visual_angle/LUw29zy7
