import pygame
from datetime import datetime, timedelta
import sys

pygame.init()
pygame.mixer.init()

#setup
alarm_triggered = False
time_wake = "12:43:00"
pygame.mixer.music.load(r"C:\Users\natip\OneDrive\Desktop\Python\mixkit-rooster-crowing-in-the-morning-2462.mp3")
snooze_add = timedelta(minutes=5)
(width, height) = (225, 110)
screen = pygame.display.set_mode((width, height))
font = pygame.font.SysFont("Arial", 40)
text_color = (255, 255, 255)
stp_btn = pygame.image.load('stop_btn.png').convert_alpha()
snooze_btn = pygame.image.load('snooze_btn.png').convert_alpha()
time_offset = timedelta()

# button class
class Button:
    def __init__(self, x, y, image):
        self.image = image
        self.rect = self.image.get_rect(topleft=(x, y))
        self.held = False
        self.hold_start = None

    def draw(self):
        screen.blit(self.image, (self.rect.x, self.rect.y))
        pos = pygame.mouse.get_pos()
        clicked = pygame.mouse.get_pressed()[0]
        if self.rect.collidepoint(pos):
            if clicked and not self.held:
                self.held = True
                self.hold_start = datetime.now()
            elif not clicked and self.held:
                duration = (datetime.now() - self.hold_start).total_seconds()
                self.held = False
                return duration  
        elif not clicked:
            self.held = False
        return None

# button
stp_button = Button(155, 25, stp_btn)

snooze_button = Button(20, 25, snooze_btn)
#timer
UPDATE_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(UPDATE_EVENT, 1000)
alarm_time_obj = datetime.strptime(time_wake, "%H:%M:%S")

#audio stop
def stop_sound_pygame():
    pygame.mixer.music.stop()
#states
running = True
edit_mode = None  
edit_start_time = None
edited_time = None
alarm_edit_time = None
flash_state = True
last_flash = datetime.now()
#clock start
while running:
    now = datetime.now() + time_offset
    screen.fill((0, 0, 0))
    #flash
    if (datetime.now() - last_flash).total_seconds() >= 0.5:
        flash_state = not flash_state
        last_flash = datetime.now()
    #display of edit mode
    if edit_mode == "clock":
        if flash_state:
            display_str = edited_time.strftime("%I:%M:%S %p")
        else:
            display_str = ""
    elif edit_mode == "alarm":
        if flash_state:
            display_str = alarm_edit_time.strftime("%I:%M:%S %p")
        else:
            display_str = ""
    else:
        display_str = now.strftime("%I:%M:%S %p")

    time_surface = font.render(display_str, True, text_color)
    screen.blit(time_surface, (20, 50))
  
    stp_duration = stp_button.draw()
    snooze_duration = snooze_button.draw()
    #stop button
    if stp_duration:
        if edit_mode == "clock":
            if stp_duration > 1.5:
                time_offset += (edited_time - datetime.now())
                edit_mode = None
            else:
                edited_time += timedelta(hours=1)
        elif edit_mode == "alarm":
            if stp_duration > 1.5:
                alarm_time_obj = alarm_edit_time
                edit_mode = None
            else:
                alarm_edit_time += timedelta(hours=1)
        else:
            if stp_duration > 1.5:
                edit_mode = "clock"
                edited_time = now
            else:
                if alarm_triggered:
                    stop_sound_pygame()
                    alarm_triggered = False
    #snooze button
    if snooze_duration:
        if edit_mode == "clock":
            if snooze_duration <= 1.5:
                edited_time += timedelta(minutes=1)
        elif edit_mode == "alarm":
            if snooze_duration > 1.5:
                alarm_time_obj = alarm_edit_time
                edit_mode = None
            else:
                alarm_edit_time += timedelta(minutes=1)
        else:
            if snooze_duration > 1.5:
                edit_mode = "alarm"
                alarm_edit_time = alarm_time_obj
            else:
                if alarm_triggered:
                    stop_sound_pygame()
                    alarm_time_obj += snooze_add
                    alarm_triggered = False
    # update
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == UPDATE_EVENT:
            if not edit_mode: 
                current_time_obj = now.time()
                if (current_time_obj.hour == alarm_time_obj.hour and
                    current_time_obj.minute == alarm_time_obj.minute and
                    current_time_obj.second == alarm_time_obj.second and
                    not alarm_triggered):
                    pygame.mixer.music.play(-1)
                    alarm_triggered = True

    pygame.display.flip()

