import tkinter as tk
import random
import os
import shutil
import json
from PIL import Image, ImageTk, ImageDraw, ImageFont
import time

# Data structure to store exercises, reps, and images
try:
    with open("exercise_reps.json", "r") as f:
        exercises = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
    exercises = {
        "Bicep Curl": {"reps": 10, "image": "Bicep_Curl.jpg", "completed": False},
        "Arnold Press": {"reps": 10, "image": "Arnold Press.jpg", "completed": False},
        "Kegel": {"reps": 10, "image": "kegels-benefits.jpg", "completed": False},
        "Dumbbell Lateral Raise": {"reps": 10, "image": "Dumbbell_Lateral_Raise.webp", "completed": False},
    }

# Create the GUI window
root = tk.Tk()
root.title("Fitness Buddy")
root.state('zoomed')  # Make the window full-sized to the real full screen

# Set dark mode colors
root.configure(bg="#333")  # Dark gray background
exercise_label_color = "#fff"   # White text color
button_color = "#444"  # Dark gray button color
button_text_color = "#fff"  # White button text color

# Create a label to display the exercise or live task
exercise_label = tk.Label(root, text="", wraplength=400, bg="#333", fg="#fff", font=("Arial", 18))
exercise_label.place(relx=0.5, rely=0.2, anchor=tk.CENTER)  # Center align the label

# Create a label to display the exercise image
image_label = tk.Label(root, image="", bg="#333")
image_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)  # Center align the image label

# Function to update the exercise reps
def update_reps(completed, exercise):
    global exercises
    if completed:
        exercises[exercise]["reps"] += 2
    else:
        exercises[exercise]["reps"] -= 0

# Function to display the exercise
def display_exercise():
    global exercises

    # Get the list of uncompleted exercises
    uncompleted_exercises = [exercise for exercise, details in exercises.items() if not details["completed"]]

    # If all exercises have been completed, reset the "completed" flags
    if not uncompleted_exercises:
        for exercise in exercises:
            exercises[exercise]["completed"] = False
        uncompleted_exercises = list(exercises.keys())

    # Choose a random uncompleted exercise
    current_exercise = random.choice(uncompleted_exercises)
    exercise_text = f"Do {exercises[current_exercise]['reps']} reps of {current_exercise}"
    exercise_image_path = exercises[current_exercise]["image"]

    # Display the exercise
    exercise_label.config(text=exercise_text)

    # Display the exercise image with reps
    image = Image.open(exercise_image_path)

    # Resize the image to fit the label
    image_width, image_height = image.size
    label_width, label_height = 1800, 1000  # adjust these values to fit your label size
    scale_factor = min(label_width / image_width, label_height / image_height)
    new_width, new_height = int(image_width * scale_factor), int(image_height * scale_factor)
    image = image.resize((new_width, new_height))

    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", 24)
    draw.text((10, 10), f"{current_exercise}", font=font, fill=(0, 0, 0))
    draw.text((10, 40), f"Reps: {exercises[current_exercise]['reps']}", font=font, fill=(0, 0, 0))
    photo = ImageTk.PhotoImage(image)
    image_label.config(image=photo)
    image_label.image = photo

    # Show the Yes and No buttons
    yes_button.config(command=lambda: [update_reps(True, current_exercise), exercises[current_exercise].update({"completed": True}), display_exercise()])
    no_button.config(command=lambda: [update_reps(False, current_exercise), exercises[current_exercise].update({"completed": True}), display_exercise()])

    yes_button.pack(side=tk.RIGHT, padx=20, pady=20)
    no_button.pack(side=tk.RIGHT, padx=20, pady=20)

    # Save the exercise reps to JSON files
    with open("exercise_reps.json", "w") as f:
        json.dump(exercises, f)

# Create a frame for the buttons
button_frame = tk.Frame(root, bg="#333")
button_frame.place(relx=1, rely=1, anchor=tk.SE)  # Bottom right align the button frame

yes_button = tk.Button(button_frame, text="Yes", bg="#444", fg="#fff", font=("Arial", 24))
no_button = tk.Button(button_frame, text="No", bg="#444", fg="#fff", font=("Arial", 24))

# Start the application
display_exercise()
root.mainloop()

{"Bicep Curl": {"reps": 70, "image": "Bicep_Curl.jpg", "completed": true}, "Arnold Press": {"reps": 52, "image": "Arnold Press.jpg", "completed": false}, "Kegel": {"reps": 76, "image": "kegels-benefits.jpg", "completed": false}, "Dumbbell Lateral Raise": {"reps": 52, "image": "Dumbbell_Lateral_Raise.webp", "completed": false}, "Pull ups": {"reps": 13, "image": "pullups.webp", "completed": false}, "Hanning": {"reps": 44, "image": "Hanning.png", "completed": false}, "5 min a day": {"reps": 11, "image": "kegel.jpg", "completed": false}, "cobra 5 min a day": {"reps": 17, "image": "cobra.jpg", "completed": false}, "bend 5 min a day": {"reps": 42, "image": "bend.jpg", "completed": false}, "Jump 10 min a day": {"reps": 13, "image": "jump.webp", "completed": false}, "wall": {"reps": 47, "image": "wall.webp", "completed": false}, "side bend": {"reps": 75, "image": "side.jfif", "completed": false}, "Rope": {"reps": 51, "image": "rope.webp", "completed": false}, "2 min single rope": {"reps": 9, "image": "single_rope.webp", "completed": false}, "5 min palm": {"reps": 9, "image": "palm.png", "completed": false}, "Abs  ": {"reps": 28, "image": "abs.jpg", "completed": false}, "Squat hero": {"reps": 19, "image": "squats.webp", "completed": false}, "calf": {"reps": 27, "image": "calf.jpg", "completed": false}}

the app become to big is there is a way to change so in the full screen  have 9 workouts like boxes   and scroll for rest