import csv
import random
import numpy as np

# Constants for the video frame
VIDEO_WIDTH = 1920
VIDEO_HEIGHT = 1080
VIDEO_DURATION_SEC = 120  # 2 minutes
SAMPLE_RATE_HZ = 50

# Define NUM_USERS
NUM_USERS = 3

# Simulate common Point of Gaze coordinates for all users
num_samples = VIDEO_DURATION_SEC * SAMPLE_RATE_HZ
time_stamps = [i / SAMPLE_RATE_HZ for i in range(num_samples)]
base_point_of_gaze_x = np.random.randint(0, VIDEO_WIDTH, num_samples)
base_point_of_gaze_y = np.random.randint(0, VIDEO_HEIGHT, num_samples)

# Simulate Point of Gaze and pupil dilation data for each user
for user_id in range(1, NUM_USERS + 1):
    output_file = f"user_{user_id}_eye_tracking.csv"
    data = []

    point_of_gaze_x_perturbation = np.random.normal(0, VIDEO_WIDTH / 20, num_samples)
    point_of_gaze_y_perturbation = np.random.normal(0, VIDEO_HEIGHT / 20, num_samples)
    user_point_of_gaze_x = np.random.randint(0, VIDEO_WIDTH, num_samples) + point_of_gaze_x_perturbation.astype(int)
    user_point_of_gaze_y = np.random.randint(0, VIDEO_HEIGHT, num_samples) + point_of_gaze_y_perturbation.astype(int)

    for t in range(num_samples):
        # Obtain Point of Gaze coordinates based on the common base
        user_point_of_gaze_x[t] = int(base_point_of_gaze_x[t] + point_of_gaze_x_perturbation[t]) % VIDEO_WIDTH
        user_point_of_gaze_y[t] = int(base_point_of_gaze_y[t] + point_of_gaze_y_perturbation[t]) % VIDEO_HEIGHT

        # Simulate pupil dilation values
        pupil_dilation = random.uniform(0.1, 1.0)  # Assumed range of pupil dilation values
        
        # Simulate which window the user is looking at (change range from 1 to 4)
        window_id = random.randint(1, 4)  # Assuming there are 4 windows

        # Simulate corneal reflection
        corneal_reflection = random.uniform(0, 1)  # Simulating corneal reflection data

        # Append data for this timestamp
        data.append([time_stamps[t], user_point_of_gaze_x[t], user_point_of_gaze_y[t], pupil_dilation, window_id, corneal_reflection])

    # Write data to CSV file
    with open(output_file, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Time(s)", "Point of Gaze X", "Point of Gaze Y", "Pupil Dilation", "Window ID", "Corneal Reflection"])
        writer.writerows(data)

    print(f"Eye-tracking data for user {user_id} saved to {output_file}")