from ..GetImage import get_vc_image
from ..ttshitu import base64_api
from selenium.webdriver.common.action_chains import ActionChains
from io import BytesIO
import time


def sign_in_vc(driver):
    url = 'https://www.mcbbs.net/plugin.php?id=dc_signin'

    driver.get(url)
    get_to_sign_in(driver)
    image = get_image(driver)
    image = image.convert('RGB')
    image.save('D:\同步文件\课程文件\文档\mcbbs搞事\AutoTool\VerificationCode\d.jpg')
    img_path = 'D:\同步文件\课程文件\文档\mcbbs搞事\AutoTool\VerificationCode\d.jpg'
    result = base64_api(uname='Shellmiao', pwd='sBif.9MMF8Pa', img=img_path)
    print(result)
    click_vc_words(driver, get_points(result))


def get_points(result):
    groups = result.split('|')
    locations = [[int(number) for number in group.split(',')] for group in groups]
    return locations


# 验证码的点击和确认
def click_vc_words(driver, locations):
    element = driver.find_element_by_class_name('geetest_widget')
    for location in locations:
        # 因为笔记本缩放是250%，所以坐标要除以2.5
        location[0] = location[0] * 0.5
        location[1] = location[1] * 0.5
        print(location)
        ActionChains(driver).move_to_element_with_offset(element, location[0], location[1]).click().perform()
        time.sleep(1)
    button = driver.find_element_by_class_name('geetest_commit_tip')
    ActionChains(driver).move_to_element(button).click().perform()


def get_to_sign_in(driver):
    sign_in_button(driver)
    sign_in(driver)


def sign_in_button(driver):
    button = driver.find_element_by_link_text('签到')
    ActionChains(driver).move_to_element(button).click().perform()


def sign_in(driver):
    time.sleep(2)
    button = driver.find_element_by_id('emot_1')
    ActionChains(driver).move_to_element(button).click().perform()
    time.sleep(2)
    submit = driver.find_element_by_name('signpn')
    ActionChains(driver).move_to_element(submit).click().perform()


def get_image(driver):
    time.sleep(2)
    element = driver.find_element_by_class_name('geetest_widget')
    vc_image = get_vc_image(element)
    return vc_image
