import os
from tkinter import Tk, Button, Label, filedialog, messagebox, IntVar, Checkbutton, Spinbox

# Assuming Image importing from PIL is already done (if needed)

class ImageBrowser:
    def __init__(self, master):
        self.master = master
        self.master.title("Image Browser")

        # Initialize variables
        self.img_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']
        self.file_index = []
        self.current_index = 0
        self.image_folder = ""
        self.limit_reached = False

        # Create widgets
        self.button_open = Button(master, text="Open Image Folder", command=self.open_image_folder)
        self.button_open.pack()

        self.button_previous = Button(master, text="Previous Image", command=self.previous_image)
        self.button_previous.pack(side='left')

        self.button_next = Button(master, text="Next Image", command=self.next_image)
        self.button_next.pack(side='right')

        self.limit_var = IntVar()
        self.limit_checkbox = Checkbutton(master, text="Limit", variable=self.limit_var)
        self.limit_checkbox.pack()

        self.limit_spinbox = Spinbox(master, from_=1, to=100, width=5)
        self.limit_spinbox.pack()

        # Image info labels
        self.label_total_img = Label(master, text="Total Images: 0")
        self.label_total_img.pack()

        self.label_img_count = Label(master, text="Image Count: 0")
        self.label_img_count.pack()

        self.label_img_name = Label(master, text="Image Name: -")
        self.label_img_name.pack()

        self.update_buttons_state()

    def update_buttons_state(self):
        state = 'normal' if self.file_index else 'disabled'
        self.button_next.config(state=state)
        self.button_previous.config(state=state)

    def open_image_folder(self):
        folder_path = filedialog.askdirectory()
        if folder_path:
            self.image_folder = folder_path
            self.file_index = [
                os.path.join(self.image_folder, f)
                for f in os.listdir(self.image_folder)
                if os.path.splitext(f)[1].lower() in self.img_EXTENSIONS
            ]
            self.file_index.sort()

            if not self.file_index:
                messagebox.showinfo("No Images", "No images found in the selected folder.")
                return
            
            self.current_index = 0
            self.limit_reached = False
            self.label_total_img.config(text=f"Total Images: {len(self.file_index)}")
            self.update_image_info()
            
            self.update_buttons_state()
        elif not self.file_index:
            messagebox.showinfo("No Folder", "Please select a folder to start browsing images.")

    def next_image(self):
        if not self.file_index:
            messagebox.showinfo("No Images", "Please open an image folder first.")
            return

        next_count = (self.current_index + 1) % len(self.file_index)

        if self.limit_var.get():
            limit = int(self.limit_spinbox.get())
            if self.limit_reached:
                self.limit_reached = False
                self.current_index = next_count
            elif next_count % limit == 0:  # Limit is reached
                self.label_img_name.config(text="End of limit")
                self.limit_reached = True
                self.label_img_count.config(text=f"Image Count: {next_count}")
                return

        self.current_index = next_count
        self.update_image_info()

    def previous_image(self):
        if not self.file_index:
            messagebox.showinfo("No Images", "Please open an image folder first.")
            return

        self.limit_reached = False  # View the previous image regardless of limit
        self.current_index = (self.current_index - 1) % len(self.file_index)
        self.update_image_info()

    def update_image_info(self):
        current_img_path = self.file_index[self.current_index]
        img_name = os.path.basename(current_img_path)
        self.label_img_name.config(text=f"Image Name: {img_name}")
        self.label_img_count.config(text=f"Image Count: {self.current_index + 1}")
        # You would add logic here to actually display the image, if desired

if __name__ == "__main__":
    root = Tk()
    image_browser = ImageBrowser(root)
    root.mainloop()

instead of calling the next iamge number in the index, make it add a random next image name to add to the pool