from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions outException
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

import pandas as pd
import numpy as np
import random
import time

def InitWeb(driver, wait):

    time.sleep(8)

    search_card = driver.find_element(By.XPATH, "//div[@class='search card']")
    chain = search_card.find_element(By.XPATH, "./div[0]")

    try:
        closebutton = driver.find_elements(
            By.XPATH, "//button[contains(@class, 'close')]"
        )
        for button in closebutton:
            button.click()
            time.sleep(0.5)
    except:
        pass
    time.sleep(3)
    infocon = wait.until(
        EC.presence_of_element_located(
            (
                By.XPATH,
                "//div[@class='user-info-container']//div[contains(@class, 'open-settings')]",
            )
        )
    )
    infocon.click()
    langselect = wait.until(
        EC.presence_of_element_located(
            (
                By.XPATH,
                "//div[@class='panel-container__body']//app-dropdown-select[@id='language']",
            )
        )
    )

    time.sleep(6)
    if langselect.text.strip() != "English":
        x = langselect.location["x"] + langselect.size["width"] / 2
        y = langselect.location["y"] + langselect.size["height"] / 2
        action = ActionChains(driver)
        action.reset_actions()
        action.move_by_offset(x, y).click().perform()
        selectlist = wait.until(
            EC.presence_of_element_located(
                (By.XPATH, "//div[contains(@class, 'dropdown-list')]")
            )
        )
        engselected = selectlist.find_element(By.XPATH, "./div[1]")
        x = engselected.location["x"] + engselected.size["width"] / 2
        y = engselected.location["y"] + engselected.size["height"] / 2
        action.reset_actions()
        action.move_by_offset(x, y).click().perform()
        time.sleep(5)
        closebutton = driver.find_element(
            By.XPATH,
            "//div[contains(@class,'panel-container')]//button[contains(@class, 'close')]",
        )
        closebutton.click()

    time.sleep(5)


# 显示等待方法
# WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
readfile = "./dextoolsselinium/"
errwritefile = "./dextoolsselinium/"
csvname = "err-bsc-0318134801"

with open("./dextoolsselinium/dexttraders.js", "r", encoding="utf-8") as f:
    scrollscript = f.read()

# df = pd.read_csv(f"./0-AveTokenList/{csvname}.csv",  encoding='utf-8')
df = pd.read_csv(f"{readfile}{csvname}.csv", encoding="utf-8")
# df = pd.read_csv("./dextoolsselinium/errbsc-0318134802.csv",
#                  encoding='latin-1')

# df = pd.read_excel(
#     "./dextoolsselinium/errbsc-0318134801.xlsx")

# df = df.drop(index=range(60))
# print(df)

addlists = [[x, y] for x, y in zip(df["Pair"], df["Tokenaddress"])]

errortokens = [["Pair", "Tokenaddress"]]
driver = webdriver.Chrome()
url = "https://www.dextools.io/app/en"
driver.get(url)
driver.maximize_window()

wait = WebDriverWait(driver, 15, ignored_exceptions=None)
driver.set_script_timeout(250)

# Enter_setEnglish(driver, wait)
# addlists = [["sWirl/WBNB", "0x93c8d3777824db6e21cfdc06a528151ba752a9ea"]]
for pair, tokenadd in addlists:
    try:
        print(f"{pair} begin")

        time.sleep(random.uniform(4, 5))
        header = wait.until(lambda x: x.find_element(By.TAG_NAME, "header"))
        # searchinput = wait.until(lambda x: x.find_element(By.TAG_NAME, "input"))

        searchinput = header.find_element(By.TAG_NAME, "input")
        searchinput.send_keys(tokenadd)
        time.sleep(10)

        divelement = wait.until(
            EC.presence_of_element_located(
                (By.XPATH, "//div[contains(@class, 'search-results')]")
            )
        )
        ulelement = divelement.find_element(By.TAG_NAME, "ul")
        # print(ulelement.get_attribute("outerHTML"))
        linum = int(divelement.find_element(
            By.XPATH, "./div").text.strip().split()[0])
        # print("linum", linum)

        lielements = ulelement.find_elements(By.XPATH, "./li")
        # print(lielements)
        lielements = lielements[:linum]
        try:
            lielement = None
            # print('pair',pair)
            for i in range(linum):
                # print(i,lielements[i].text,'replace',lielements[i].text.replace("\n", ""))
                # print(i,)
                if pair == lielements[i].find_element(
                    By.XPATH, ".//app-token-name"
                ).text.replace("\n", ""):
                    lielement = lielements[i]
                    break

            liclick = lielement.find_element(By.XPATH, "./div")
            liclick.click()
        except:
            # close-search ng-tns-c151-6 ng-star-inserted
            closebutotn = driver.find_element(
                By.XPATH, "//button[contains(@class,'close-search')]"
            )
            closebutotn.click()
            raise

            # print(liclick.get_attribute("outerHTML"))

        time.sleep(random.uniform(8, 10))
        # driver.execute_async_script(scrollscript)
        wait.until(EC.presence_of_element_located(
            (By.XPATH, "//datatable-body")))
        # driver.execute_async_script(scrollscript)
        driver.execute_script(scrollscript)

    except TimeoutException as e:
        # print('timeout', e)
        print(f"time {pair}")
        errortokens.append([pair, tokenadd])
        # raise
        # continue
        # break
    except Exception as e:
        print(f"error {pair}")
        errortokens.append([pair, tokenadd])
        # print(e)
        # raise
        # break

        # continue

print("ready quit")
time.sleep(300)

driver.quit()  # 退出时自动清理临时文件


errdf = pd.DataFrame(errortokens)
print("errortokens list", errortokens)
errdf.to_csv(f"{errwritefile}err-{csvname}.csv", index=False)
