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")
        self.history_pool = []  # This will be the shuffled list of images
        
        # 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()


    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()
            

        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()

when i press open image, set file index and call an image from the index.put it in the history pool. 
make the program calls from the history pool. make the next buttton calls random image number and add it to the pool. when i press previous, make it call from the history pool. make the next button call from the pool until it reach the bottom of the pool then add new random image to the pool from the index. keep the limit button showing an end limit text for every (limit number)

make it so that the next button and previous button will show every image in the folder once before starting another cycle.