#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO
import subprocess
import datetime
import pygame
from pygame.locals import *

gpio = 4

#setup pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio,GPIO.IN)

#setup variables
count = 0
up = False
down = False
command = ""
current_dir = os.getcwd()
filename = ""
image_dir = current_dir + "/images"
camera_pause  = "1000"

enable_dropbox = True
dropbox_command = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh -q upload"
dropbox_dir = "photobooth"

print("Raspberry Pi - Point & Shoot Camera v2.2")
print("-with pygame and screen, and messaging")

running = True;

pygame.init()
screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)

width, height = screen.get_size()

font = pygame.font.SysFont("verdana", 65, bold=1)
font2 = pygame.font.SysFont("verdana", 55, bold=0)
textsurface = font.render("Raspberry Pi", 1, pygame.Color(255,255,255))
screen.blit(textsurface,(20,280))
textsurface = font2.render("Point & Shoot Camera", 1, pygame.Color(255,255,255))
screen.blit(textsurface,(20,350))

if(not os.path.exists(image_dir)):
	command = "mkdir " + image_dir
	print(command)
	os.system(command)

def takepic(imageName):
    writemessage("taking photo...")
    command = "sudo raspistill -o " + imageName + " -q 100 -rot 270 -t " + camera_pause
    print(command)

    os.system(command)
    writemessage("loading photo...")

def loadpic(imageName):
    print("loading image: " + imageName)
    
    background = pygame.image.load(imageName);
    background.convert_alpha()
    background = pygame.transform.scale(background,(width,height))
    screen.blit(background,(0,0),(0,0,width,height))

    font = pygame.font.SysFont("verdana", 40, bold=0)
    textsurface = font.render(imageName, 1, pygame.Color(0,0,0))
    screen.blit(textsurface,(20,0))

def movepic(imageName):
    command = "mv " + imageName + " " + image_dir + "/" + imageName
    print(command)
    os.system(command)

def upload(imageName):
    command = dropbox_command + " " + image_dir + "/" + imageName + " " + dropbox_dir + "/" + imageName
    print(command)
    subprocess.Popen(command, shell=True)
    
def writemessage(message):
    screen.fill(pygame.Color(0,0,0))
    font = pygame.font.SysFont("verdana", 50, bold=1)
    textsurface = font.render(message, 1, pygame.Color(255,255,255))
    screen.blit(textsurface,(35,40))
    pygame.display.update()
    
while running:
    if(up == True):
        if(GPIO.input(gpio)==False):
            print("button pressed")
            now = datetime.datetime.now()
            timeString = now.strftime("%Y-%m-%d_%H:%M:%S")
            print("request received: " + timeString)
            filename = "photo-" + timeString + ".jpg"
            takepic(filename)
            loadpic(filename)
            movepic(filename)
            if(enable_dropbox == True):
                upload(filename)
            
    up = GPIO.input(gpio)
    count = count + 1
    
    pygame.display.update()
    sleep(.1)

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            running = False

#never hit
print("end of loop");
