from tkinter import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup

# id pw log.login

win=Tk()
win.geometry("400x300")
win.title('아이템매니아로그인')
win.option_add("*Font","궁서 20")

#로고
lab_logo = Label(win)
img = PhotoImage(file="./itemmania_logo.gif", master=win)
#img = img.subsample(2)
lab_logo.config(image=img)
lab_logo.pack()

#id 라벨
lab1 = Label(win)
lab1.config(text="아이디")
lab1.pack()

#pw 입력창
ent1 = Entry(win)
ent1.insert(0,"sungro815")
ent1.bind()
ent1.pack()

#pw 라벨
lab2 = Label(win)
lab2.config(text="비밀번호")
lab2.pack()

#pw 입력창
ent2 = Entry(win)
ent2.config(show="*")
ent2.insert(0,"tjekdfl")
ent2.pack()

#로그인버튼
btn = Button(win)
btn.config(text='로그인')

def login():
    my_id = ent1.get()
    my_pass = ent2.get()

    options = ChromeOptions()
    user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    options.add_argument('user-agent=' + user_agent)
    options.add_argument("lang=ko_KR")
    options.add_argument('window-size=1920x1080')
    options.add_argument("disable-gpu")
    options.add_experimental_option("detach", True)

    service = ChromeService(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)
    url = "https://www.itemmania.com/portal/user/p_login_form.html"
    driver.get(url)
    driver.implicitly_wait(5)

    xpath1 = "//input[@name='user_id']"
    driver.find_element(By.XPATH, xpath1).send_keys(my_id)
    driver.implicitly_wait(5)
    xpath2 = "//input[@name='user_password']"
    driver.find_element(By.XPATH, xpath2).send_keys(my_pass)
    driver.implicitly_wait(5)
    xpath3 = "//button[@class='btn btn_blue']"
    driver.find_element(By.XPATH, xpath3).click()

    driver.implicitly_wait(5)
    lab3.config(text='[메세지]로그인성공')

    # 모든 팝업 창 핸들 가져오기
    popup_handles = driver.window_handles

    # 부모 창 핸들 가져오기
    parent_handle = driver.current_window_handle

    # 모든 팝업 창 닫기 (부모 창 제외)
    for handle in popup_handles:
        if handle != parent_handle:
            driver.switch_to.window(handle)
            driver.close()

    # 다시 부모 창으로 돌아가기
    driver.switch_to.window(parent_handle)
    driver.get("https://trade.itemmania.com/index.html")
    driver.implicitly_wait(5)

    xpath3 = "//input[@name='searchGameServer']"
    driver.find_element(By.XPATH, xpath3).click()

    page_content = driver.page_source
    soup = BeautifulSoup(page_content, 'html.parser')

    # <ul> 태그 중에서 속성을 가져올 요소 선택
    selected_ul = soup.find('ul')

    # 선택한 <ul> 요소의 모든 속성 가져오기
    attributes = selected_ul.attrs

    # 속성 출력
    print(attributes)

    # element = soup.find('ul', {'class': 'popular_list'})
    # print(element.text)
    #
    # # 전체 HTML 데이터 가져오기
    # html_data = element.prettify()
    #
    # # HTML 데이터 출력
    # print(html_data)




    #인기게임확인







btn.config(command=login)
btn.pack()

#메세지라벨
lab3 = Label(win)
lab3.pack()

win.mainloop()