#!/usr/bin/python
import RPi.GPIO as GPIO
import os
from omxplayer.player import OMXPlayer
from time import sleep

GPIO.setmode(GPIO.BCM)
PI_TX = 20              # Set Pi Link Pin
PI_RX = 21              # Set Pi Link Pin

GPIO.setup(PI_RX, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)      # Set Pi Link Pin as Input with pull-down resistor
GPIO.setup(PI_TX, GPIO.OUT)                                 # Set Pi Link Pin as Output
GPIO.output(PI_TX, 0)

Loop1 = '/home/pi/Videos/PumpkinsShort_Loop.mp4'        # Define Video Files and Paths
Loop2 = '/home/pi/Videos/PumpkinsLong_Loop.mp4'         # Runtime = 73
Pumpkin1 = '/home/pi/Videos/Pumpkins_Song1.mp4'         # Runtime = 111
Pumpkin2 = '/home/pi/Videos/Pumpkins_Song2.mp4'         # Runtime = 115

os.system('clear')                                      # Clear the console
print("Halloween Media Player v1.0 by Third Earth Design(c)")
sleep(2)
print("Initialising...")
sleep(5)
print("Slave Module Started...")
sleep(2)                                                # Delay Allows External Components to Initialize
GPIO.output(PI_TX, 1)                                   # Notifies Master that Slave is Ready
sleep(1)
while GPIO.input(PI_RX) == 0:                           # Waiting for Master to Indicate Ready
    sleep(1)
    print("Waiting for Master")
    sleep(1)
    print("Waiting for Master...")
GPIO.output(PI_TX, 0)
print("Master is Ready!")
player = OMXPlayer(Loop1, args=['--no-osd', '--loop', '--blank'])  # Define Main OMXPlayer-Wrapper
player.pause()
sleep(1)

try:
    while True:                                             # Main Program - Loop Infinitely
        #Play the First Pumpkin Sequence
        print("Playing Pumpkins Loop (Short)")
        sleep(5)
        player.load(Loop1)
        sleep(1)

        while GPIO.input(PI_RX) == 0:                     # Wait for Input High Signal from Master
            sleep(1)
            print("Waiting for Trigger")
            sleep(1)
            print("Waiting for Trigger...")

        print("Trigger from Master Received")
        sleep(1)
        print("Playback ending...")
        player.stop()
        sleep(3)
        print("Playing Pumpkins Loop (Long)")
        player.load(Loop2)
        sleep(2)        # Delay to prevent DBUS connection errors
        player.play()
        sleep(137)      # Runtime of video minus 1 second to allow stop command to execute before video ends
        player.stop()
        print("Playback ending...")
        sleep(5)

        print("Playing Pumpkins Loop (Short)")
        player.load(Loop1)
        sleep(5)
        player.play()

        #Play the Second Pumpkin Sequence
        while GPIO.input(PI_RX) == 0:
            sleep(1)
            print("Waiting for Trigger")
            sleep(1)
            print("Waiting for Trigger...")

        print("Trigger from Master Received")
        sleep(10)
        print("Playback ending...")
        player.stop()
        sleep(2)
        print("Playing Pumpkins (Song 1)")
        player.load(Pumpkin1)
        sleep(2)
        player.play()
        sleep(110)
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the Third Pumpkin Sequence
        print("Playing Pumpkins Loop (Short)")
        player.load(Loop1)
        sleep(5)
        player.play()
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)

        while GPIO.input(PI_RX) == 0:
            sleep(1)
            print("Waiting for Trigger")
            sleep(1)
            print("Waiting for Trigger...")

        print("Trigger from Master Received")
        sleep(2)
        print("Playback ending...")
        player.stop()
        sleep(2)
        print("Playing Pumpkins Loop (Long)")
        player.load(Loop2)
        sleep(2)
        player.play()
        sleep(131)
        player.stop()
        print("Playback ending...")
        sleep(5)

        #Play the Fourth Pumpkin Sequence
        print("Playing Pumpkins Loop (Short)")
        player.load(Loop1)
        sleep(5)
        player.play()

        while GPIO.input(PI_RX) == 0:
            sleep(1)
            print("Waiting for Trigger")
            sleep(1)
            print("Waiting for Trigger...")

        print("Trigger from Master Received")
        sleep(10)
        player.stop()
        sleep(2)
        print("Playing Pumpkins (Song 2)")
        player.load(Pumpkin2)
        sleep(2)
        player.play()
        sleep(114)
        player.stop()
        print("Playback ending...")
        GPIO.output(PI_TX, 1)
        sleep(5)
        GPIO.output(PI_TX, 0)
        print("Restarting Video Sequence...")
        sleep(5)

except KeyboardInterrupt:
    print('Halloween Media Player Interrupted, QUIT')
    sleep(0.2)
    player.quit()  # Kill the `omxplayer` process gracefully.
    sleep(0.2)
    GPIO.cleanup()  # Exit GPIO cleanly