from typing import NoReturn
from selenium import webdriver
import time
import networkx as nx
import dataset
import pickle
import traceback
import codecs
from selenium.webdriver.common import keys
from selenium.webdriver.common.keys import Keys
import sys
import os
import time
import re
import pandas as pd
from browser_common import JBrowser
import datetime
import dataset

#db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')


def get_designer_statistics():
    global db
    jb=JBrowser()
    data=""
    jb.set_profile_path("Profile 7")
#    googleurl='https://ads.google.com/aw/keywordplanner/'
#    googleurl='https://ads.google.com/aw/overview?ocid=600024232&euid=459838964&__u=6055300436&uscid=600024232&__c=2195332968&authuser=0&subid=ALL-zh-TW-et-g-aw-c-home-awhp_xin1_signin!o2'
#    googleurl='https://ads.google.com/aw/keywordplanner/home?ocid=600024232&euid=459838964&__u=6055300436&uscid=600024232&__c=2195332968&authuser=0&subid=ALL-zh-TW-et-g-aw-c-home-awhp_xin1_signin%21o2'
    googleurl='https://ads.google.com/aw/keywordplanner/home?ocid=600024232&euid=459838964&__u=6055300436&uscid=600024232&__c=2195332968&authuser=0&subid=ALL-zh-TW-et-g-aw-c-home-awhp_xin1_signin%21o2'
    jb.get(googleurl)
    driver=jb.get_driver()
    driver.refresh()

    time.sleep(5)
    driver.refresh()

    time.sleep(15)
#    elmts=driver.find_elements_by_xpath("//div[@role='button' and contains(text(),'尋找新的關鍵字')]")
    elmts=driver.find_elements_by_xpath("//span[contains(text(),'尋找新的關鍵字')]/../..")
    print(elmts)
    for elmt in elmts:
        print('found')
        print(elmt)
        print('clicking....')
        webdriver.ActionChains(driver).move_to_element(elmt).perform()
        webdriver.ActionChains(driver).move_to_element(elmt).click().perform()

    time.sleep(15)

    elmt=driver.find_element_by_xpath("//input[contains(@class,'search-input')]")
#    elmt.send_keys("紓困")
    elmt.send_keys("疫苗")

    time.sleep(3)

    elmt=driver.find_element_by_xpath("//div[contains(text(),'取得結果')]/..")
    print('clicking....')
    webdriver.ActionChains(driver).move_to_element(elmt).perform()
    webdriver.ActionChains(driver).move_to_element(elmt).click().perform()

    time.sleep(5)
    elmt=driver.find_element_by_xpath("//span[contains(text(),'關鍵字檢視畫面')]/../../..")
    print('clicking....')
    webdriver.ActionChains(driver).move_to_element(elmt).perform()
    webdriver.ActionChains(driver).move_to_element(elmt).click().perform()

#    elmt=driver.find_element_by_xpath("//span[contains(text(),'分組檢視畫面')]/../..")
#    elmt=driver.find_element_by_xpath("//span[contains(text(),'分組檢視畫面')]")
    try:
        time.sleep(4)

        elmt=driver.find_element_by_xpath("//material-select-item[contains(@aria-label,'分組檢視畫面')]")
    except:
        print('except')
        traceback.print_exc()
    print('clicking....')

    webdriver.ActionChains(driver).move_to_element(elmt).perform()
    webdriver.ActionChains(driver).move_to_element(elmt).click().perform()

#    try:
#        time.sleep(8)
#
#       elmt=driver.find_element_by_xpath("//span[contains(text(),'分組檢')]/../..")
#   except:
#        print('except')
#        traceback.print_exc()

    print('clicking....')




    time.sleep(40)
    print('after sleep')
#    elmts=driver.find_elements_by_xpath("//div[@class='keyword-text _ngcontent-owh-97']")
    elmts=driver.find_elements_by_xpath("//zippy-icon/..//keyword-text")
    for elmt in elmts:
        print(elmt.text)
        data+=elmt.text+"\n"

    fw=codecs.open('c:/tmp/out.txt','w','utf-8')
    fw.write(data)
    fw.close()

#        print(elmt)

    time.sleep(9999)

    return 'ok'


get_designer_statistics()
