from ..GetImage import get_vc_image
from ..ttshitu import base64_api, error_report
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import time


def main_sign_in(driver):
    print('签到中')
    url = 'https://www.mcbbs.net/plugin.php?id=dc_signin'
    driver.get(url)
    print('正在前往签到界面')
    if get_to_sign_in(driver):
        sign_in_vc(driver)


def sign_in_vc(driver):
    while True:
        image = get_image(driver)
        image = image.convert('RGB')
        image.save('D:\\同步文件\\课程文件\文档\\mcbbs搞事\\AutoTool\\VerificationCode\\image.jpg')
        img_path = 'D:\同步文件\课程文件\文档\mcbbs搞事\AutoTool\VerificationCode\image.jpg'
        result, rid = base64_api(uname='Shellmiao', pwd='sBif.9MMF8Pa', img=img_path)
        if not rid:
            continue
        else:
            print(result)
            if click_vc_words(driver, get_points(result), rid):
                print("打码成功")
            else:
                print("打码失败")
            break


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, rid):
    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)
        temp = ActionChains(driver).move_to_element_with_offset(element, location[0], location[1])
        time.sleep(2)
        temp.click().perform()
        time.sleep(1)
    time.sleep(1)
    button = driver.find_element_by_class_name('geetest_commit_tip')
    time.sleep(1)
    ActionChains(driver).move_to_element(button).click().perform()
    time.sleep(6)
    try:
        button = driver.find_element_by_class_name('geetest_commit_tip')
    except NoSuchElementException:
        return True
    else:
        error_report(rid)
        sign_in_vc(driver)
        return False


def get_to_sign_in(driver):
    if sign_in_button(driver):
        sign_in(driver)
        return True
    else:
        return False


def sign_in_button(driver):
    try:
        button = driver.find_element_by_link_text('签到')
    except NoSuchElementException:
        return False
    else:
        ActionChains(driver).move_to_element(button).click().perform()
        return True


def sign_in(driver):
    while True:
        try:
            time.sleep(2)
            button = driver.find_element_by_id('emot_1')
        except NoSuchElementException:
            continue
        else:
            ActionChains(driver).move_to_element(button).click().perform()
        try:
            time.sleep(2)
            submit = driver.find_element_by_name('signpn')
        except NoSuchElementException:
            continue
        else:
            ActionChains(driver).move_to_element(submit).click().perform()
            break


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