#Import Libraries
import RPi.GPIO as GPIO
from picamzero import Camera
from time import sleep
import time
from datetime import date, datetime
import random
import os

#Setup button pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN) #Off Button
GPIO.setup(25, GPIO.IN) #Photo Button
GPIO.setup(16, GPIO.IN) #Video Button
GPIO.setup(12, GPIO.IN) #Question Button

#Setup camera object
cam = Camera()
cam.resolution = (1280,720)
cam.start_preview()

#Seed the random function
random.seed(time.time())

#Setup the date
today = datetime.now()
d1 = today.strftime('Today is, %b %d, %Y')

#PiPhone startup
print("Pi Phone is now on")
os.system("espeak -p 20 'Pie Phone is now on'")
print(d1)
os.system("espeak -p 20 \"" + d1 + "\"")

while True: #Loop allows for multiple uses
    if GPIO.input(25) == GPIO.HIGH: #If photo button is pressed
        sleep(1)
        print("Taking Photo")
        os.system("espeak -p 20 'Taking Photo'") #Announce Photo
        cam.take_photo("myphoto.jpg") #Take photo
        sleep(0.1)
        print("Photo Done")
        
    if GPIO.input(16) == GPIO.HIGH: #If video button is pressed
        sleep(1)
        print("Recording Video")
        os.system("espeak -p 20 'Recording Video.'") #Announce Video
        cam.record_video("myvideo.mp4", duration=5) #Take 5 second video
        sleep(0.1)
        print("Video Complete")
        
    if GPIO.input(20) == GPIO.HIGH: #If off button is pressed
        print("Off Button Pressed")
        break #Break out loop
    
    if GPIO.input(12) == GPIO.HIGH: #If Question button is pressed
        print("Pi is Listening")
        #Loop to wait until button is released
        while(True):
            if(GPIO.input(12) == GPIO.LOW):
                break
            sleep(0.5)
        #Randomly pick a response
        randomnum = random.randint(1, 6)
        #randomnum = 6 #used to test specific lines
        #Print and announce response+
        if randomnum == 1:
            os.system("espeak -p 20 'Ugh, how would I know?'")
            print("Ugh, how would I know?")
        elif randomnum == 2:
            os.system("espeak -p 20 'Go ask Chat GPT.'")
            print("Go ask Chat GPT.")
        elif randomnum == 3:
            os.system("espeak -p 20 'Who cares?'")
            print("Who cares?")
        elif randomnum == 4:
            os.system("espeak -p 20 'Dont ask me.'")
            print("Don't ask me.")
        elif randomnum == 5:
            os.system("espeak -p 20 'Are you stupid?'")
            print("Are you stupid?")
        elif randomnum == 6:
            os.system("espeak -p 20 'Thats a good question, hold on while I process that...'")
            print("That's a good question, hold on while I process that...")
        elif randomnum == 7:
            os.system("espeak -p 20 'I am not wasting my ram on that question.'")
            print("I am not wasting my RAM on that question.")
        
    sleep(0.1)

#End code procedure
cam.stop_preview()
GPIO.cleanup()
os.system("espeak -p 20 'Finally, a break.'") #Announce end
print("Program End")
