import pygame
import time
import os
import Colors
import SwitchState
from photoslideshow import Photoslideshow


class Photobox():

    def __init__(self, windowsize, photofolder, camera, switch):
        pygame.init()
        self.windowsize = windowsize
        self.photofolder = photofolder
        self.screen = pygame.display.set_mode(self.windowsize, pygame.FULLSCREEN)
        self.clock = pygame.time.Clock()
        self.camera = camera
        self.switch = switch
        self.countdowntime = 3
        pygame.mouse.set_visible(0)

    def start(self):
        slideshow = Photoslideshow(self)
        self.slideshow = slideshow
        while True:
            self.clearscreen()
            slideshow.shownextphoto()
            self.handleevents()
            self.exit_if_needed()

    def exit_if_needed(self):
        events = pygame.event.get()
        if len(events) == 0:
            return
        event = events[0]
        if event.type == pygame.KEYDOWN:
            key = event.key
            if key == pygame.K_ESCAPE:
                exit()

    def showphoto(self, path):
        self.clearscreen()
        photo = pygame.image.load(path)
        photo = pygame.transform.scale(photo, self.windowsize)
        self.screen.blit(photo, (0, 0))
        self.updatescreen()

    def updatescreen(self):
        pygame.display.flip()

    def clearscreen(self):
        self.screen.fill((0, 0, 0))

    def showtext(self, text, fontsize):
        self.clearscreen()
        font = pygame.font.Font(None, fontsize)
        fontWidth = font.size(text)[0]
        fontHeight = font.size(text)[1]
        midX = self.windowsize[0] / 2 - fontWidth / 2
        midY = self.windowsize[1] / 2 - fontHeight / 2
        screentext = font.render(text, True, Colors.randomcolor_rgb())
        self.screen.blit(screentext, (midX, midY))
        self.updatescreen()

    def handleevents(self):
        switchstate = self.switch.get_switch_state()
        if switchstate == SwitchState.SHUTDOWN:
            os.system("sudo shutdown now -h")
            exit(0)
        if switchstate == SwitchState.EXIT:
            exit(0)
        if switchstate == SwitchState.TRIGGER:
            self.takenewphoto()
            self.slideshow.reset_timer()

    def takenewphoto(self):
        self.showcountdown(self.countdowntime)
        self.showtext("Cheese!", 300)
        nextphotophath = self.photofolder.getnextfilename_fullpath()
        photoTaken = self.camera.takephoto(nextphotophath)
        if photoTaken:
            self.showphoto(nextphotophath)

    def showcountdown(self, upperbound):
        for i in range(upperbound, 0, -1):
            self.showtext(str(i), 400)
            time.sleep(1)

@@ -19,16 +19,32 @@
 rt time%0A
+import datetime%0A
 import o
@@ -350,16 +350,29 @@
 et_mode(
+%0A            
 self.win
@@ -529,16 +529,61 @@
 ime = 3%0A
+        self.lastphototaken = datetime.now()%0A
         
@@ -2249,16 +2249,57 @@
 RIGGER:%0A
+            if self.newPhotoAllowed:%0A    
         
@@ -2326,32 +2326,36 @@
 o()%0A            
+    
 self.slideshow.r
@@ -2364,24 +2364,307 @@
 et_timer()%0A%0A
+    def newPhotoAllowed(self):%0A        nextphotoallowed = (self.lastphototaken +%0A                            datetime.timedelta(0, self.countdowntime + 2))%0A        timenow = datetime.datetime.now()%0A        if timenow %3E nextphotoallowed:%0A            return True%0A        return False%0A%0A
     def take
@@ -2915,16 +2915,16 @@
 oTaken:%0A
-
         
@@ -2957,16 +2957,74 @@
 tophath)
+%0A            self.lastphototaken = datetime.datetime.now()
 %0A%0A    de

Allow photos only within a given time range from old photos