import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import re

CLEANR = re.compile('<.*?>')

def cleanhtml(raw_html):
  cleantext = re.sub(CLEANR, '', raw_html)
  return cleantext

#options = webdriver.ChromeOptions()
#options.add_argument("headless")
#driver = webdriver.Chrome(executable_path='C:\Users\user\Downloads\RenPy TVNDNE\chromedriver.exe', chrome_options=options)

week = 0
lovemeter = 0
print("Logging in to Novel AI, please wait.")

driver = webdriver.Chrome('C:\Users\user\Downloads\RenPy TVNDNE\chromedriver.exe')
story_window = driver.window_handles[0]

#driver = webdriver.Firefox("C:/")

# Log In to Novel AI
driver.get('https://novelai.net/login')
driver.execute_script('window.open()')
evaluator_window = driver.window_handles[1]
driver.switch_to.window(evaluator_window)
driver.get('https://novelai.net/login')

driver.switch_to.window(story_window)
time.sleep(5)
WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, '//*[@id="username"]'))).send_keys('oshojabbe@gmail.com')
driver.find_element_by_xpath('//*[@id="password"]').send_keys('Noriental1!')
driver.find_element_by_xpath("//input[contains(@type,\"submit\")]").click()

driver.switch_to.window(evaluator_window)
WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, '//*[@id="username"]'))).send_keys('oshojabbe@gmail.com')
driver.find_element_by_xpath('//*[@id="password"]').send_keys('Noriental1!')
driver.find_element_by_xpath("//input[contains(@type,\"submit\")]").click()

#Get to a Prose Window
driver.switch_to.window(story_window)
WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, '//button[contains(@aria-label,\"Close Modal\")]'))).click()

driver.switch_to.window(evaluator_window)
WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, '//button[contains(@aria-label,\"Close Modal\")]'))).click()

time.sleep(1)
driver.switch_to.window(story_window)
driver.find_element_by_xpath("//button[contains(@style,\"flex-grow: 2.5;\")]").click()

driver.switch_to.window(evaluator_window)
driver.find_element_by_xpath("//button[contains(@style,\"flex-grow: 2.5;\")]").click()
time.sleep(3)

#Initialize Evaluator Window
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Evaluate the following scenario:")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO START")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Elizabeth smiles, looks at the rose, and thanks me before putting it in her hair. She then asks if I'd like to join the same club as well. We both agree, and proceed to discuss our garden plans for the next school year.")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO END")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Question: On a scale of 1 to 10, was this terrible (1) or very good (10) for their romantic relationship?")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Answer: 7, pretty good.")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Evaluate the following scenario:")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO START")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Elizabeth laughs at you. She can't believe you'd have the audacity to ask her out. She knows she is so much better than you, and would never date you in a million years.")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO END")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Question: On a scale of 1 to 10, was this terrible (1) or very good (10) for their romantic relationship?")
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Answer: 2, very bad.")

driver.switch_to.window(story_window)
#Start Adventure Text
print("What is your name?")
protag = raw_input()
print("What is the name of your waifu?")
waifu = raw_input()

def cleanstory():
    print("[Cleaning output"),
    proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    proseString = proseString.split("==Week {} of School==".format(week),1)[1]
    while proseString.count('Scenario:') > 1:
        print("."),
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.BACKSPACE)
        proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
        proseString = proseString.split("==Week {} of School==".format(week),1)[1]
    while "Week {}".format(week + 1) in proseString:
        print("."),
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.BACKSPACE)
        proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
        proseString = proseString.split("==Week {} of School==".format(week),1)[1]
    print("]")

def cleaneval(prose):
    print("[Cleaning evaluation"),
    evalString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    evalString = evalString.split(prose,1)[1]
    while evalString.count('Evaluate the') > 0:
        print("."),
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.BACKSPACE)
        evalString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
        evalString = evalString.split(prose,1)[1]
    while "Week {}".format(week + 1) in proseString:
        print("."),
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.BACKSPACE)
        evalString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
        evalString = evalString.split(prose,1)[1]
    print("]")

#Week Loop
while 0 < 1:
    driver.switch_to.window(story_window)
    week = week + 1

    if week > 1:
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    
    WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class,\"ProseMirror\")]"))).send_keys("==Week {} of School==".format(week))
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)

    if week == 1:
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Scenario: Your name is {}, and it's your first day of high school. You're curious to see what the year will have in store for you. You were assigned your seat behind the beautiful {}. She starts chatting with you, and talks about how excited she is for the new school year, and asks what school clubs you plan on joining.".format(protag, waifu))
    elif week == 2:
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Scenario: {} tries to strike up a conversation again with you. Something about your conversation last week was interesting to her. She tells you that she joined a club and is having a lot of fun. She asks if you want to join the club as well.".format(waifu))
    else:
        driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Scenario: ")
        driver.find_element_by_xpath("//button[contains(@aria-label,\"Send\")]").click()
        time.sleep(5)
        cleanstory()

    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)

    proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    proseString = proseString.split("==Week {} of School==".format(week),1)[1]
    print(cleanhtml(proseString))

    print("What do you do?")
    action = raw_input()
    action = "Your Action: " + action

    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(action)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Outcome:")
    driver.find_element_by_xpath("//button[contains(@aria-label,\"Send\")]").click()
    time.sleep(5)

    cleanstory()

    #Display Output
    proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    proseString = cleanhtml(proseString)
    proseString = proseString.split("==Week {}".format(week),1)[1]
    proseString = proseString.split("Outcome:",1)[1]

    #Evaluate Outcome
    driver.switch_to.window(evaluator_window)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Evaluate the following scenario:")
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO START")
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(proseString)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("SCENARIO END")
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Question: On a scale of 1 to 10, was this terrible (1) or very good (10) for their romantic relationship?")
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(Keys.RETURN)
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys("Answer: ")
    driver.find_element_by_xpath("//button[contains(@aria-label,\"Send\")]").click()
    time.sleep(5)

    cleaneval(proseString)

    evalString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    evalString = cleanhtml(evalString)
    evalString = evalString.split(proseString,1)[1]
    evalString = evalString.split("Answer:",1)[1]
    print(evalString)
    number = re.findall('\d+', evalString)
    print(number)
    number = int(str(''.join(number)))

    if number > 6:
        print("[She really liked that interaction.]")
        lovemeter = lovemeter + 1
    elif number < 5:
        print("[She didn't care for that interaction.]")
        lovemeter = lovemeter - 1
    else:
        print("[She didn't feel one way or the other about that interaction.]")
        lovemeter = lovemeter
    
    print(proseString)
    print("Current Love: {}/20".format(lovemeter))

#Everything Else

time.sleep(5)

WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class,\"ProseMirror\")]"))).send_keys('Question - What is the answer to life the universe and everything?')
driver.find_element_by_xpath("//button[contains(@aria-label,\"Send\")]").click()

time.sleep(5)

proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
print(proseString)

while 0 < 1:
    print("What do you say?")
    data = raw_input()
    driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").send_keys(data)
    driver.find_element_by_xpath("//button[contains(@aria-label,\"Send\")]").click()
    time.sleep(5)
    proseString = driver.find_element_by_xpath("//div[contains(@class,\"ProseMirror\")]").get_attribute('innerHTML')
    print(proseString)

time.sleep(2000)
#play_button = driver.find_element_by_name('div.r-12vffkv:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > svg:nth-child(1) > path:nth-child(1)')
#play_button.click()

#time.sleep(5)

#login_form = driver.find_element_by_xpath("//div/a[@href]")
#login_form = login_form.get_attribute("href")
#print(login_form)
driver.quit()
