import time
from selenium import webdriver

DRIVER_PATH = "/home/michtang/selenium/chromedriver"
# Shining Fates ETB
URL  = "https://store.401games.ca/collections/pokemon-shining-fates-pre-orders/products/pokemon-shiningfates-elitetrainerboxpre-order"
# Board Game Test
# URL = "https://store.401games.ca/collections/pokemon-sealed-product/products/pokemon-battleacademyboxset"
driver = webdriver.Chrome(DRIVER_PATH)
driver.get(URL)
time.sleep(3)
found = 0
timeWait = 100
buttons = driver.find_elements_by_id("AddToCart-product-template")
while found == 0:
    if buttons[0].get_attribute("innerText") == "SOLD OUT":
        print(buttons[0].text)
        while timeWait > 0:
            print("waiting to refesh in " + str(timeWait) )
            timeWait = timeWait - 1
            time.sleep(1)
        print("Time to refresh")
        driver.refresh()
        buttons = driver.find_elements_by_id("AddToCart-product-template")
        timeWait = 100
    else:
        found = 1
        print(buttons[0].text)
        quantity = driver.find_element_by_id("Quantity")
        quantity.clear()
        quantity.send_keys("10")
        buttons = driver.find_elements_by_id("AddToCart-product-template")
        buttons[0].click()
        time.sleep(3)
        cart = driver.find_elements_by_id("CartButton")
        cart[0].click()
        checkout = driver.find_element_by_xpath("//input[@name='checkout']")
        checkout.click()
        time.sleep(1)
        email = driver.find_element_by_xpath("//input[@name='customer[email]']")
        email.send_keys("michael.h.tang@gmail.com")
        password = driver.find_element_by_xpath("//input[@name='customer[password]']")
        print("done")
