import os
import shutil

# def copy_last_100_images(source_folder, destination_folder):

#     image_files = [file for file in os.listdir(source_folder) if file.endswith('.jpg')]

#     last_100_images = image_files[-100:]
    

#     for index, image_file in enumerate(last_100_images):
#         source_path = os.path.join(source_folder, image_file)
#         destination_path = os.path.join(destination_folder, f"{index:05d}.jpg")  
#         shutil.copyfile(source_path, destination_path)


# def extract_last_100_lines(input_file, output_file):
#     with open(input_file, 'r') as f:
#         lines = f.readlines()
#     last_100_lines = lines[-100:]

#     with open(output_file, 'w') as f:
#         f.writelines(last_100_lines)
# def copy_last_100_images(source_folder, destination_folder):

#     image_files = [file for file in os.listdir(source_folder)]

#     last_100_images = image_files[-100:]
    

#     for index, image_file in enumerate(last_100_images):
#         source_path = os.path.join(source_folder, image_file)
#         destination_path = os.path.join(destination_folder, f"{index:05d}.png")  
#         shutil.copyfile(source_path, destination_path)


# def extract_last_100_lines(input_file, output_file):
#     with open(input_file, 'r') as f:
#         lines = f.readlines()
#     last_100_lines = lines[-100:]

#     with open(output_file, 'w') as f:
#         f.writelines(last_100_lines)
# def copy_first_100_images(source_folder, destination_folder):

#     image_files = [file for file in os.listdir(source_folder)]

#     last_100_images = image_files[0:100]
    

#     for index, image_file in enumerate(last_100_images):
#         source_path = os.path.join(source_folder, image_file)
#         destination_path = os.path.join(destination_folder, f"{index:05d}.png")  
#         shutil.copyfile(source_path, destination_path)


# def extract_first_100_lines(input_file, output_file):
#     with open(input_file, 'r') as f:
#         lines = f.readlines()
#     last_100_lines = lines[0:100]

#     with open(output_file, 'w') as f:
#         f.writelines(last_100_lines)
def copy_images_in_range(source_folder, destination_folder, start_index, end_index):

    image_files = sorted(os.listdir(source_folder))

    images_in_range = image_files[start_index:end_index]
    

    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)
    

    for index, image_file in enumerate(images_in_range):
        source_path = os.path.join(source_folder, image_file)
        destination_path = os.path.join(destination_folder, f"{index:05d}.png")  
        shutil.copyfile(source_path, destination_path)


def extract_lines_in_range(input_file, output_file, start_line, end_line):
    with open(input_file, 'r') as f:
        lines = f.readlines()
    

    lines_in_range = lines[start_line:end_line]

    with open(output_file, 'w') as f:
        f.writelines(lines_in_range)



# source_folder = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/images_true"
# destination_folder = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/renders"
# start_index = 890
# end_index = 930
# copy_images_in_range(source_folder, destination_folder, start_index, end_index)

# input_file = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/images.txt"
# output_file = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/image_timestamps_old.txt"
# start_line = 890
# end_line = 930
# extract_lines_in_range(input_file, output_file, start_line, end_line)


# copy_first_100_images(source_folder, destination_folder)
# extract_first_100_lines(input_file, output_file)
# copy_last_100_images(source_folder, destination_folder)
# extract_last_100_lines(input_file, output_file)
def copy_lines_until_threshold(input_file, output_file, threshold):
    with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
        for line in f_in:

            first_number = int(float(line.split()[0]) * 1e6)

            if first_number <= threshold:

                line_parts = line.split()
                line_parts[0] = str(first_number)
                f_out.write(' '.join(line_parts) + '\n')

            else:
                break
def copy_lines_after_threshold(input_file, output_file, threshold):
    with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
        copy = False
        for line in f_in:

            first_number = int(float(line.split()[0]) * 1e6)

            if first_number > threshold:
                copy = True

            if copy:

                line_parts = line.split()
                line_parts[0] = str(first_number)
                f_out.write(' '.join(line_parts) + '\n')
def copy_lines_in_time_range(input_file, output_file, start_time, end_time):
    with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
        for line in f_in:

            timestamp = int(float(line.split()[0]) * 1e6)

            if start_time <= timestamp <= end_time:

                line_parts = line.split()
                line_parts[0] = str(timestamp)
                f_out.write(' '.join(line_parts) + '\n')

# input_file = 'D:/2024/3DGS/PureEventFilter/data/dynamic_translation_colmap_easy/events.txt'

# output_file = 'D:/2024/3DGS/PureEventFilter/data/dynamic_translation_colmap_easy/calibration_volt.txt'

# threshold = 55 * 1e6 


# # copy_lines_until_threshold(input_file, output_file, threshold)
# copy_lines_after_threshold(input_file, output_file, threshold)
input_file = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/events.txt"
output_file = "D:/2024/3DGS/PureEventFilter/data/dynamic_high_colmap_easy/dynamic_high_volt.txt"
start_time = 41.89*1e6  
end_time = 43.76*1e6   
copy_lines_in_time_range(input_file, output_file, start_time, end_time)
