from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException


# ESPN website URL
url_input = "https://www.espn.com/fantasy/basketball/"

# Username and password to login to site
username_input = "brennanmosher7@gmail.com"
password_input = "ESPN42."

delay = 5


def init_webdriver(url):
    # Startup selenium
    d_new = webdriver.Chrome(ChromeDriverManager().install())
    d_new.get(url)
    return d_new


def login(driver, username_in, password_in):
    # Find and click the account icon
    driver.find_element_by_id("global-user-trigger").click()
    account_menu = driver.find_element_by_class_name("account-management")
    # Find the login button and click it
    login = account_menu.find_elements_by_tag_name("li")[4]
    login.click()

    # Switch to pop up frame
    iframe = driver.find_element_by_xpath("//iframe[@id= 'disneyid-iframe']")
    driver.switch_to.frame(iframe)
    # Input the username
    username = driver.find_element_by_xpath("//span[@class='input-wrapper']/input")
    username.send_keys(username_in)
    # Input the password
    password = driver.find_element_by_xpath("(//span[@class='input-wrapper'])[2]/input")
    password.send_keys(password_in)

    # Click the login button
    driver.find_element_by_xpath("//button[@class= 'btn btn-primary btn-submit ng-isolate-scope']").click()
    # Waits the full time but does not find the element so just times out
    # The timeout gives the page enough time to load and find the element
    # Will need to fix in the future but for now it works well enough
    try:
        WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'fav-header-titles fantasy')))
        print("Page is ready!")
    except TimeoutException:
        print("Loading took too much time!")

    # Click on my fantasy team
    driver.find_element_by_xpath("//div[@class = 'wrapper']/h1").click()

    return driver


def move(driver, x):
    # Method takes in the webdriver and the position of the player as input and clicks on the move button for specified player
    # Need to fix this try catch as it always goes into the exception which allows enough time to load page
    try:
        WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'fav-header-titles fantasy')))
        print("Page is ready!")
    except TimeoutException:
        print("Loading took too much time!")

    # Find the table containing the players
    player = driver.find_element_by_xpath("//table[@class= 'Table2__table__wrapper']")
    # Concatenate the string to specify the player
    xpath = "//tr[" + str(x) + "]/td[3]/div/div/button"
    # Click on the move button for specified player
    player.find_element_by_xpath(xpath).click()

    return driver


def here(driver, x):
    # Method to click on the here button, which is the spot for the player to be moved to
    player = driver.find_element_by_xpath("//table[@class= 'Table2__table__wrapper']")
    xpath = "//tr[" + str(x) + "]/td[3]/div/div/button"
    player.find_element_by_xpath(xpath).click()

    return driver



driver_ESPN = init_webdriver(url_input)
driver_ESPN = login(driver_ESPN, username_input, password_input)
driver_ESPN = move(driver_ESPN, 1)
driver_ESPN = here(driver_ESPN, 6)

