'''
title: Automated EPSB check in/out script
author: michael Tang
date created: 2021-01-25
date modified: 2022-03-03
'''
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

global URL
global whoHereFor

# path to chromedriver
# DRIVER_PATH = "/home/michtang/selenium/chromedriver"
DRIVER_PATH = "/Users/michaeltang/Documents/selenium/chromedriver" 
driver = webdriver.Chrome(DRIVER_PATH)

def checkIn():
    ############### Start of check In #################
    textboxes = driver.find_elements_by_xpath("//input[@class='whsOnd zHQkBf']")
    textboxes[0].send_keys("Michael Tang")
    checkInRadio = driver.find_element_by_css_selector("#i9")
    checkInRadio.click()
    nextButton = driver.find_element_by_class_name("uArJ5e")
    nextButton.click()
    time.sleep(2)
    # check In
    # get textboxes
    textboxes2 = driver.find_elements_by_xpath("//input[@class='whsOnd zHQkBf']")
    # get radio buttons
    radiobuttons = driver.find_elements_by_id("i21")
    # get submit button
    buttons = driver.find_elements_by_xpath("//div[@class='uArJ5e UQuaGc YhQJj zo8FOc IT3Lge']")

    # email [0]
    textboxes2[0].send_keys("michael.tang@epsb.ca")
    # org [1]
    textboxes2[1].send_keys("EPS Supply Services")

    # phone number [2]
    textboxes2[2].send_keys("7809656618")

    # who will you be working with [3]
    textboxes2[3].send_keys(whoHereFor)

    # click no radio
    radiobuttons[0].click()

    # click next
    buttons[1].click()

    print('waiting 2 seconds for final page to load')
    time.sleep(2)

    buttons =  driver.find_elements_by_xpath("//div[@role='button']")
    # find the submit button 
    #buttonText = buttons[1].text
    pressSubmit = input("Press submit? (y/n)")
    if pressSubmit == "y":
        print(buttons[1].text+" clicked!")
        buttons[1].click()
        time.sleep(1)
        print("quitting")
        driver.quit()
    elif pressSubmit == "n":
        print("Manual mode")
        #buttons[0].click()
        #find back button
        backButton = driver.find_element_by_xpath("//div[@class='uArJ5e UQuaGc YhQJj zo8FOc IT3Lge']")
        print(backButton.text + " clicked.")
        buttons[0].click()
    else:
        print("invalid key pressed.")
    
    ########################## end of Check in ##############################

def checkOut():
    ############################ start Check Out ############################
    textboxes = driver.find_elements_by_xpath("//input[@class='whsOnd zHQkBf']")
    textboxes[0].send_keys("Michael Tang")
    checkOutRadio = driver.find_element_by_css_selector("#i12")
    try:
        checkOutRadio.click()
    except:
        print("exception")
    
    nextButton = driver.find_element_by_class_name("uArJ5e")
    nextButton.click()
    time.sleep(3) # wait for next page to load
    # Next page of check Out
    try:
        whoWereYouHereFor = driver.find_element_by_xpath("//textarea[@class='KHxj8b tL9Q4c']")
        whoWereYouHereFor.click()
        whoWereYouHereFor.send_keys(whoHereFor)
    except NoSuchElementException:
        print("no text area to enter")
    # Automated click of submit
    submitButton =  driver.find_elements_by_xpath("//div[@role='button']")
    # find the submit button 
    buttonText = submitButton[1].text
    pressSubmit = input("Press submit? (y/n)")
    if pressSubmit == "y":
        print(buttonText + " clicked!")
        submitButton[0].click()
        time.sleep(1)
        print("quitting")
        driver.quit()
    elif pressSubmit == "n":
        print("Manual mode engaged.")
        # find the back button
        backButton = driver.find_elements_by_xpath("//div[@role='button']")
        print(backButton[0].text+ " clicked!")
        backButton[0].click()
    else:
        print("invalid key pressed.")
    ############################ end Check Out ##############################

def locationSelector():
    print("Select the school: ")
    print("1. ABM")
    print("2. DTK")
    print("3. LOHS")
    print("4. GHL")
    print("5. DDM")
    print("6. McNally")
    print("7. DAA")
    print("8. Victoria")
    schoolSelected = input("Select the school: ")
    if schoolSelected == "1":
        global URL
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSeJf3A_bADAUyaXuTEfiyU5O4V77PxcL9lYBSC3vgP4Fn4eGg/viewform"
    elif schoolSelected == "2":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSfIZdJoeB8MUrUSAsbKkgsrsRbXB0nUYui2Dn2weCfa5l_KIw/viewform"
    elif schoolSelected == "3":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSeACzhxKn6mVV0ArePRXE1qz_uojT9CT53OaXnYpEh7T4GzJg/viewform"
    elif schoolSelected == "4":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSdIT2XbkW4leXrQmS0rEuQ70lZoXkNG_zv7a0JOIRS4wuyxfw/viewform"
    elif schoolSelected == "5":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSeQJO4uzCHg-OIawaWiVcjJrhcVsFKZ-2hDF4wOaDmz9Xx86w/viewform"
    elif schoolSelected == "6":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSe8faJgWiVMym4pw6VNR5aJ7j9cJUkP2TYSNUIrYnAcJyaD2w/viewform"
    elif schoolSelected == "7":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSftjfI8mR3fF8IlnRnBiD5w8rbP1Q10hd_sL7vv1zd_qU50iQ/viewform?fbzx=-2834371413822588919"
    elif schoolSelected == "8":
        URL = "https://docs.google.com/forms/d/e/1FAIpQLSdviM6-Ua5yQvYAGLfBXeMN7w3SjHAcQPB9d29fZEettwWHlQ/viewform"
    else:
        print("not a valid selection.")
        locationSelector()

def main():
    locationSelector()
    # executor_url = driver.command_executor._url
    # session_id = driver.session_id
    # print(session_id)
    driver.get(URL)
    # time.sleep(2)
    print("Enter what you want to do: ")
    print("1: check in")
    print("2. check out")
    checkInOut = input("Your selection: ")
    # print(checkInOut)
    if checkInOut == "1":
        print("running check in")
        global whoHereFor
        whoHereFor = input("Who are you in for: ")
        checkIn()
    elif checkInOut == "2":
        print("running check out") 
        whoHereFor = input("Who were you in for: ")
        checkOut()
    else:
        print("you did not enter a valid option")
        main()

# execute main function
main()